• 设计模式 值 抽象工厂模式


    简介

    围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.

    UML

    code

    public class Clinet {
        public static void main(String[] args) {
            System.out.println("==================");
            XiaomiFactory xiaomiFactory = new XiaomiFactory();
    
            IphoneProduct iphoneProduct = xiaomiFactory.iphoneProduct();
            iphoneProduct.callup();
            iphoneProduct.sendSMS();
    
            IRouterProduct iRouterProduct = xiaomiFactory.routerProduct();
            iRouterProduct.openWife();
    
        }
    }
    
    
    public class HuaweiFactory implements IProductFactory {
        @Override
        public IphoneProduct iphoneProduct() {
            return new HuaweiPhone();
        }
    
        @Override
        public IRouterProduct routerProduct() {
            return new HuaweiRouter();
        }
    }
    
    
    public class HuaweiPhone implements IphoneProduct {
        @Override
        public void start() {
            System.out.println("开启华为手机");
        }
    
        @Override
        public void shutdown() {
            System.out.println("关闭华为手机");
        }
    
        @Override
        public void callup() {
            System.out.println("华为打电话");
        }
    
        @Override
        public void sendSMS() {
            System.out.println("华为发短信");
        }
    }
    
    
    public class HuaweiRouter implements IRouterProduct {
        @Override
        public void start() {
            System.out.println("启动华为路由器");
        }
    
        @Override
        public void shutdown() {
            System.out.println("关闭华为路由器");
        }
    
        @Override
        public void openWife() {
            System.out.println("打开华为wifi");
    }
    
        @Override
        public void seeting() {
            System.out.println("华为设置");
        }
    }
    
    
    public interface IphoneProduct {
        void start();
        void shutdown();
        void callup();
        void sendSMS();
    }
    
    
    public interface IProductFactory {
        IphoneProduct iphoneProduct();
    
        IRouterProduct routerProduct();
    }
    
    
    public interface IRouterProduct {
        void start();
        void shutdown();
        void openWife();
        void seeting();
    }
    
    
    public class XiaomiFactory implements IProductFactory {
        @Override
        public IphoneProduct iphoneProduct() {
            return new XiaomiPhone();
        }
    
        @Override
        public IRouterProduct routerProduct() {
            return new XiaomiRouter();
        }
    }
    
    
    public class XiaomiPhone implements IphoneProduct {
        @Override
        public void start() {
            System.out.println("开启小米手机");
        }
    
        @Override
        public void shutdown() {
            System.out.println("关闭小米手机");
        }
    
        @Override
        public void callup() {
            System.out.println("用小米手机打电话");
        }
    
        @Override
        public void sendSMS() {
            System.out.println("小米手机发短信");
        }
    }
    
    public class XiaomiRouter implements IRouterProduct {
        @Override
        public void start() {
            System.out.println("启动小米路由器");
        }
    
        @Override
        public void shutdown() {
            System.out.println("关闭小米路由器");
        }
    
        @Override
        public void openWife() {
            System.out.println("打开小米wifi");
        }
    
        @Override
        public void seeting() {
            System.out.println("小米设置");
        }
    }
    
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    MongDB简单介绍
    Docker的简单介绍
    maven简单介绍
    粗谈Springboot框架,众所周知Springboot是有spring推出的微服务框架,什么是微服务框架呢!
    Springboot打包问题,打包的话是通过
    SpringBoot注解及swagger注解使用及规范
    properties配置
    日志配置
    c++几个面试题
    c++四种强制类型转化的区别
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14817451.html
Copyright © 2020-2023  润新知