设计模式:工厂与工厂方法对抽象工厂 [英] Design Patterns: Factory vs Factory method vs Abstract Factory

查看:123
本文介绍了设计模式:工厂与工厂方法对抽象工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从网站上阅读设计模式。



在这里我读到关于工厂,工厂方法和抽象工厂,但他们是如此困惑,我不清楚的定义。根据定义


Factory - 创建对象而不将实例化逻辑暴露给客户端,并通过公共接口引用新创建的对象。工厂方法 - 定义用于创建对象的界面,但让子类决定要实例化哪个类,并引用引用(Factory)方法



<通过一个共同的界面来新创建的对象。



抽象工厂 - 提供创建一系列相关对象的界面,而无需明确指定他们的类。


我还看过关于抽象工厂对工厂方法的其他stackoverflow线程,但是在这里绘制的UML图形使我的理解更加糟糕。



任何人都可以告诉我


  1. 这三种模式是如何彼此不同的?

  2. 何时使用哪个?

  3. 如果可能的话,与这些模式相关的任何java示例?


解决方案

所有三种工厂类型都做同样的事情:他们是一个聪明的构造函数。



假设您想要创建两种Fruit:Apple和Orange。



Factory



工厂是固定的,因为你只有一个实现没有子类。在这种情况下,你将有一个这样的类:

  class FruitFactory {

public Apple makeApple (){
//在此创建Apple的代码。
}

public Orange makeOrange(){
//在这里创建一个橙色的代码。
}

}

用例:构建一个苹果或一个橙色有点太复杂,无法在构造函数中处理。



工厂方法



工厂方法是通常当你在一个类中有一些通用的处理方法,但是想要改变你实际使用的那种水果。所以:

 抽象类FruitPicker {

protected abstract Fruit makeFruit();

public void pickFruit(){
private final Fruit f = makeFruit(); //我们将要工作的水果..
< bla bla bla>
}
}

...那么你可以重用常用的功能 FruitPicker.pickFruit()通过在子类中实现工厂方法:

  OrangePicker类扩展了FruitPicker {

@Override
protected Fruit makeFruit(){
return new Orange();
}
}



抽象工厂



抽象工厂通常用于诸如依赖注入/策略之类的东西,当您想要创建需要具有同一类型的整个对象系列时,并且具有一些常见的基类。这是一个模糊的水果相关例子。这里的用例是我们想确保我们不会在Apple上不小心使用OrangePicker。只要我们从同一个工厂获得我们的水果和选择器,他们将会匹配。

 界面PlantFactory {

植物makePlant();

选择器makePicker();

}

public class AppleFactory实现PlantFactory {
工厂makePlant(){
返回新的Apple();
}

选择器makePicker(){
返回新的ApplePicker();
}
}

public class OrangeFactory implements PlantFactory {
Plant makePlant(){
return new Orange();
}

选择器makePicker(){
返回新的OrangePicker();
}
}


I was reading design patterns from a website

There I read about Factory, Factory method and Abstract factory but they are so confusing, am not clear on the definition. According to definitions

Factory - Creates objects without exposing the instantiation logic to the client and Refers to the newly created object through a common interface. Is a simplified version of Factory Method

Factory Method - Defines an interface for creating objects, but let subclasses to decide which class to instantiate and Refers to the newly created object through a common interface.

Abstract Factory - Offers the interface for creating a family of related objects, without explicitly specifying their classes.

I also looked the other stackoverflow threads regarding Abstract Factory vs Factory Method but the UML diagrams drawn there make my understanding even worse.

Can anyone please tell me

  1. How are these three patterns different from each other?
  2. When to use which?
  3. And also if possible, any java examples related to these patterns?

解决方案

All three Factory types do the same thing: They are a "smart constructor".

Let's say you want to be able to create two kinds of Fruit: Apple and Orange.

Factory

Factory is "fixed", in that you have just one implementation with no subclassing. In this case, you will have a class like this:

class FruitFactory {

  public Apple makeApple() {
    // Code for creating an Apple here.
  }

  public Orange makeOrange() {
    // Code for creating an orange here.
  }

}

Use case: Constructing an Apple or an Orange is a bit too complex to handle in the constructor for either.

Factory Method

Factory method is generally used when you have some generic processing in a class, but want to vary which kind of fruit you actually use. So:

abstract class FruitPicker {

  protected abstract Fruit makeFruit();

  public void pickFruit() {
    private final Fruit f = makeFruit(); // The fruit we will work on..
    <bla bla bla>
  }
}

...then you can reuse the common functionality in FruitPicker.pickFruit() by implementing a factory method in subclasses:

class OrangePicker extends FruitPicker {

  @Override
  protected Fruit makeFruit() {
    return new Orange();
  }
}

Abstract Factory

Abstract factory is normally used for things like dependency injection/strategy, when you want to be able to create a whole family of objects that need to be of "the same kind", and have some common base classes. Here's a vaguely fruit-related example. The use case here is that we want to make sure that we don't accidentally use an OrangePicker on an Apple. As long at we get our Fruit and Picker from the same factory, they will match.

interface PlantFactory {

  Plant makePlant();

  Picker makePicker(); 

}

public class AppleFactory implements PlantFactory {
  Plant makePlant() {
    return new Apple();
  }

  Picker makePicker() {
    return new ApplePicker();
  }
}

public class OrangeFactory implements PlantFactory {
  Plant makePlant() {
    return new Orange();
  }

  Picker makePicker() {
    return new OrangePicker();
  }
}

这篇关于设计模式:工厂与工厂方法对抽象工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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