質問

while coding a training exercise I thought that I was using an abstract factory pattern, but as it turns out I seem to have implemented an abstract class pattern (according to the guy who reviewed my code).

I implemented my alleged 'factory' by using the explanations I found online and an UML chart. I just studied them again and compared it to my code and still can't see the error and the reason why it should only be an abstract class pattern.

As shown in the UML chart I've found, I implemented a single abstract class "AbstractMachineFactory" that defines all the needed attributes and methods for my concrete machines. They only differ in the product produced.

Then I implemented three different ConcreteMachineType classes that extend AbstractMachineFactory, also exactly as shown in the UML chart.

Here's my code:

Factory:

public abstract class abstractMachineFactory {

public final int     machineID;
public int           producedGoods;
public boolean       status;

private final Logger abstractMachineFactoryLogger = LoggerFactory.getLogger( abstractMachineFactory.class );

public abstractMachineFactory( final int ID, final boolean state ) {
    this.machineID = ID;
    this.status = state;
}

public Integer getMachineID() {
    return this.machineID;
}

public Integer getNumberOfProducedGoods() {
    return this.producedGoods;
}

public Boolean getStatus() {
    return this.status;
}

public void startUp() {
    this.status = true;
}

public void shutDown() {
    this.status = false;
}

public abstract void produceGoods();

}

One of the concrete machines:

public class concreteMachineType1 extends abstractMachineFactory {

private final Logger concreteMachineType1Logger = LoggerFactory.getLogger( concreteMachineType1.class );

public concreteMachineType1( final int ID, final boolean state ) {
    super( ID, state );
}

@Override
public void produceGoods() {

    machineController machineController = exercise.java.basics.machine.machineController.getInstance();

    int timeToProduce = (int) ( ( Math.random() * ( 6 - 1 ) ) + 1 ) * 1000;


    try {
        Thread.sleep( timeToProduce );
    } catch ( InterruptedException ex ) {
        Thread.currentThread().interrupt();
    }

    machineController.moveNailToStorage();

    this.producedGoods++;

}

}

I create new machines via a Controller class that handles all actions regarding the machines. All created machines are stored in an ArrayList in order to access them later on within the controller class. Here's a small extract from that class regarding the creation of a new machine:

 public void createMachine( final MachineType type ) {

    int machineID;
    if ( MachineType.TYPE1.equals( type ) ) {

        machineID = this.arrType1.size() + 1;
        abstractMachineFactory newMachine = new concreteMachineType1( machineID, false );
        this.numberOfType1++;
        this.arrType1.add( newMachine );

    } else if ( MachineType.TYPE2.equals( type ) ) {

        machineID = this.arrType2.size() + 1;
        abstractMachineFactory newMachine = new concreteMachineType2( machineID, false );
        this.numberOfType2++;
        this.arrType2.add( newMachine );

    }

    else if ( MachineType.TYPE3.equals( type ) ) {

        machineID = this.arrType3.size() + 1;
        abstractMachineFactory newMachine = new concreteMachineType3( machineID, false );
        this.numberOfType3++;
        this.arrType3.add( newMachine );

    }
}

best regards daZza

役に立ちましたか?

解決

The factory pattern uses a factory to return objects of a base type.

I expect what he was expecting was something like this:

public abstract class AbstractBase() {}

public class Concrete1 extends AbstractBase() {}

public class Concrete2 extends AbstractBase() {}

public class Factory() {
    enum BaseType {
        Type1,
        Type2
    };

    public static AbstractBase getObject(BaseType baseType) {
        if (baseType == Type1) {
            return new Concrete1();
        } else if (baseType == Type2) {
            return new Concrete2();
        }
        throw new IllegalArgumentException();
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top