抽象工厂模式

3、抽象工厂模式

抽象工厂模式提供一个创建一系列相关或者相互依赖对象的接口,无需指定他们具体的类

1、创建产品接口

package com.design.model_02_factory.abstract01;

/**
 *手机产品接口
 * @program: designMode
 * @description:
 * @author: lxf
 * @create: 2021-09-09 14:36
 */
public interface IphoneProduct {

    void  call();   //打电话
    void  message();  //发短信
    void  palyGame(); //玩游戏
}


package com.design.model_02_factory.abstract01;

/**
 * @program: designMode
 * @description: 路由器产品接口
 * @author: lxf
 * @create: 2021-09-09 14:43
 */
public interface RouterProduct {

     void  shutdown();
     void  openWifi();
     void  setting();


}

2、创建产品的接口的实现类

package com.design.model_02_factory.abstract01;

/**
 * @program: designMode
 * @description: 小米手机
 * @author: lxf
 * @create: 2021-09-09 14:46
 */

public class XiaoMiPhone  implements IphoneProduct{

    @Override
    public void call() {
        System.out.println("小米手机打电话");
    }

    @Override
    public void message() {
        System.out.println("小米手机发消息");

    }

    @Override
    public void palyGame() {
        System.out.println("小米手机玩游戏");
    }
}

3、抽象产品工厂

package com.design.model_02_factory.abstract01;

/**
 * @program: designMode
 * @description: 抽象产品工厂
 * @author: lxf
 * @create: 2021-09-09 14:55
 */
public interface ProductFactory {
    //生产手机
    IphoneProduct  createIphoneProduct();
    //生产路由器
    RouterProduct  createRouterProduct();
}

4、抽象产品工厂的接口的实现类

package com.design.model_02_factory.abstract01;

/**
 * @program: designMode
 * @description: 华为工厂
 * @author: lxf
 * @create: 2021-09-09 14:59
 */
public class HuaWeiFactory implements ProductFactory {


    @Override
    public IphoneProduct createIphoneProduct() {
        return new HuaWeiPhone();
    }

    @Override
    public RouterProduct createRouterProduct() {
        return new HuaWeiRouter();
    }
}


package com.design.model_02_factory.abstract01;

/**
 * 小米工厂
 * @program: designMode
 * @description:
 * @author: lxf
 * @create: 2021-09-09 14:58
 */
public class XiaoMiFactory implements ProductFactory{

    @Override
    public IphoneProduct createIphoneProduct() {
        return new XiaoMiPhone();
    }

    @Override
    public RouterProduct createRouterProduct() {
        return new XiaoMiRouter();
    }
}


4、消费者

package com.design.model_02_factory.abstract01;

/**
 * @program: designMode
 * @description: 测试
 * @author: lxf
 * @create: 2021-09-09 15:02
 */
public class Client {

    public static void main(String[] args) {
        System.out.println("=====================小米系列产品=========================");

        XiaoMiFactory xiaoMiFactory = new XiaoMiFactory();
        IphoneProduct iphoneProduct = xiaoMiFactory.createIphoneProduct();
        iphoneProduct.call();
        iphoneProduct.message();
        RouterProduct routerProduct = xiaoMiFactory.createRouterProduct();
        routerProduct.openWifi();
        routerProduct.shutdown();
    }
}

抽象工厂模式

上一篇:Android Apk包下查看 sha1


下一篇:Js实现监听input输入