【问题标题】:How is the following design pattern a Factory method. I believe it is more of a Abstract Factory than factory method以下设计模式如何成为工厂方法。我相信它更像是一个抽象工厂而不是工厂方法
【发布时间】:2019-11-25 00:26:56
【问题描述】:

以下问题中使用的设计模式如何是工厂方法?我怀疑它既不是工厂方法也不是抽象工厂。它是两者的混合体。

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);
}

我相信这不是工厂方法:

  1. 使用接口VehicleFactory(抽象工厂中使用)

  2. 正在打造油车、电动车等产品系列

  3. 它不使用继承。

出于同样的原因,我相信它是一种抽象工厂方法。但它不使用工厂的组合。

究竟是什么模式?

谢谢

【问题讨论】:

  • 您需要做的第一件事是确定您使用的语言,并相应地标记
  • @TheGeneral 是不是很接近?另外,模式语言是特定的吗?
  • softwareengineering.stackexchange.com 更适合设计模式及其讨论
  • @TheGeneral 在引用其他网站时,指出cross-posting is frowned upon 通常会有所帮助
  • 设计模式是对常见问题的通用解决方案。代码期望解决什么设计问题/意图/要求?

标签: java c# oop design-patterns factory-pattern


【解决方案1】:

您的代码示例实现了抽象工厂模式。满足模式的定义。

要调用策略设计模式,它必须满足某些约束。一个限制是,它必须有一个明确的定义。

您可以通过根据定义验证您的设计来简单地验证您的实现是否符合模式:

抽象工厂

“为创建相关或依赖对象 (2) 的族 (1) 提供接口,而无需指定它们的具体类 (3)。”

(1) 你已经定义了一个抽象工厂:

interface VehicleFactory {

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

(2) 你已经定义了一个抽象的产品系列:

interface Vehicle { }

(3) 由于抽象工厂返回一个抽象类型,客户端不必知道具体实现:

Vehicle make(int seats);

工厂方法

“定义用于创建对象的接口 (1),但让子类决定实例化哪个类 (2)。Factory 方法允许类将其使用的实例化推迟到子类。” (四人组)

您的代码显然不满足定义的 (2)。

根据定义的正确实现是:

(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();
  }
}

用法

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

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多