美文网首页设计模式
抽象工厂模式

抽象工厂模式

作者: jfson_土豆 | 来源:发表于2017-09-20 16:57 被阅读4次

    抽象工厂模式相对于工厂模式来说,最大的不同是:将工厂的共同特征抽取出来,提供一个生成工厂类。

    • 1.举例制作果汁,首先提供两个接口类,果汁的种类和加冰多少。
    public interface MakeIce {
        void putIce();
    }
    
    public interface MakeJuice {
        void juicing();
    }
    
    • 2.提供水果类和冰多少的类,这里方便起见,我们都写成内部类。
    // 水果类
    public class Fruits {
        static final String TAG = Fruits.class.getSimpleName();
    
         public class Apple implements MakeJuice {
    
            @Override
            public void juicing() {
                Log.d(TAG, "Apple juice");
            }
        }
    
         public class Mango implements MakeJuice {
    
            @Override
            public void juicing() {
                Log.d(TAG, "Mango juice");
            }
        }
    
         public class Orange implements MakeJuice {
    
            @Override
            public void juicing() {
                Log.d(TAG, "Orange juice");
            }
        }
    }
    
    // 冰类
    public class Ice {
        static final String TAG = Ice.class.getSimpleName();
    
        public class LittleIce implements MakeIce {
            @Override
            public void putIce() {
                Log.d(TAG, "little ice");
            }
        }
    
        public class MoreIce implements MakeIce {
            @Override
            public void putIce() {
                Log.d(TAG, "More ice");
            }
        }
    
        public class NoeIce implements MakeIce {
            @Override
            public void putIce() {
                Log.d(TAG, "No ice");
            }
        }
    }
    
    • 3.对于水果类,生成一个水果类工厂,获取一种水果对应一种果汁。对于冰类,生成一个冰类工厂。那么两个工厂可以提取公同的抽象方法出来。

    • 3.1 所有工厂的共同的抽象方法

    public abstract class FruitsFactorysAbstract {
        abstract MakeJuice getJuice(String want);
        abstract MakeIce getIce(String want);
    }
    
    • 3.2 果汁工厂
    public class FruitsAbstractFactory extends FruitsFactorysAbstract {
        @Override
        public MakeJuice getJuice(String want) {
            MakeJuice ret;
            switch (want) {
                case "apple":
                    ret = new Fruits().new Apple();
                    break;
                case "orange":
                    ret = new Fruits().new Orange();
                    break;
                case "mango":
                    ret = new Fruits().new Mango();
                    break;
                default:
                    ret = new Fruits().new Apple();
                    break;
            }
            return ret;
        }
    
        @Override
        public MakeIce getIce(String want) {
            return null;
        }
    }
    
    
    
    • 3.4 冰工厂
    public class IceFactoryAbstract extends FruitsFactorysAbstract {
        MakeIce ret;
    
        @Override
        public MakeJuice getJuice(String want) {
            return null;
        }
    
        @Override
        public MakeIce getIce(String want) {
            switch (want) {
                case "little":
                    ret = new Ice().new LittleIce();
                    break;
                case "more":
                    ret = new Ice().new MoreIce();
                    break;
                case "no":
                    ret = new Ice().new NoeIce();
                    break;
            }
            return ret;
        }
    }
    
    • 提供一个生成工厂类(抽象工厂模式跟工厂模式最大的区别)
    public class FactoryProducer {
        static FruitsFactorysAbstract ret;
    
        public static FruitsFactorysAbstract getFactory(String what) {
            switch (what) {
                case "Fruits":
                    ret = new FruitsFactory();
                    break;
                case "Ice":
                    ret = new IceFactory();
                    break;
                default:
                    break;
            }
            return ret;
        }
    }
    
    • 最后,获取到对应工厂后,调用相应get即可。
     FruitsFactorysAbstract fruits = FactoryProducer.getFactory("Fruits");
            MakeJuice orange = fruits.getJuice("orange");
            Log.d("orange",orange.toString());
    
            FruitsFactorysAbstract ice = FactoryProducer.getFactory("ice");
            MakeIce  makeIce= ice.getIce("orange");
            Log.d("orange",makeIce.toString());
    

    相关文章

      网友评论

        本文标题:抽象工厂模式

        本文链接:https://www.haomeiwen.com/subject/nzmosxtx.html