• 【设计模式】4.工厂模式


    工厂模式

    1)简单工厂模式

    结构图:

    public interface Phone {   // AbstractProduct
        void playgames();
    }
    public class HWPhone implements Phone{  // Product1
        @Override
        public void playgames() {
            System.out.println("HW手机玩游戏");
        }
    }
    public class MIPhone implements Phone {  // Product2
        @Override
        public void playgames() {
            System.out.println("MI手机玩游戏");
        }
    }
    public class SimplePhoneFactory {    // SimpleFactory 
        public Phone createHWPhone(){  // 生产HW手机
            return new HWPhone();
        }
    
        public Phone createMIPhone(){   // 生产MI手机
            return new MIPhone();
        }
    }
    public class Test {
        public static void main(String[] args) {
            SimplePhoneFactory simpleFactory = new SimplePhoneFactory();
            Phone hwphone = simpleFactory.createHWPhone();
            hwphone.playgames();
            Phone miphone = simpleFactory.createMIPhone();
            miphone.playgames();
        }
    }

    输出:

    HW手机玩游戏
    MI手机玩游戏

    2)工厂模式

      简单工厂中,一个工厂生产不同品牌的手机,可以对工厂进行抽象,分成各自的工厂。

    结构图:

    public interface Phone {  // AbstractProduct
        void playgames();
    }
    public class HWPhone implements Phone{  // Product1
        @Override
        public void playgames() {
            System.out.println("HW手机玩游戏");
        }
    }
    public class MIPhone implements Phone {  // Product2
        @Override
        public void playgames() {
            System.out.println("MI手机玩游戏");
        }
    }
    public interface PhoneFactory {  // AbstractFactory
        Phone produce();
    }
    public class HWFactory implements PhoneFactory {  // Factory1
        @Override
        public Phone produce() {
            return new HWPhone();
        }
    }
    public class MIFactory implements PhoneFactory {  // Factory2
        @Override
        public Phone produce() {
            return new MIPhone();
        }
    }
    public class Test {
        public static void main(String[] args) {
            PhoneFactory hwphoneFactory = new HWFactory();
            HWPhone hwPhone = (HWPhone) hwphoneFactory.produce();
            hwPhone.playgames();
    
            PhoneFactory miphoneFactory = new MIFactory();
            MIPhone miPhone = (MIPhone)miphoneFactory.produce();
            miPhone.playgames();
        }
    }

    输出:

    HW手机玩游戏
    MI手机玩游戏

    3)抽象工厂

      工厂模式只能生产一种产品,抽象工厂模式提供了一种方式,可以将同一品牌各种产品单独的工厂封装起来

    结构图:

    public interface Phone {  // AbstractProductA
        void playgames();
    }
    public class HWPhone implements Phone{  // ProductA1
        @Override
        public void playgames() {
            System.out.println("HW手机玩游戏");
        }
    }
    public class MIPhone implements Phone {  // ProductA2
        @Override
        public void playgames() {
            System.out.println("MI手机玩游戏");
        }
    }
    public interface Charger {   // AbstractProductB
        void charge();
    }
    public class HWCharger implements Charger{  // ProductB1
        @Override
        public void charge() {
            System.out.println("HW手机充电器");
        }
    }
    public class MICharger implements Charger {  // ProductB2
        @Override
        public void charge() {
            System.out.println("MI手机充电器");
        }
    }
    public interface PhoneFactory {   // AbstractFactory
        Phone producePhone();
        Charger produceCharger();
    }
    public class HWFactory implements PhoneFactory {  // Factory1
        @Override
        public Phone producePhone() {
            return new HWPhone();
        }
    
        @Override
        public Charger produceCharger() {
            return new HWCharger();
        }
    }
    public class MIFactory implements PhoneFactory {  // Factory2
        @Override
        public Phone producePhone() {
            return new MIPhone();
        }
    
        @Override
        public Charger produceCharger() {
            return new MICharger();
        }
    }
    public class Test {
        public static void main(String[] args) {
            PhoneFactory hwphoneFactory = new HWFactory();
            HWPhone hwPhone = (HWPhone) hwphoneFactory.producePhone();
            HWCharger hwCharger = (HWCharger)hwphoneFactory.produceCharger();
            hwPhone.playgames();
            hwCharger.charge();
    
            PhoneFactory miphoneFactory = new MIFactory();
            MIPhone miPhone = (MIPhone)miphoneFactory.producePhone();
            MICharger miCharger = (MICharger)miphoneFactory.produceCharger();
            miPhone.playgames();
            miCharger.charge();
        }
    }

    输出:

    HW手机玩游戏
    HW手机充电器
    MI手机玩游戏
    MI手机充电器
  • 相关阅读:
    类与对象
    类的声明与实例化
    面向对象的基本概念
    css下拉导航栏代码
    面向对象的三大特性
    面向对象三大基本特性,五大基本原则
    dom事件
    PHP 流程
    权限 查找
    留言板案例
  • 原文地址:https://www.cnblogs.com/jiazhongxin/p/12807264.html
Copyright © 2020-2023  润新知