美文网首页
设计模式-工厂模式

设计模式-工厂模式

作者: 东方欲晓_莫道君行早 | 来源:发表于2021-06-09 13:17 被阅读0次

    1、简单工厂模式

    package com.jdwa.factory.simple;
    
    public interface IProduct {
        void use();
    }
    
    
    package com.jdwa.factory.simple;
    
    public class Phone implements IProduct {
        @Override
        public void use() {
            System.out.println("通话");
        }
    }
    
    
    package com.jdwa.factory.simple;
    
    public class Fridge implements IProduct {
        @Override
        public void use() {
            System.out.println("保鲜食物");
        }
    }
    
    package com.jdwa.factory.simple;
    
    public class ProductFactory {
        public IProduct create(Class<? extends IProduct> clazz){
            if (clazz != null) {
                try {
                    return clazz.newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }
    
    
    package com.jdwa.factory.simple;
    
    public class Client {
        public static void main(String[] args) {
            ProductFactory factory = new ProductFactory();
            IProduct fridge = factory.create(Fridge.class);
            fridge.use();
        }
    }
    
    

    2、工厂方法模式

    package com.jdwa.factory.method;
    
    public interface IProduct {
        void use();
    }
    
    
    package com.jdwa.factory.method;
    
    public class Fridge implements IProduct {
        @Override
        public void use() {
            System.out.println("工厂方法-保鲜食物");
        }
    }
    
    
    package com.jdwa.factory.method;
    
    public class Phone implements IProduct {
        @Override
        public void use() {
            System.out.println("工厂方法-通话");
        }
    }
    
    
    package com.jdwa.factory.method;
    
    public interface IProductFactory {
        IProduct create();
    }
    
    
    package com.jdwa.factory.method;
    
    public class FridgeFactory implements IProductFactory {
        @Override
        public IProduct create() {
            return new Fridge();
        }
    }
    
    
    package com.jdwa.factory.method;
    
    public class PhoneFactory implements IProductFactory {
        @Override
        public IProduct create() {
            return new Phone();
        }
    }
    
    
    package com.jdwa.factory.method;
    
    public class Client {
        public static void main(String[] args) {
            IProductFactory factory = new PhoneFactory();
            IProduct ph = factory.create();
            ph.use();
    
            factory = new FridgeFactory();
            IProduct fridge = factory.create();
            fridge.use();
    
        }
    }
    
    

    3、抽象工厂模式

    package com.jdwa.factory.abstractfactory;
    
    public interface IComputer {
        void code();
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public interface IPhone {
        void call();
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public class AppleComputer implements IComputer{
        @Override
        public void code() {
            System.out.println("use apple computer to code ==");
        }
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public class HuaWeiComputer implements IComputer {
        @Override
        public void code() {
            System.out.println("use huawei computer to code===");
        }
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public class ApplePhone implements IPhone {
        @Override
        public void call() {
            System.out.println("use apple phone to call someone ===");
        }
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public class HuaWeiPhone implements IPhone{
        @Override
        public void call() {
            System.out.println("use huawei  phone to call someone ===");
        }
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public abstract class ProductFactory {
        public void init(){
            System.out.println("init============");
        }
    
        protected abstract IComputer createCom();
    
        protected abstract IPhone createPh();
    
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public class AppleFactory extends ProductFactory {
        @Override
        protected IComputer createCom() {
            return new AppleComputer();
        }
    
        @Override
        protected IPhone createPh() {
            return new ApplePhone();
        }
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public class HuaWeiFactory extends ProductFactory {
        @Override
        protected IComputer createCom() {
            return new HuaWeiComputer();
        }
    
        @Override
        protected IPhone createPh() {
            return new HuaWeiPhone();
        }
    }
    
    
    package com.jdwa.factory.abstractfactory;
    
    public class Client {
        public static void main(String[] args) {
            HuaWeiFactory huaWeiFactory = new HuaWeiFactory();
            huaWeiFactory.createCom().code();
            huaWeiFactory.createPh().call();
    
            AppleFactory appleFactory = new AppleFactory();
            appleFactory.createCom().code();
            appleFactory.createPh().call();
        }
    }
    
    

    相关文章

      网友评论

          本文标题:设计模式-工厂模式

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