美文网首页
Android源码设计模式学习笔记-抽象工厂方法模式

Android源码设计模式学习笔记-抽象工厂方法模式

作者: e小e | 来源:发表于2018-01-07 22:15 被阅读1次

    从一个栗子开始,现在两种产品一个是普通轮胎,一个是越野轮胎,然后生产这两种产品,用工厂模式去实现是

    public interface ITire {
        void tire();
    }
    
    public class NormalTire implements ITire{
        @Override
        public void tire() {
            System.out.println("普通轮胎");
        }
    }
    
    public class SUVTire implements ITire{
        @Override
        public void tire() {
            System.out.println("越野轮胎");
        }
    }
    
    public class FactoryTire {
        public static <T extends ITire> T createTire(Class<T> clz){
            ITire tire = null;
            try {
                tire = (ITire) Class.forName(clz.getName()).newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return (T) tire;
        }
    }
    

    但是现在需求变了,我不生产轮胎要生产汽车了,那么汽车包含的零件就多了,绝不仅仅限于轮胎,而且汽车的款式也多,这么一个复杂的生产关系,我们就要用到抽象工厂设计模式了. 在这个时候抽象的工厂是汽车生产工厂,注意它不像简单工厂模式那样,仅仅局限于一种特定的产品去生产,所以我们这里还需要对工厂抽象。
    先来看一张抽象工厂的uml.


    image.png

    目前需求是生产Q7和Q3汽车,那么需要引擎和轮胎两种零件,uml可以看成是


    image.png
    实际代码
    public interface ITire {
        void tire();
    }
    
    public class NormalTire implements ITire{
        @Override
        public void tire() {
            System.out.println("普通轮胎");
        }
    }
    
    public class SUVTire implements ITire{
        @Override
        public void tire() {
            System.out.println("越野轮胎");
        }
    }
    
    public interface IEngine {
        void engine();
    }
    
    public class ImportEngine implements IEngine{
        @Override
        public void engine() {
            System.out.println("进口发动机");
        }
    }
    
    public class DemesticEngine implements IEngine{
        @Override
        public void engine() {
            System.out.println("国产发动机");
        }
    }
    
    public interface CarFactory {
        ITire createTire();
        IEngine createEngine();
    }
    
    public class Q3Factory implements CarFactory{
        @Override
        public ITire createTire() {
            return new NormalTire();
        }
    
        @Override
        public IEngine createEngine() {
            return new DemesticEngine();
        }
    }
    
    public class Q7Factory implements CarFactory{
        @Override
        public ITire createTire() {
            return new SUVTire();
        }
    
        @Override
        public IEngine createEngine() {
            return new ImportEngine();
        }
    }
    

    最后调用

            Q3Factory q3Factory = new Q3Factory();
            q3Factory.createEngine().engine();
            q3Factory.createTire().tire();
    
            Q7Factory q7Factory = new Q7Factory();
            q7Factory.createEngine().engine();
            q7Factory.createTire().tire();
    

    输出

    01-07 22:14:38.730 9413-9413/? I/System.out: 国产发动机
    01-07 22:14:38.731 9413-9413/? I/System.out: 普通轮胎
    01-07 22:14:38.732 9413-9413/? I/System.out: 进口发动机
    01-07 22:14:38.732 9413-9413/? I/System.out: 越野轮胎
    

    相关文章

      网友评论

          本文标题:Android源码设计模式学习笔记-抽象工厂方法模式

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