Design Mode C # Description - Abstract Factory Model

zhaozj2021-02-16  42

Design Mode C # Description - Abstract Factory Model

Read this article first read simple factory model and factory method mode

Abstract factory model is an object's creation mode, which is a further promotion of factory method patterns.

Suppose a subsystem requires some product objects, and these products belong to more than one product level structure. Then, in order to divide the responsibility of consumed these product objects and the responsibilities of the creation of these product objects, the abstract factory model can be introduced. In this way, customers of consumer products do not need to directly participate in the creation of the product, but only need to request the products needed to a public factory interface.

System class diagrams designed with abstract factory models are as follows.

As can be seen from the above figure, the abstract plant mode is designed to the following roles:

Abstract Factory Role: The role is the core of the factory method model, which is unrelated to the business logic of the application system. Usually implemented using an interface or abstract class.

Specific factory role: This role creates an instance of a product directly under the call. This role contains the logic of the appropriate product object, and this logic is closely related to the commercial logic of the application system. Usually use specific class implementations.

Abstract Product Role: The class that serves this role is the parent class of the object created by the abstract factory method mode, or the interface they share. This role is usually implemented using an interface or abstract class.

Specific product role: Any product object created by abstract plant mode is an example of a specific product class. This is something that the client is finally needed. This role is usually implemented using a specific class.

The original code for this system is given below:

Creator:

Public Interface Creator

{

Producta Factorya ();

ProductB fabryb ();

}

Concretecreator1:

Public Class Concretecreator1: CREATOR

{

Public producta factorya ()

{

Return new producta1 ();

}

Public productB factoryB ()

{

Return new productb1 ();

}

}

Concretecreator2:

Public Class Concretecreator2: Creator

{

Public producta factorya ()

{

Return new producta2 ();

}

Public productB factoryB ()

{

Return new productb2 ();

}

}

Producta:

Public Interface Producta PUBLIC Interface

{

}

ProductA1:

Public Class Producta1: Producta PRODUCTA

{

Public productA1 ()

{

}

}

ProductA2:

Public Class Producta2: Producta PRODUCTA

{

Public productA2 ()

{

}

}

ProductB:

Public Interface ProductB, PUBLIC INTERFACE

{

}

ProductB1:

Public Class ProductB1: ProductB? PRODUCTB

{

Public productB1 ()

{

}

}

ProductB2:

Public Class ProductB2: ProductB, PRODUCTB

{

Public productB2 ()

{

}

}

Abstract factory models should be used in the following cases:

A system should not depend on how the product instance is created, combined, and expressed, which is important for factory models of all forms.

This system has more than one product family, and the system only consumes the products of one of them.

It is used with products that belong to the same product family, which must be reflected in the design of the system.

The system provides a library of a product class, all products appear in the same interface, so that the client does not rely on implementation.

转载请注明原文地址:https://www.9cbs.com/read-25081.html

New Post(0)