一,定义
工厂模式会让你想要获取的操作来获得你想要的的功能类(对象),然后去做一些事情
工厂模式把所有的逻辑判断在工厂中去处理了(OperationFactory),把你想要的操作交给工厂,再去实例化想要的具体的操作类
使用场景:子类型不会变(增加)的子类用比较好,多变的话用策略模式
二,简单工厂模式
贴代码:
package com.boot.lijunhao.lijunhao;
/**
*
* 在这里放入共有的属性和方法(抽象类)
*
* Created by lijunhao on 2017/8/11.
*/
public abstract class Operation {
private Integer num1;
private Integer num2;
/**
* 想要返回的结果方法也写在这里
* @return
*/
public Integer getResout() {
int resout =0;
return resout;
}
public Integer getNum1() {
return num1;
}
public void setNum1(Integer num1) {
this.num1 = num1;
}
public Integer getNum2() {
return num2;
}
public void setNum2(Integer num2) {
this.num2 = num2;
}
}
package com.boot.lijunhao.lijunhao;
/**
* 加法
* Created by lijunhao on 2017/8/11.
*/
public class OperationAdd extends Operation {
@Override
public Integer getResout() {
int i = getNum1() + getNum2();
return i;
}
}
package com.boot.lijunhao.lijunhao;
/**
* 减法
* Created by lijunhao on 2017/8/11.
*/
public class OperationReduce extends Operation {
@Override
public Integer getResout() {
int i = getNum1() - getNum2();
return i;
}
}
package com.boot.lijunhao.lijunhao;
/**
* Created by lijunhao on 2017/8/11.
*/
public class OperationFactory {
/**
* 在这里判断
* @param operations
* @return
*/
public static Operation getOperation(String operations) {
Operation operation = null;
switch (operations) {
case "+":
operation = new OperationAdd();
break;
case "-":
operation = new OperationReduce();
break;
default:
operation = null;
break;
}
return operation;
}
}
public static void main(String[] args) {
Operation operation = OperationFactory.getOperation("-");
operation.setNum1(12);
operation.setNum2(2);
Integer resout = operation.getResout();
System.out.println(resout);
Operation operation1 = OperationFactory.getOperation("+");
operation1.setNum1(12);
operation1.setNum2(2);
Integer resout1 = operation1.getResout();
System.out.println(resout1);
}
三,工厂模式
另外一种工厂模式为直接创建一个所想要的AFactory,在具体的factory中直接去实例化具体的类(在客户端就能隐藏掉具体实例化的类)
-
3.1代码示例
package com.boot.lijunhao.factory;
/**
* 工厂类接口,提供工厂的统一规范
* Created by lijunhao on 2017/8/25.
*/
public interface Factory {
Person createPersonAFactory();
}
package com.boot.lijunhao.factory;
/**
* 抽象的人
* Created by lijunhao on 2017/8/25.
*/
public interface Person {
void speak();
void shaodi();
}
package com.boot.lijunhao.factory;
import lombok.extern.slf4j.Slf4j;
/**
* Created by lijunhao on 2017/8/25.
*/
@Slf4j
public class PersonA implements Person {
@Override
public void speak() {
log.info("speak在类(PersonA.java:13)行:{}","A说了句话");
}
@Override
public void shaodi() {
log.info("shaodi在类(PersonA.java:18)行:{}","A扫了地");
}
}
package com.boot.lijunhao.factory;
import lombok.extern.slf4j.Slf4j;
/**
* Created by lijunhao on 2017/8/25.
*/
@Slf4j
public class PersonB implements Person {
@Override
public void speak() {
log.info("speak在类(PersonA.java:13)行:{}","B说了句话");
}
@Override
public void shaodi() {
log.info("shaodi在类(PersonA.java:18)行:{}","B扫了地");
}
}
package com.boot.lijunhao.factory;
import lombok.extern.slf4j.Slf4j;
/**
* 具体的工厂类,产生具体的PersonA的类(通过PersonAFactory可以隐藏掉真实的PersonA)
* Created by lijunhao on 2017/8/25.
*/
@Slf4j
public class PersonFactory implements Factory{
@Override
public Person createPersonAFactory(){
return new PersonA();
}
}
package com.boot.lijunhao.factory;
/**
* Created by lijunhao on 2017/8/25.
*/
public class WaixingrenFactory implements Factory {
@Override
public Person createPersonAFactory() {
return new PersonB();
}
}
package com.boot.lijunhao.factory;
/**
* Created by lijunhao on 2017/8/25.
*/
public class Start {
public static void main(String[] args) {
//虽然在这里new PersonFactory 但是客户端并不知道是实例化了具体的PersonA类
PersonFactory personFactory = new PersonFactory();
Person personAFactory1 = personFactory.createPersonAFactory();
personAFactory1.shaodi();
personAFactory1.speak();
//虽然在这里new PersonFactory 但是客户端并不知道是实例化了具体的PersonB类
WaixingrenFactory waixingrenFactory = new WaixingrenFactory();
Person personAFactory2 = waixingrenFactory.createPersonAFactory();
personAFactory2.shaodi();
personAFactory2.speak();
//把想要创建的东西交给工厂去new,下面调用的方法都是一样的,不需要改变了
//16:36:07.740 [main] INFO com.boot.lijunhao.factory.PersonA - shaodi在类(PersonA.java:18)行:A扫了地
//16:36:07.743 [main] INFO com.boot.lijunhao.factory.PersonA - speak在类(PersonA.java:13)行:A说了句话
//16:36:07.744 [main] INFO com.boot.lijunhao.factory.PersonB - shaodi在类(PersonA.java:18)行:B扫了地
//16:36:07.744 [main] INFO com.boot.lijunhao.factory.PersonB - speak在类(PersonA.java:13)行:B说了句话
}
}