设计模式

抽象工厂模式

2017-09-20  本文已影响4人  jfson_土豆

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

public interface MakeIce {
    void putIce();
}

public interface MakeJuice {
    void juicing();
}
// 水果类
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");
        }
    }
}
public abstract class FruitsFactorysAbstract {
    abstract MakeJuice getJuice(String want);
    abstract MakeIce getIce(String want);
}
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;
    }
}


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;
    }
}
 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());
上一篇下一篇

猜你喜欢

热点阅读