Abstract factory pattern: get the object and ignore the implementation process


1 Introduction

1.1. Definition

​ Abstract Factory Pattern (Abstract Factory Pattern) is around aAbstract factorycreateOther factories. The abstract factory pattern provides an interface for creating a series of related or interdependent objects without specifying their specific classes.


1.2 Applicable scenarios

	- 客户端(应用层)不依赖于产品类实例如何被创建、实现等细节。
	- 强调一系列相关的产品对象(属于同一产品)一起使用创建对象需要大量的重复代码。
	- 提供一个产品类的库,所有的产品以同样的接口出现,从而使得客户端不依赖于具体的实现。


1.3, advantages

  • The code isolation of specific products at the application layer does not need to care about the details of creation.
  • Create a series of products together.


1.4. Disadvantages

  • All product sets that may be created are specified, and it is difficult to expand new products in the product cluster.
  • Increase the abstraction of the system and the difficulty of understanding.


2. Realize


Take Huawei and Xiaomi's corresponding product mobile phones and routers as examples


Step 1. Define the product interface

//手机产品接口
public interface IphoneProduct {
    
    
    void start();
    void shutdown();
    void callup();
    void sendSMS();
}
//路由器产品接口
public interface IRouterProduct {
    
    
    void start();
    void shutdown()
    void openWife();
    void setting();
}



Step 2. Create a product corresponding to the interface

//华为手机
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("打开华为Wife");
    }

    @Override
    public void setting() {
    
    
        System.out.println("华为设置");
    }
}

//小米手机
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("打开小米Wife");
    }

    @Override
    public void setting() {
    
    
        System.out.println("小米设置");
    }
}



Step 3. Create abstract factory

//抽象产品工厂
public interface IProductFactory {
    
    
    //生产手机
    IphoneProduct iphoneProduct();

    //生产路由器
    IRouterProduct routerProduct();
}



Step 4. Create other factories

public class XiaomiFactory implements IProductFactory {
    
    
    @Override
    public IphoneProduct iphoneProduct() {
    
    
        return new XiaomiPhone();
    }

    @Override
    public IRouterProduct routerProduct() {
    
    
        return new XiaomiRouter();
    }
}
public class HuaweiFactory implements IProductFactory {
    
    
    @Override
    public IphoneProduct iphoneProduct() {
    
    
        return new HuaweiPhone();
    }

    @Override
    public IRouterProduct routerProduct() {
    
    
        return new HuaweiRouter();
    }
}





Learn about other design patterns

Guess you like

Origin blog.csdn.net/qq_44972847/article/details/108058748