zoukankan      html  css  js  c++  java
  • JAVA设计模式之工厂模式

    一、问题引入:

    在面向对象编程中,最通常的方法是new一个实例化对象,但在一些情况下,这时模式在不需要了解细节过程的前提下,就能解决构造对象实例的问题。

    工厂模式概述(以宝马车为例):

    1.还没有工厂时代:如果一个客户要一款宝马车,一般的做法是客户自己创建一款宝马车,然后拿来用

    2.简单工厂模式:客户不需要自己创建宝马车,有一个工厂会帮他创建。客户想要什么系列的车,工厂就创建这个系列的车,即工厂可以创建产品。

    3.工厂方法模式:为了满足客户的需求,宝马车的系列越来越多,(工厂抽象的)于是分出来很多工厂,每个工厂具体生产一个系列的车,即一个工厂类只能创建一个具体的产品。需要哪个系列的车就指定哪个工厂生产这个系列的车。

    4.抽象工厂模式:例:宝马320系列配置A型号空调和A型号发动机,宝马230系列配置B型号空调和B型号发动机。

     

    最终客户只要对宝马车的工厂提出需求即可,而不需要自己创建宝马车。

    二、分类

    工厂模式可分为三类:

    简单工厂模式

    工厂方法模式:

    一个抽象产品类,可以派生出多个具体产品类。

    一个抽象工厂类,可以派生出多个具体工厂类。

    每个具体工厂类只能创建一个具体产品类的实例。

    抽象工厂模式:

    多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。

    一个抽象工厂类,可以派生出多个具体工厂类。

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

    三、区别(工厂方法模式和抽象工厂模式):

    工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。

    工厂方法模式的具体工厂类只能创建一个具体的产品类的实例,而抽象工厂模式可以创建多个

    四、优点:可不修改源代码,进行扩展

    五、代码实现:

    无工厂时代:客户自己创建宝马车

    public class BMW320 {
    	public BMW320(){
    		System.out.println("制造-->BMW320");
    	}
    }
    
    public class BMW523 {
    	public BMW523(){
    		System.out.println("制造-->BMW523");
    	}
    }
    
    public class Customer {
    	public static void main(String[] args) {
    		BMW320 bmw320 = new BMW320();
    		BMW523 bmw523 = new BMW523();
    	}
    }

    简单工厂模式:一个抽象产品类,有多个具体的具体产品类;只有一个具体的工厂类,一个工厂类可以创建多个具体的产品类
    abstract class BMW {
    	public BMW(){
    		
    	}
    }
    
    public class BMW320 extends BMW {
    	public BMW320() {
    		System.out.println("制造-->BMW320");
    	}
    }
    public class BMW523 extends BMW{
    	public BMW523(){
    		System.out.println("制造-->BMW523");
    	}
    }
    

    工厂类:

    public class Factory {
    	public BMW createBMW(int type) {
    		switch (type) {
    		
    		case 320:
    			return new BMW320();
    
    		case 523:
    			return new BMW523();
    
    		default:
    			break;
    		}
    		return null;
    	}
    }


    客户类:

    public class Customer {
    	public static void main(String[] args) {
    		Factory factory = new Factory();
    		BMW bmw320 = factory.createBMW(320);
    		BMW bmw523 = factory.createBMW(523);
    	}
    }

    工厂方法模式:一个抽象工厂类,有多个具体工厂类;一个抽象产品类,有多个具体产品类;每个具体工厂类类只能创建一种具体产品
    产品类:
     

    产品类:

    
    

     

    1. abstract class BMW {  
    2.     public BMW(){  
    3.           
    4.     }  
    5. }  
    6. public class BMW320 extends BMW {  
    7.     public BMW320() {  
    8.         System.out.println("制造-->BMW320");  
    9.     }  
    10. }  
    11. public class BMW523 extends BMW{  
    12.     public BMW523(){  
    13.         System.out.println("制造-->BMW523");  
    14.     }  
    15. }  
    
    


    创建工厂类:

    
    
     
    1. interface FactoryBMW {  
    2.     BMW createBMW();  
    3. }  
    4.   
    5. public class FactoryBMW320 implements FactoryBMW{  
    6.   
    7.     @Override  
    8.     public BMW320 createBMW() {  
    9.   
    10.         return new BMW320();  
    11.     }  
    12.   
    13. }  
    14. public class FactoryBMW523 implements FactoryBMW {  
    15.     @Override  
    16.     public BMW523 createBMW() {  
    17.   
    18.         return new BMW523();  
    19.     }  
    20. }  
    
    


    客户类:

    
    


    1. public class Customer {  
    2.     public static void main(String[] args) {  
    3.         FactoryBMW320 factoryBMW320 = new FactoryBMW320();  
    4.         BMW320 bmw320 = factoryBMW320.createBMW();  
    5.   
    6.         FactoryBMW523 factoryBMW523 = new FactoryBMW523();  
    7.         BMW523 bmw523 = factoryBMW523.createBMW();  
    8.     }  
    9. }  
    
    

    抽象工厂模式:多个抽象产品类,每个抽象产品类可以派生出多个具体产品类;一个抽象工厂类,每个抽象工厂类可以派生出多个具体工厂类,每个具体工厂类可以创建多个产品类。

    产品类: 


     

    1. //发动机以及型号    
    2. public interface Engine {    
    3.   
    4. }    
    5. public class EngineA extends Engine{    
    6.     public EngineA(){    
    7.         System.out.println("制造-->EngineA");    
    8.     }    
    9. }    
    10. public class EngineBextends Engine{    
    11.     public EngineB(){    
    12.         System.out.println("制造-->EngineB");    
    13.     }    
    14. }    
    15.   
    16. //空调以及型号    
    17. public interface Aircondition {    
    18.   
    19. }    
    20. public class AirconditionA extends Aircondition{    
    21.     public AirconditionA(){    
    22.         System.out.println("制造-->AirconditionA");    
    23.     }    
    24. }    
    25. public class AirconditionB extends Aircondition{    
    26.     public AirconditionB(){    
    27.         System.out.println("制造-->AirconditionB");    
    28.     }    
    29. }   


    创建工厂类:



     
    1. //创建工厂的接口    
    2. public interface AbstractFactory {    
    3.     //制造发动机  
    4.     public Engine createEngine();  
    5.     //制造空调   
    6.     public Aircondition createAircondition();   
    7. }    
    8.   
    9.   
    10. //为宝马320系列生产配件    
    11. public class FactoryBMW320 implements AbstractFactory{    
    12.         
    13.     @Override    
    14.     public Engine createEngine() {      
    15.         return new EngineA();    
    16.     }    
    17.     @Override    
    18.     public Aircondition createAircondition() {    
    19.         return new AirconditionA();    
    20.     }    
    21. }    
    22. //宝马523系列  
    23. public class FactoryBMW523 implements AbstractFactory {    
    24.     
    25.      @Override    
    26.     public Engine createEngine() {      
    27.         return new EngineB();    
    28.     }    
    29.     @Override    
    30.     public Aircondition createAircondition() {    
    31.         return new AirconditionB();    
    32.     }    
    33.   
    34.   
    35. }   

    客户:



     
    1. public class Customer {    
    2.     public static void main(String[] args){    
    3.         //生产宝马320系列配件  
    4.         FactoryBMW320 factoryBMW320 = new FactoryBMW320();    
    5.         factoryBMW320.createEngine();  
    6.         factoryBMW320.createAircondition();  
    7.             
    8.         //生产宝马523系列配件    
    9.         FactoryBMW523 factoryBMW523 = new FactoryBMW523();    
    10.         factoryBMW320.createEngine();  
    11.         factoryBMW320.createAircondition();  
    12.     }    
    13. }  
     



    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
     
  • 相关阅读:
    qt的.pro配置总结
    【GOJ 1489】Monster Hunter
    CPU 杂谈
    【CF 1061C|GOJ 3505】Multiplicity
    【CF 1039D|GOJ 3502】You Are Given a Tree
    我跳过的坑
    【CF 1101D|GOJ 3501】GCD Counting
    【HDU 5269|GOJ 739】xor的最低位
    beta阶段组间的140字互评
    【第七周】【新蜂站会】3
  • 原文地址:https://www.cnblogs.com/cn-chy-com/p/7474323.html
Copyright © 2011-2022 走看看