首页 > 其他 > 详细

设计模式 值 抽象工厂模式

时间:2021-05-27 16:38:40      阅读:25      评论:0      收藏:0      [点我收藏+]

简介

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

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("小米设置");
    }
}

设计模式 值 抽象工厂模式

原文:https://www.cnblogs.com/eat-too-much/p/14817451.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!