zoukankan      html  css  js  c++  java
  • 设计模式-工厂模式

    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();
        }
    }
    
    
    欢迎大家留言,以便于后面的人更快解决问题!另外亦欢迎大家可以关注我的微信公众号,方便利用零碎时间互相交流。共勉!

    ------愿来生只做陌上的看花人,无须入尘缘,仅行于陌上,看一川风花,无爱无伤-----
  • 相关阅读:
    ndk-build:command not found
    Linux shell 之 sed 命令详解 第三部分
    Linux shell 之 sed 命令详解 第二部分
    Linux shell 之 sed 概述
    Linux shell 之 sed 命令详解 第一部分
    git 如何撤销 commit (未 push)
    如何在 ubuntu 系统上安装 jdk
    Ubuntu 16.04.06(32位)安装samba--“”拒绝访问”
    Ubuntu 安装 vim、ssh、samba
    OC中block对于变量的捕获
  • 原文地址:https://www.cnblogs.com/caozz/p/factory.html
Copyright © 2011-2022 走看看