《工厂模式》

工厂模式

    • 1.简单工厂模式
    • 2.工厂模式
    • 3.抽象工厂模式
    • 4.工厂模式和抽象工厂模式的区别?
    • 5.Shiro源码中的工厂模式

1.简单工厂模式

  • 角色组成
    • 工厂类:根据逻辑不同,产生具体的工厂产品。
    • 抽象产品:一般是具体产品继承的父类或者实现的接口。
    • 具体产品: 工厂类所创建的对象。

《工厂模式》_第1张图片

  • 1.运算接口
public interface Operation {
    public double compute(double numberA,double numberB);
}
  • 2.加法运算
public class AddOperation implements Operation {
    @Override
    public double compute(double numberA, double numberB) {
        return numberA + numberB;
    }
}
  • 3.减法运算
public class SubOperation implements Operation {
    @Override
    public double compute(double numberA, double numberB) {
        return numberA - numberB;
    }
}
  • 4.乘法运算
public class MultiplyOperation implements Operation{
    @Override
    public double compute(double numberA, double numberB) {
        return numberA * numberB;
    }
}
  • 5.除法运算
public class DivideOperation implements Operation {
    @Override
    public double compute(double numberA, double numberB) {
        if (numberB==0)
            throw new IllegalArgumentException("numberB must not be 0");
        return numberA / numberB;
    }
}
  • 6.运算枚举:使用枚举来标识运算的类型
public enum OperationEnum {
    ADD,
    SUB,
    MULTIPLY,
    DIVIDE;
    private OperationEnum(){}
}
  • 7.简单运算工厂
public class SimpleOperationFactory {

    public static Operation createOperation(OperationEnum type){
        Assert.notNull(type,"type must not be null");
        Operation operation = null;
        switch (type)
        {
            case ADD:
                operation = new AddOperation();
                //可追加定制对象
                break;
            case SUB:
                operation = new SubOperation();
                break;
            case MULTIPLY:
                operation = new MultiplyOperation();
                break;
            case DIVIDE:
                operation = new DivideOperation();
                break;
        }
        return operation;
    }
}
  • 8.简单工厂测试
public class SimpleFactoryTest {
    public static void main(String[] args) {
        Operation operation = SimpleOperationFactory.createOperation(OperationEnum.ADD);
        double compute = operation.compute(1, 22);
        System.out.println("compute = " + compute);

        Operation o2 = SimpleOperationFactory.createOperation(OperationEnum.SUB);
        double compute1 = o2.compute(55, 12);
        System.out.println("compute1 = " + compute1);

        Operation o3 = SimpleOperationFactory.createOperation(OperationEnum.MULTIPLY);
        double compute2 = o3.compute(12, 3);
        System.out.println("compute2 = " + compute2);

        Operation o4 = SimpleOperationFactory.createOperation(OperationEnum.DIVIDE);
        double compute3 = o4.compute(25, 0);
        System.out.println("compute3 = " + compute3);

    }
}

2.工厂模式

  • 角色组成
    • 抽象工厂:是具体工厂角色必须实现的接口或者必须继承的父类。
    • 具体工厂:每个具体工厂只负责一个具体产品。
    • 抽象产品:它是具体产品继承的父类或者是实现的接口。
    • 具体产品:具体工厂角色所创建的对象就是此角色的实例。
      《工厂模式》_第2张图片
//抽象产品
public interface Mouse{
   void sayHi();
}
//具体产品
public class HpMouse implements Mouse{

    @Override
    public void sayHi() {
        System.out.println("HpMouse...");
    }
}
//具体产品
public class DellMouse implements Mouse{

    @Override
    public void sayHi() {
        System.out.println("DellMouse...");
    }
}
//抽象工厂
public interface MouseFactory{
  Mouse createMouse();
}
//具体工厂
public class HpMouseFactory implements MouseFactory{

    @Override
    public Mouse createMouse() {
        return new HpMouse();
    }
}
//具体工厂
public class DellMouseFactory implements MouseFactory{

    @Override
    public Mouse createMouse() {
        return new DellMouse();
    }
}

3.抽象工厂模式

  • 角色组成
    • 抽象工厂:是具体工厂角色必须实现的接口或者必须继承的父类。
    • 具体工厂:每个具体工厂负责一个系列产品的生产(产品族)。
    • 抽象产品:它是具体产品继承的父类或者是实现的接口,抽象工厂模式有多个抽象产品。
    • 具体产品:具体工厂角色所创建的对象就是此角色的实例。

《工厂模式》_第3张图片

//抽象产品A
public interface BenzCar{
    void makeBenzCar();
}
//抽象产品B
public interface BmwCar{
    void makeBmwCar();
}
//抽象产品A的具体产品
public class BenzSportCar implements BenzCar{

