java设计模式---简单工厂和抽象工厂(实战demo及对比优化)

//此总结为个人原创,有任何问题欢迎指出并改正,谢谢支持

使用场景:计划在不同的场景下创建不同的产品;分为简单工厂和抽象工厂 

两种设计模式的区别:

       区别在于产品,如果产品单一,最合适用工厂模式,但是如果有多个业务品种、业务分类时,通过抽象工厂模式产生需要的对象是一种非常好的解决方式。再通俗深化理解下:工厂模式针对的是一个产品等级结构抽象工厂模式针对的是面向多个产品等级结构的。

简单工厂方法模式Demo

抽象产品接口

public interface Operation{

    public double getResult(double numberA,double numberB)throws Exception;

}

 

具体产品接口

public class Add implements Operation{   // 加法计算   
public double getResult(double numberA, double numberB){       return numberA + numberB;  }}
public class Sub implements Operation{   // 减法计算       public double getResult(double numberA, double numberB){               return  numberA-numberB;   }}
public class Mul implements Operation{// 乘法计算
    public double getResult(double numberA, double numberB){      
     return numberA * numberB;
    }
}
public class Div implements Operation{// 除法计算  
   public double getResult(double numberA, double numberB)throws Exception {       
    if(numberB == 0) {               throw  new  Exception("除数不能为0");    }           return  numberA / numberB; }} 

简单工厂类

public class EasyFactory // 简单工厂,根据字符串创建相应的对象    

publicstatic Operation createOperation(String name){
        Operation operationObj = null;
        switch(name) {            
            case"+":
                operationObj = new Add();
                break;
            case"-":
                operationObj = new Sub();
                break;
            case"*":
                operationObj = new Mul();
                break;
            case"/":
                operationObj = new Div();
                break;
        }
        return operationObj;
    }
}

客户端调用

public class Client{  
public static void main(String[] args) throws Exception {
  Operation add = EasyFactory.createOperation("+");
  Operation sub = EasyFactory.createOperation("-");
  Operation mul = EasyFactory.createOperation("*");
  Operation div = EasyFactory.createOperation("/");
  System.out.println(add.getResult(1, 1));
  System.out.println(sub.getResult(1, 1));
  System.out.println(mul.getResult(1, 1));
  System.out.println(div.getResult(1, 1));
    }
}

*****************

 可以对简单工厂进行改良

 

public class EasyFactory
private static Operation operationObj = null;    
private static Operation add(){
        System.out.println("加法运算");        return new Add();
}
    privatestatic Operation sub(){
        System.out.println("减法运算");                return new Sub();
    }
    private static Operation mul(){
         System.out.println("乘法运算");
        return new Mul();
}    
private static Operation div(){
        System.out.println("除法运算");        return new Div();}    
// 简单工厂,根据字符串创建相应的对象   
publicstatic Operation createOperation(String name){      
switch (name) {
            case"+":
                operationObj = add();
                break;
            case"-":
                operationObj = sub();
                break;
            case"*":
                operationObj = mul();
                break;
            case"/":
                operationObj = div();
                break;
        }
        return operationObj;
    }
}

抽象工厂方法模式Demo

 

产品类不变,只抽象工厂类

 

抽象工厂类

 

public interface Factory{    
public Operation createOperation();
}

 

具体工厂类

 

// 加法类工厂
public class AddFactory implements Factory{   
public Operation createOperation(){
        System.out.println("加法运算");
        return new Add();
    }
}

 

// 减法类工厂
public class SubFactory implements Factory{   public Operation createOperation(){        System.out.println("减法运算");        return new Sub();
  }}

   // 乘法类工厂

public class MulFactory implements Factory{
 public Operation createOperation(){
        System.out.println("乘法运算");               return new Mul();
    }
}
// 除法类工厂
public class DivFactory implements Factory 
public Operation createOperation(){
        System.out.println("除法运算");        
         return new Div();
    }
}

 

 

客户端代码:

 

public class Client{  
public static void main(String[] args)throws Exception {       
// 使用反射机制实例化工厂对象,因为字符串是可以通过变量改变的
        Factory addFactory = (Factory) Class.forName("org.zero01.factory.AddFactory").newInstance();
        Factory subFactory=(Factory) Class.forName("org.zero01.factory.SubFactory").newInstance();
        Factory mulFactory=(Factory) Class.forName("org.zero01.factory.MulFactory").newInstance();
        Factory divFactory=(Factory) Class.forName("org.zero01.factory.DivFactory").newInstance();       
// 通过工厂对象创建相应的实例对象
        Operation add = addFactory.createOperation();
        Operation sub = subFactory.createOperation();
        Operation mul = mulFactory.createOperation();
        Operation div = divFactory.createOperation();
 
        System.out.println(add.getResult(1, 1));
        System.out.println(sub.getResult(1, 1));
        System.out.println(mul.getResult(1, 1));
        System.out.println(div.getResult(1, 1));
    }
}

*******************************

 抽象工厂类可以改良(代码中常用)如下:

public abstract class Factroy{
    public abstract<T extends Operation > T create(Class<T> clz);
}
public class BaseFactory extends Factroy{
  @override
    public <T extends Operation > T create(Class<T> clz){
      Operation shape=null;
      try{

          shape=(Operation)Class.forName(clz.getName()).newInstance();
      } catch(Exception e){
          e.printStackTrace();
      }
      return (T)shape;
  }
}

客户端调用

public class Client{    
public static void main(String[] args)throws Exception {
        Factory factory=new BaseFactory();
        Factory addFactory =factory.create(AddFactory.class);
        Factory subFactory =factory.create(SubFactory.class);
        Factory mulFactory =factory.create(MulFactory.class);
        Factory divFactory =factory.create(DivFactory.class);        
         // 通过工厂对象创建相应的实例对象
        Operation add = addFactory.createOperation();
        Operation sub = subFactory.createOperation();
        Operation mul = mulFactory.createOperation();
        Operation div = divFactory.createOperation();
 
        System.out.println(add.getResult(1, 1));
        System.out.println(sub.getResult(1, 1));
        System.out.println(mul.getResult(1, 1));
        System.out.println(div.getResult(1, 1));
    }
}

 



阅读更多

没有更多推荐了,返回首页