简体   繁体   中英

How is the following design pattern a Factory method. I believe it is more of a Abstract Factory than factory method

How is the design pattern used in the following question a Factory method?. I suspect it is neither Factory Method nor Abstract Factory. It is a mix of both.

interface Vehicle { }
class Car implements Vehicle{ }       // Oil Vehicles
class Bus implements Vehicle{ }
class ElectricCar implements Vehicle{ } // ElectricVehicles
class ElectricBus implements Vehicle{ }

interface VehicleFactory{
  Vehicle make(int seats);
}

class OilVehicleFactory implements VehicleFactory{ // Oil vehicle Factory 
  @Override
  Vehicle make(int  seats){
     if(seats > 5){
        return new Bus();
     }
     else{
          return new Car();
    }
}

class ElectricVehicleFactory implements VehicleFactory{ // Electric Vehicle Factory
  @Override
  Vehicle make(int  seats){
     if(seats > 5){
        return new ElectricBus();
     }
     else{
          return new ElectricCar();
    }
}

class Client{                   // Cient Code
    boolean isElectric;

    void doSomething(){
     VehicleFactory  factory;
     if(isElectric){ 
       factory = new ElectricVehicleFactory();   
     }
     else{
       factory = new oilVehicleFactory();
    }

    Vehicle vehicle = factory.make(5);
}

I believe it is NOT a factory method as:

  1. It is using the interface VehicleFactory (which is used in Abstract Factory)

  2. It is creating families of products such as Oil vehicles and Electric Vehicle

  3. It doesn't use inheritance.

For the same reasons I believe that It is an Abstract factory method. But it doesn't use Composition for the factory.

What pattern is it exactly?

Thanks

Your code example implements the Abstract Factory pattern. It satisfies the definition of the pattern.

To call a strategy Design Pattern it must satisfy certain constraints. One constraint is, that it must have a clear definition.

You can simply verify if your implementations complies to a pattern by validating your design against the definition:

Abstract Factory

"Provide an interface for creating families (1) of related or dependent objects (2) without specifying their concrete classes (3)."

(1) You have defined an abstract factory:

interface VehicleFactory {

  // Returns a
  Vehicle make(int seats);
}

(2) You have defined an abstract product family:

interface Vehicle { }

(3) Since the abstract factory returns an abstract type, the client don't have to know concrete implementations:

Vehicle make(int seats);

Factory Method

"Define an interface for creating an object (1), but let subclasses decide which class to instantiate (2). The Factory method lets a class defer instantiation it uses to subclasses." (Gang Of Four)

Your code obviously doesn't satisfy (2) of the definition.

The proper implementation according to the definition would be:

(1)

interface VehicleFactory {

  // Returns a
  Vehicle makeVehicle();
}

(2)

abstract class Driver implements VehicleFactory {

  // Let subclasses decide which class to instantiate
  abstract protected Vehicle make();

  public void driveToLocation(Location destination) {

    // Let subclasses decide which class to instantiate
    Vehicle car = makeVehicle();

    car.driveTo(destination);
  }
}

class TaxiDriver extends Driver {
  @Override    
  protected Vehicle makeVehicle() {
    return new Taxi();
  }
}

class BusDriver extends Driver {
  @Override    
  protected Vehicle makeVehicle() {
    return new Bus();
  }
}

Usage

Location destination = new Location("Workplace");
Driver driver = new BusDriver();
driver.driveToLocation(destination);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM