您的位置:首页 > 其它

工厂模式(简单工厂模式,工厂方法模式,抽象工厂模式)

2016-04-24 17:48 393 查看

一、简单工厂模式

简单工厂(Simple Factory)模式又称为静态工厂方法(Static Factory Method)模式,属于类的创建型模式,但其不属于23种GOF设计模式之一,通常它根据自变量的不同返回不同的类的实例。

UML图:



示例代码:

public abstract class Operation {
double numberA;
double numberB;
abstract double getResult();
}

public class AddOperation extends Operation {
public double getResult() {
return numberA + numberB;
}
}
public class MultiplyOperation extends Operation {
double getResult() {
return numberA * numberB;
}
}
public class DivOperation extends Operation {
double getResult() {
return numberA / numberB;
}
}
public class SubOperation extends Operation {

double getResult() {
return numberA - numberB;
}
}
public class OperationSimpleFactory {
public static Operation createOperate(String operate) {
Operation oper = null;
switch (operate) {
case "+":
oper = new AddOperation();
break;
case "-":
oper = new SubOperation();
break;
case " *":
oper = new MultiplyOperation();
break;
case "/ ":
oper = new DivOperation();
break;
}
return oper;
}
}


优点:

1、简单工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。

2、让客户端不用管该用哪个类的实例,只要把条件给工厂,工厂自动就给出了实例,客户端只要去做运算就可以了,不同的实例会实现不同的运算。

3、简单工厂实现了责任的分割。

缺点:

1.如果要加一个新的功能,就要在原有方法中加一个分支条件,就要修改原有的类,违背了开放-封闭原则,于是工厂方法就来了

二、工厂模式

1、工厂方法模式定义了一个创建对象的接口。

2、由子类决定实例化的类是哪一个。

3、工厂方法使一个类的实例化延迟到其子类。

UML图:



先构建一个工厂接口

public interface IFactory {
Operation CreateOperation();
}


然后加减乘除各建一个具体工厂去实现这个接口

public class AddFactory implements IFactory {
public Operation CreateOperation() {
return new AddOperation();
}

}
public class SubFactory implements IFactory {
public Operation CreateOperation() {
return new SubOperation();
}
}
public class MulFactory implements IFactory {
public Operation CreateOperation() {
return new MultiplyOperation();
}
}
public class DivFactory implements IFactory {
public Operation CreateOperation() {
return new DivOperation();
}
}
public class IFactoryImplement {
public static void main(String[] args) {
IFactory operFactory = new AddFactory();
Operation oper = operFactory.CreateOperation();
oper.numberA = 1;
oper.numberB = 2;
double result = oper.getResult();
System.err.println(result);
}
}


工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断转移到了客户端代码来进行。如果要加功能,本来是改工厂类的,而现在是修改客户端

小结:

1、 一个抽象产品类,可以派生出多个具体产品类, 一个抽象工厂类,可以派生出多个具体工厂类, 每个具体工厂类只能创建一个具体产品类的实例。

2、工厂方法克服了简单工厂违背开放-封闭原则的缺点,又保持了封装对象创建过程的优点。

3、它们都是集中封装了对象的创建,使得要更换对象时,不需要做大的改动就可实现,降低了客户程序与产品对象的耦合。

4、工厂方法模式是简单工厂模式的进一步抽象和推广,由于使用了多态性,工
4000
厂方法模式保持了简单工厂模式的优点,而且克服了它的缺点。

5、缺点是由于每加一个产品,就需要加一个产品工厂的类,增加了额外了开发量。

三、抽象工厂模式

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

示例代码:

public interface Fridge {
void make();
}
public interface TV {
void make();
}
public class HisenseFridge implements Fridge {
public void make() {
System.out.println("海信制造的冰箱");

}
}
public class HaierFridge implements Fridge {
public void make() {
System.out.println("海尔制造的冰箱");
}
}
public class HisenseTV implements TV {
public void make() {
System.out.println("海信制造的电视");
}
}
public class HaierTV implements TV {
public void make() {
System.out.println("海尔制造的TV");
}
}
public interface IFactory {
Fridge createFridge();
TV createTV();
}
public class HaierFactory implements IFactory {
public Fridge createFridge() {
return new HaierFridge();
}
public TV createTV() {
return new HaierTV();
}
}
public class HisenseFactory implements IFactory {
public Fridge createFridge() {
return new HisenseFridge();
}
public TV createTV() {
return new HisenseTV();
}
}
public class AbstractFactoryTest {

public static void main(String[] args) {
IFactory haierFactory = new HaierFactory();
Fridge haierFridge = haierFactory.createFridge();
TV haierTV = haierFactory.createTV();
haierFridge.make();
haierTV.make();

IFactory hisenseFactory = new HisenseFactory();
Fridge hisenseFridge = hisenseFactory.createFridge();
TV hisenseTV = hisenseFactory.createTV();
hisenseFridge.make();
hisenseTV.make();
}

}


小结:

1、多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。 2、一个抽象工厂类,可以派生出多个具体工厂类。

3、 每个具体工厂类可以创建多个具体产品类的实例。

参考资料设计模式PPT
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计模式