您的位置:首页 > 其它

Factory Pattern 工厂模式

2015-07-04 10:50 204 查看
If your code is written to an interface, then it will work with any new classes implementing that interface through polymorphism. However, when you have code that makes use of lots of concrete classes, you're looking for trouble because that code may have
to be changed as new concrete classes are added. So in other words, your code will not be "closed for modification." To extend it with new concrete types, you'll have to reopen it.

Design Principle: Dependency Inversion Principle

Depend upon abstractions. Do not depend upon concrete classes.

High level components should not depend on low-level components. They should both depend on abstractions.

Guidelines:

No variable should hold a reference to a concrete class. (Not new, but factory)

No class should derive from a concrete class.

No method should override an implemented method of any of its base classes. ()

工厂模式将创建Object的方法单独封装出来。对象不使用new创建,而是通过工厂获得。这样将产品的创建与产品的应用分割开,只需要更新工厂方法而不用改变所有用到工厂的代码。

工厂模式分为三种:

1. 简单工厂(simple factory)或者静态工厂方法模式。简单工厂可以看做是工厂方法模式的一种特例,即只有一种工厂,生产一种系列的产品。

创建一个工厂类,内有创建产品的静态方法。

public class SimplePizzaFactory {
public static Pizza createPizza(String type) {
if (type.equals("cheese")) {
return new CheesePizza();
} else if(type.eqauls("veggie")) {
return new VeggiePizza();
} else {
return null;
}
}
}


public class PizzaStore {
SimplePizzaFactory factory;
public Pizza orderPizza(String type) {
Pizza pizza = factory.createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
在简答工厂模式中,简单工厂通常使用静态方法,内涵逻辑判断。

2. 工厂方法模式(factory method)又称为多形性工厂。在工厂方法模式下,有多种工厂,生产多种系列产品。

Factory Method Pattern: defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

定义工厂超类(抽象类或者接口都可):

public abstract class PizzaFactory {
public Pizza createPizza(String type);
}
实现不同的Concrete工厂:

public class NYPizzaFactory extends PizzaFactory {
@Override
public Pizza createPizza(String type) {
if (type.equals("cheese")) {
return new NYCheesePizza();
} else if(type.eqauls("veggie")) {
return new NYVeggiePizza();
} else {
return null;
}
}
}

public class CAPizzaFactory extends PizzaFactory {
@Override
public Pizza createPizza(String type) {
if (type.equals("cheese")) {
return new CACheesePizza();
} else if(type.eqauls("veggie")) {
return new CAVeggiePizza();
} else {
return null;
}
}
}


在不同的应用下使用不同的工厂:

public abstract PizzaStore {
PizzaFactory factory;
public Pizza orderPizza(String type) {
Pizza pizza = factory.createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}
public class NYPizzaStore extends PizzaStore {
public NYPizzaStore() {
this.factory = new NYPizzaFactory() ;
}
}
public class CAPizzaStore {
public NYPizzaStore() {
this.factory = new CAPizzaFactory() ;
}
}
这样在使用工厂时,可以选择使用什么样的工厂。每种工厂内可以采用简单工厂方法(但不能声明为静态的,static method不能被override)。
一种工厂生产一种系列的产品。

3. 抽象工厂(abstract factory)生产产品族。

Abstract Factory Pattern: provides an interface for creating families of related or dependent objects without specifying their concrete classes. Composition.

所谓产品族(product family),是指位于不同产品等级结构中,功能相关联的产品组成的家族。由多个超类作为部件,组合成一个产品。

所谓产品等级,是指由相同结构的产品。一个超类的各种Concrete类,组合成一个产品等级结构。

图中,主板、芯片组、CPU各为一个等级结构。Intel的主板、芯片组和CPU相互兼容,构成一个产品族。

定义抽象工厂:

public interface AbstractFactory {
public Cpu createCpu();
public Mainboard createMainboard();
}
定义产品族工厂:
public class IntelFactory implements AbstractFactory {
@Override
public Cpu createCpu() {
return new IntelCpu(755);
}

@Override
public Mainboard createMainboard() {
return new IntelMainboard(755);
}
}
public class AmdFactory implements AbstractFactory {
@Override
public Cpu createCpu() {
return new IntelCpu(938);
}

@Override
public Mainboard createMainboard() {
return new IntelMainboard(938);
}
}


定义应用:

public class ComputetStore {
public Computer makeComputer(AbstractFactory factory) {
Computer computer = new Computer();
CPU cpu = factory.createCpu();
Mainboard mainboard = factory.createMainboard();
computer.addCPU(cpu);
computer.addMainBoard(mainboard);
return computer;
}
}

public class Client {
public static void main(String[]args){
ComputerStore store = new ComputerStore();
AbstractFactory factory = new IntelFactory();
Computer computer = store.makeComputer(factory);
}
}
抽象工厂生产产品族。每个实例工厂生产一种产品族,一个产品族有多个部件构成。实例工厂保证各个部件相互兼容。

简单工厂: 基于一种超类生产产品,子类较少,能够用简单的逻辑判断。

工厂方法:生产系列产品。有多种超类,或者inheritance tree层次多。逻辑判断层次多。

抽象工厂:生产产品族。一个产品有多个部件构成。

应用:Pizza,Car
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: