简单工厂模式VS工厂方法模式
简单工厂模式
图片来源:百度
操作接口
package com.zhongsb.charpter1;
public interface Operate {
double getResult(double a, double b);
}
加法类,实现运算接口
package com.zhongsb.charpter1;
public class Add implements Operate {
@Override
public double getResult(double a, double b) {
return a+b;
}
}
除法类,实现运算接口
package com.zhongsb.charpter1;
public class Divide implements Operate {
@Override
public double getResult(double a, double b) {
return a/b;
}
}
减法类,实现运算接口
package com.zhongsb.charpter1;
public class Minus implements Operate {
@Override
public double getResult(double a, double b) {
return a-b;
}
}
乘法类,实现运算接口
package com.zhongsb.charpter1;
public class Muti implements Operate {
@Override
public double getResult(double a, double b) {
return a*b;
}
}
工厂类
package com.zhongsb.charpter1;
public class OperateFactory {
Operate op = null;
String type = "";
public OperateFactory (){
}
public OperateFactory (Operate op){
this.op=op;
}
public Operate getOp(String type){
switch(type){
case "+":{
op=new Add();
break;
}case "-":{
op=new Minus();
break;
}case "*":{
op=new Muti();
break;
}case "/":{
op=new Divide();
break;
}
}
/*if(type.equals("+")){
op=new Add();
}else if(type.equals("-")){
op=new Minus();
}else if(type.equals("*")){
op=new Muti();
}else if(type.equals("/")){
op=new Divide();
}*/
return op;
}
}
测试类
package com.zhongsb.charpter1;
import java.util.Scanner;
/**
* @Project: 20170816BigTalkDesignPattern
* @Title: TestCompute
* @Description:
* @author: zhongsb
* @date: 2017年8月17日
* @company: webyun
* @Copyright: Copyright (c) 2014
* @UpdateUser:
* @UpdateDescription:
* @UpdateTime:下午6:31:59
* @Version :V1.0
*/
public class TestCompute {
public static void main(String[] args) {
System.out.println("请输入需要运算的两个数:");
Scanner sca = new Scanner(System.in);
double a = sca.nextDouble();
double b = sca.nextDouble();
System.out.println("请输入运算符号:");
String next = sca.next();
Operate op = null;
OperateFactory factory = new OperateFactory();
op = factory.getOp(next);
System.out.println("运算结果是="+op.getResult(a, b));
}
}
工厂方法模式
图片来源:百度
操作类
package com.zhongsb.charpter8;
public class Operate {
double getResult(double a, double b){
return 0;
};
}
加法类,继承操作类
public class Add extends Operate {
@Override
public double getResult(double a, double b) {
return a+b;
}
}
除法类,继承操作类
package com.zhongsb.charpter8;
public class Divide extends Operate {
@Override
public double getResult(double a, double b) {
return a/b;
}
}
乘法类,继承操作类
package com.zhongsb.charpter8;
public class Muti extends Operate {
@Override
public double getResult(double a, double b) {
return a*b;
}
}
减法类,继承操作类
package com.zhongsb.charpter8;
public class Minus extends Operate {
@Override
public double getResult(double a, double b) {
return a-b;
}
}
工厂接口
package com.zhongsb.charpter8;
public interface IFactory {
Operate createOperate();
}
加法工厂,实现工厂接口
package com.zhongsb.charpter8;
public class AddFactory implements IFactory {
@Override
public Operate createOperate() {
return new Add();
}
}
除法工厂,实现工厂接口
package com.zhongsb.charpter8;
public class DivideFactory implements IFactory {
@Override
public Operate createOperate() {
return new Divide();
}
}
乘法工厂,实现工厂接口
package com.zhongsb.charpter8;
public class MutiFactory implements IFactory {
@Override
public Operate createOperate() {
return new Muti();
}
}
减法工厂,实现工厂接口
package com.zhongsb.charpter8;
public class MinusFactory implements IFactory {
@Override
public Operate createOperate() {
return new Minus();
}
}
工厂方法测试类
package com.zhongsb.charpter8;
public class TestFactoryMethod {
public static void main(String[] args) {
IFactory operFactory = new MinusFactory();
Operate operate = operFactory.createOperate();
double result = operate.getResult(10, 12);
System.out.println(result);
}
}
说明:
1、当需要增加一种运算时,简单工厂需要增加一个运算类,修改工厂方法的选择判断语句——违背了“开放-封闭原则”。
2、工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断还是存在的,也就是说,工厂方法把简单工厂的内部逻辑移
到了客户端代码来进行。
思考:什么时候用简单工厂?什么时候用工厂方法模式?
实现一个加减乘除运算的例子
参考:《大话设计模式》
所有发生在我们身上的事件都是一个经过仔细包装的礼物。只要我们愿意面对它有时候有点丑恶的包装,带着耐心和勇气一点一点的拆开包装的话,我们会惊喜的看到里面珍藏的礼物。
----遇见未知的自己