Abstract Factory Pattern

The Abstract factory pattern is a creation pattern that provides an interface for creating a set of related or interdependent objects without specifying their concrete classes.

The abstract factory pattern provides an interface to clients to create product objects in multiple product families without specifying the specifics of the product.

role

1. Abstract Factory (Creator)

Is the core role of the abstract factory method pattern, the factory class of any object created in the pattern must implement this product family interface;

2. Concrete Creator

This is the concrete factory class that implements the abstract factory interface, contains application-specific logic, and is called by the application to create product objects;

3. Abstract Products

The base class of the object to be created, which is the common parent or co-owned interface of the product object;

4. Concrete Products

This role implements the interface defined by the abstract product role.

The sample





The AbstractFactory namespace contains the base MobilePhone class MobilePhone, the base portable device class Tablet, and the base Computer class Computer, as well as their corresponding six implementation classes. Abstract DeviceFactory class DeviceFactory contains two implementation classes, ChineseFactory ChineseFactory class and AmericanFactory AmericanFactory class. Today, with so much competition for smart devices, I will try to produce a batch of smart devices in two different factories so that you can understand the correct use of the abstract factory pattern.

public abstract class MobilePhone {

    public abstract void Print();

}
Copy the code

Abstract MobilePhone class MobilePhone, a member of the product family.

public class ApplePhone : MobilePhone {

    public override void Print() {
        Console.WriteLine("Apple Phone is created by American Factory!"); }}Copy the code

Apple mobile phone realization of ApplePhone, specific products.

public class HuaWeiPhone : MobilePhone {

    public override void Print() {
        Console.WriteLine("HuaWei Phone is created by Chinese Factory!"); }}Copy the code

Huawei Mobile phone implementation category HuaWeiPhone, specific product.

public abstract class Computer {

    public abstract void Print();

}
Copy the code

Abstract Computer class, a member of the product family.

public class DellComputer : Computer {

    public override void Print() {
        Console.WriteLine("Dell Computer is created by American Factory!"); }}Copy the code

DellComputer implementation class DellComputer, specific products.

public class LenovoComputer : Computer {

    public override void Print() {
        Console.WriteLine("Lenovo Computer is created by Chinese Factory!"); }}Copy the code

LenovoComputer class LenovoComputer, specific products.

public abstract class Tablet {

    public abstract void Print();

}
Copy the code

Abstract portable device class Tablet, a member of the product family.

public class Nexus10 : Tablet {

    public override void Print() {
        Console.WriteLine("Nexus10 is created by American Factory!"); }}Copy the code

Nexus10 Portable device implementation class Nexus10, specific products.

public class XiaoMiPad : Tablet {

    public override void Print() {
        Console.WriteLine("XiaoMiPad is created by Chinese Factory!"); }}Copy the code

Xiaomi tablet portable device implementation class XiaoMiPad, the specific product.

public abstract class DeviceFactory {

    public abstract MobilePhone CreateMobilePhone();

    public abstract Computer CreateComputer();

    public abstract Tablet CreatePad();

}
Copy the code

The Intelligent Device Production Factory base class, which defines a generation interface for a product family, is the main module distinguished from the factory method pattern.

public class AmericanFactory : DeviceFactory {

    public override MobilePhone CreateMobilePhone() {
        return new ApplePhone();
    }

    public override Computer CreateComputer() {
        return new DellComputer();
    }

    public override Tablet CreatePad() {
        return newNexus10(); }}Copy the code

AmericanFactory is a specific smart device manufacturing factory.

public class ChineseFactory : DeviceFactory {

    public override MobilePhone CreateMobilePhone() {
        return new HuaWeiPhone();
    }

    public override Computer CreateComputer() {
        return new LenovoComputer();
    }

    public override Tablet CreatePad() {
        return newXiaoMiPad(); }}Copy the code

ChineseFactory is a specific intelligent equipment production factory.

public class CreateDevice<T> where T : DeviceFactory {

    private static T _deviceFactory = null;

    public static void Create(){ _deviceFactory = Activator.CreateInstance<T>(); _deviceFactory.CreateMobilePhone().Print(); _deviceFactory.CreateComputer().Print(); _deviceFactory.CreatePad().Print(); }}Copy the code

A helper class that helps me produce smart devices and output results to the command line.

public class Program {

    public static void Main(string[] args){ CreateDevice<ChineseFactory>.Create(); CreateDevice<AmericanFactory>.Create(); Console.ReadKey(); }}Copy the code

This is the caller’s code, and here is the output of the case:

HuaWei Phone is created by Chinese Factory!
Lenovo Computer is created by Chinese Factory!
XiaoMiPad is created by Chinese Factory!
Apple Phone is created by American Factory!
Dell Computer is created by American Factory!
Nexus10 is created by American Factory!
Copy the code

advantages

1. Able to succinctly obtain desired specific products from multiple products of multiple product families; 2. Solve the problem of inconsistent opening and closing principle in factory mode.

disadvantages

1. It is difficult to expand product family. To add a certain product in a series, specific product categories and corresponding factory categories should be added.

Usage scenarios

1. A system is not required to depend on how instances of product classes are created, combined, and expressed, which is a prerequisite for all factory pattern applications; 2. There are multiple series of products in the system, and only one series of products is consumed in the system; 3, the system requires to provide a library of product classes, all products to the same interface, the client does not need to rely on concrete implementation.