第三讲:抽象工厂模式

Posted 绿茵好莱坞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三讲:抽象工厂模式相关的知识,希望对你有一定的参考价值。

如果AppleFactory里面包含两个方法一个方法生产北方的苹果一个方法生产南方的苹果,那么如果多了一个生产温室的苹果那就要修改AppleFactory的源码了,不符合开放封闭原则.所以那就可以用一个具体工厂负责生产一个产品族(比如南方的产品族).

抽象工厂模式非常符合OCP原则/开放封闭原则.从产品族这个角度看完全符合OCP原则,但是从产品等级这个角度看就不符合OCP原则了,因为如果增加一个产品等级(比如说梨子),从FruitFactory开始就要修改源码.

 


抽象工厂角色FruitFactory包含对多个产品结构(Apple、Banana)的声明

具体工厂角色:NorthFruitFactory、SouthFruitFactory、WenshiFruitFactory

抽象角色:Apple、Banana、Fruit

具体产品角色:SouthApple、SouthBanana、WenshiApple、NorthApple、NorthBanana、WenshiBanana


 

抽象工厂模式的抽象工厂负责定义产品结构,具体的工厂类负责一个产品族.


//public class Apple implements Fruit{
public abstract class Apple implements Fruit{
   /*
    * 采集
    * 
    */
/*    public void get(){
        System.out.println("采集苹果");    
    }*/
    public abstract void get();
}
//public class Banana implements Fruit{
public abstract class Banana implements Fruit{
    /*
     * 采集
     * 
     */
    //public void get(){
/*    public void get(){
        System.out.println("采集香蕉");
        
    }*/
    public abstract void get();
}
public interface Fruit {
   /*
    * 采集
    */
    public void get();
}
/*public class FruitFactory {
     
    public static  Fruit getFruit(String type) throws InstantiationException, IllegalAccessException{        
        if(type.equalsIgnoreCase("apple")){
            return Apple.class.newInstance();
            
        }else if(type.equalsIgnoreCase("banana")){
        return Banana.class.newInstance();
        }else if(type.equalsIgnoreCase("pear")){
            return Pear.class.newInstance();
        }else{
            System.out.println("找不到相应的实例化类");
            return null;
        }

}
}*/
public interface FruitFactory{
    //public Fruit getFruit();
    //实例化Apple
    public Fruit getApple();
    //实例化Banana
    public Fruit getBanana();
    
}
public class NorthApple extends Apple{

    @Override
    public void get() {
        // TODO Auto-generated method stub
        System.out.println("采集北方苹果");
    }

}
public class NorthBanana extends Banana{

    @Override
    public void get() {
        // TODO Auto-generated method stub
        System.out.println("采集北方香蕉");
    }

}
public class NorthFruitFactory implements FruitFactory {

    @Override
    public Fruit getApple() {
        // TODO Auto-generated method stub
        return new NorthApple();
    }

    @Override
    public Fruit getBanana() {
        // TODO Auto-generated method stub
        return new NorthBanana();
    }

}
public class SouthApple extends Apple{

    @Override
    public void get() {
        // TODO Auto-generated method stub
        System.out.println("采集南方苹果");
    }

}
public class SouthBanana extends Banana{

    @Override
    public void get() {
        // TODO Auto-generated method stub
        System.out.println("采集南方香蕉");
    }

}
public class SouthFruitFactory implements FruitFactory {

    @Override
    public Fruit getApple() {
        // TODO Auto-generated method stub
        return new SouthApple();
    }

    @Override
    public Fruit getBanana() {
        // TODO Auto-generated method stub
        return new SouthBanana();
    }

}
public class WenshiApple extends Apple{

    @Override
    public void get() {
        // TODO Auto-generated method stub
        System.out.println("采集温室苹果");
    }

}
public class WenshiBanana extends Banana {

    @Override
    public void get() {
        // TODO Auto-generated method stub
        System.out.println("采集温室香蕉");
    }

}
public class WenshiFruitFactory implements FruitFactory{

    @Override
    public Fruit getApple() {
        // TODO Auto-generated method stub
        return  new WenshiApple();
    }

    @Override
    public Fruit getBanana() {
        // TODO Auto-generated method stub
        return new WenshiBanana();
    }

}
public class MainClass {
     public static void main(String[] args) {
        FruitFactory ff = new NorthFruitFactory();
        Fruit apple = ff.getApple();
        apple.get();
        Fruit banana = ff.getBanana();
        banana.get();
        FruitFactory ff2 = new SouthFruitFactory();
        Fruit apple2 = ff2.getApple();
        apple2.get();
        Fruit banana2 = ff2.getBanana();
        banana2.get();
        FruitFactory ff3 = new WenshiFruitFactory();
        Fruit apple3 = ff3.getApple();
        apple3.get();
        Fruit banana3 = ff3.getBanana();
        banana3.get();
    }
}  

 

以上是关于第三讲:抽象工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式第三谈:抽象工厂模式

设计模式实践笔记第二节:抽象工厂模式

设计模式实践笔记第二节:抽象工厂模式

设计模式实践笔记第二节:抽象工厂模式

《设计模式奇妙之旅》第3章 抽象工厂模式

C++设计模式:抽象工厂模式