    @Override
    public void makeBenzCar() {
        System.out.println(" make BenzSportCar...");
    }
}
//抽象产品A的具体产品
public class BenzBusinessCar implements BenzCar{

    @Override
    public void makeBenzCar() {
        System.out.println(" make BenzBusinessCar...");
    }
}
//抽象产品B的具体产品
public class BmwSportCar implements BmwCar{

    @Override
    public void makeBmwCar() {
        System.out.println(" make BmwSportCar...");
    }
}
//抽象产品B的具体产品
public class BmwBusinessCar implements BmwCar{

    @Override
    public void makeBmwCar() {
        System.out.println(" make BmwBusinessCar...");
    }
}

//抽象工厂
public abstract class CarFactory{
    public abstract  BenzCar createBenzCar();

    public abstract BmwCar createBmwCar();

}

//生产跑车系汽车的具体工厂
public class SportCarFactory extends CarFactory{
    public BenzCar createBenzCar(){
        return new BenzSportCar();
    }
    public BmwCar createBmwCar(){
        return new BmwSportCar();
    }
}

//生产商务系汽车的具体工厂
public class BusinessCarFactory extends CarFactory{
    public BenzCar createBenzCar(){
        return new BenzBusinessCar();
    }
    public BmwCar createBmwCar(){
        return new BmwBusinessCar();
    }
}

//抽象工厂测试类
public class Test{
	public static void main(String[] args) {
	        
	    SportCarFactory sportCarFactory = new SportCarFactory();
	    BenzCar benzCar = sportCarFactory.createBenzCar();
	    benzCar.makeBenzCar();
	    BmwCar bmwCar = sportCarFactory.createBmwCar();
	    bmwCar.makeBmwCar();
	
	    BusinessCarFactory businessCarFactory = new BusinessCarFactory();
	    BenzCar benzCar2 = businessCarFactory.createBenzCar();
	    benzCar2.makeBenzCar();
	    BmwCar bmwCar2 = businessCarFactory.createBmwCar();
	    bmwCar2.makeBmwCar();
	  }
}

控制台输出结果:
			 make BenzSportCar...
			 make BmwSportCar...
			 
			 make BenzBusinessCar...
			 make BmwBusinessCar...

4.工厂模式和抽象工厂模式的区别?

  • ① 抽象产品:工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。
  • ② 具体工厂类能创建的具体产品类型:工厂方法模式的具体工厂类只能创建一种具体产品类的实例, 而抽象工厂模式可以创建多种具体产品类的实例。

5.Shiro源码中的工厂模式

《工厂模式》_第4张图片

  • 抽象工厂
public interface SubjectFactory {

    Subject createSubject(SubjectContext context);

}
  • 具体工厂DefaultSubjectFactory
public class DefaultSubjectFactory implements SubjectFactory {

    public DefaultSubjectFactory() {
    }

    public Subject createSubject(SubjectContext context) {
        SecurityManager securityManager = context.resolveSecurityManager();
        Session session = context.resolveSession();
        boolean sessionCreationEnabled = context.isSessionCreationEnabled();
        PrincipalCollection principals = context.resolvePrincipals();
        boolean authenticated = context.resolveAuthenticated();
        String host = context.resolveHost();

        return new DelegatingSubject(principals, authenticated, host, session, sessionCreationEnabled, securityManager);
    }
}
  • 具体工厂DefaultWebSubjectFactory,扩展了DefaultSubjectFactory
public class DefaultWebSubjectFactory extends DefaultSubjectFactory {
    public DefaultWebSubjectFactory() {
    }

    public Subject createSubject(SubjectContext context) {
        if (!(context instanceof WebSubjectContext)) {
            return super.createSubject(context);
        } else {
            WebSubjectContext wsc = (WebSubjectContext)context;
            SecurityManager securityManager = wsc.resolveSecurityManager();
            Session session = wsc.resolveSession();
            boolean sessionEnabled = wsc.isSessionCreationEnabled();
            PrincipalCollection principals = wsc.resolvePrincipals();
            boolean authenticated = wsc.resolveAuthenticated();
            String host = wsc.resolveHost();
            ServletRequest request = wsc.resolveServletRequest();
            ServletResponse response = wsc.resolveServletResponse();
            return new WebDelegatingSubject(principals, authenticated, host, session, sessionEnabled, request, response, securityManager);
        }
    }
}
  • 抽象产品Subject
public interface Subject {
    Object getPrincipal();

    PrincipalCollection getPrincipals();

    boolean isPermitted(String var1);
    //......  
}
  • 具体产品DelegatingSubject和WebDelegatingSubject

《工厂模式》_第5张图片

你可能感兴趣的:(JAVA设计模式)