工厂方法模式比Simple Factory更灵活吗? [英] Is the Factory Method Pattern more flexible than Simple Factory?

查看:127
本文介绍了工厂方法模式比Simple Factory更灵活吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读书头号:设计模式,我发现这是一个很好的介绍设计模式。但是,我有一个关于他们在第4章中提出的声明的问题:



他们定义了Simple Factory模式如下(Java伪代码):

  public abstract class Product 
{
//产品特性
//混凝土产品应该子类化这个
}
public class SimpleFactory {
public Product createProduct(){
//返回Product
的一些子类的实例
}
公共类商店{
SimpleFactory工厂;
public Product orderProduct(){
Product product = factory.createProduct();
//对产品进行一些操作
返回产品;
}
}

Factory Method的定义如下(class产品保持不变,省略):

  public abstract class Store {
//具体商店必须将此类和override createProduct()
public abstract Product createProduct();

public产品orderProduct(){
产品product = createProduct();
//对产品进行一些操作
返回产品;
}
}

然后作者继续声称出厂方法模式比Simple Factory灵活得多,因为简单工厂是一次性处理,使用Factory Method创建一个框架,让子类决定应该使用哪个实现(第135页)。



现在我不明白为什么这是真的。我看到它的方式,简单工厂在某种意义上比工厂方法灵活一些更加:您可以将Simple Factory(而不是子类化Store)子类化以获得基本相同的行为。如果您愿意,您甚至可以在运行时更改行为!简单工厂的唯一缺点我可以想到的是,产品创建取决于Store类的状态变量:这是作者所称的灵活性,还是我缺少某些东西?

解决方案

你是绝对正确的:作者的假设是你不会将 SimpleFactory 假设(除非 SimpleFactory 标记为 final )。



<因为 SimpleFactory 不是最终的,你可以绝对地将其子类化,获得比使用工厂方法更多的灵活性,因为 SimpleFactory 使用组合替换继承。



一个更好的方法是使 SimpleFactory 一个接口。这样做可以让您根据自己的喜好选择组合或继承,因为当您的 Store 类已经继承类时,界面不会限制您。

  public interface SimpleFactory {
Product createProduct();
}

然后你可以使用组合

  public class FactoryImpl实现SimpleFactory {
public Product createProduct(){
//返回Product
的一些子类的实例$
}
public class StoreComposition {
SimpleFactory factory = new FactoryImpl();
}

或继承/组合组合

  public class StoreInheritance implements SimpleFactory {
SimpleFactory factory = this;
public产品createProduct(){
//返回Product
的一些子类的实例
}


I've been reading the book Head First: Design Patterns, which I have found to be a good introduction to design patterns. However, I've got a question about a claim they make in Chapter 4:

They define the "Simple Factory" pattern as follows (Java pseudocode):

public abstract class Product
{
// Product characteristics
// Concrete Products should subclass this    
}
public class SimpleFactory {
    public Product createProduct(){
        // Return an instance of some subclass of Product
    }
}
public class Store {
    SimpleFactory factory;
    public Product orderProduct(){
        Product product = factory.createProduct();
        // Do some manipulation on product
        return product;
    }
}

The "Factory Method" is defined as follows (class Product remains the same and is omitted):

public abstract class Store {
//Concrete Stores must subclass this and override createProduct()
    public abstract Product createProduct();

    public Product orderProduct(){
        Product product = createProduct();
        // Do some manipulation on product
        return product;
    } 
}

Then the authors go on to claim that the Factory Method Pattern is much more flexible than Simple Factory, because while Simple Factory is "a one shot deal, with Factory Method you are creating a framework that lets the subclasses decide which implementation should be used" (page 135).

Now I don't get why this is true. The way I see it, Simple Factory is, in some senses, slightly more flexible than Factory Method: you can subclass the Simple Factory (instead of subclassing the Store) to get essentially the same behavior. You can even change the behavior at runtime if you wish! The only disadvantage of Simple Factory I could think of is when the product creation depends on state variables of the Store class: is this what the authors are calling flexibility, or am I missing something?

解决方案

You are absolutely right: author's assumption has been that you are not going to subclass SimpleFactory, which is not a fair assumption to make (unless SimpleFactory is marked final).

Since SimpleFactory is not final, you can definitely subclass it, gaining more flexibility than with a factory method, because SimpleFactory replaces inheritance with composition.

An even better approach would be making SimpleFactory an interface. Doing so would let you pick composition or inheritance according to your preference, because an interface would not limit you in cases when your Store class already inherits a class.

public interface SimpleFactory {
    Product createProduct();
}

Then you can use either composition

public class FactoryImpl implements SimpleFactory {
    public Product createProduct(){
        // Return an instance of some subclass of Product
    }
}
public class StoreComposition {
    SimpleFactory factory = new FactoryImpl();
}

or inheritance/composition combo

public class StoreInheritance implements SimpleFactory {
    SimpleFactory factory = this;
    public Product createProduct(){
        // Return an instance of some subclass of Product
    }
}

这篇关于工厂方法模式比Simple Factory更灵活吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