Design Patterns learning summary 1-creation type 1-Abstract Factory patterns

Source: Internet
Author: User

Abstractfactory Abstract Factory mode (Creation type)

Purpose:

This mode allows you to create different objects. These objects are similar to and generally created together. Abstract factories are divided into different factories to create different products with similar products. This mode separates the clients used for creation through the factory. In this way, various products can be easily changed without changing the user structure.

Role
This pattern supports the creation of products that exist in families and are designed to be produced together. the abstract factory can be refined to concrete factories, each of which can create different products of different types and in different combinations. the pattern isolates the product definitions and their class names from the client so that the only way to get one of them is through a factory. for this reason, product families can easily be interchanged or updated without upsetting the structure of the client.

Design:

The abstract factory interface iabstractfactory contains operations for creating abstract products of various types;

The factory 1Factory1 and 2Factory2 implement the operation of creating products through the abstract factory interface IAbstractFactory;

Abstract product interfaces: IProductA and IProductB, which contain operations of a specific product;

Specific products ProductA1, ProductA2, ProductB1, and ProductB2 implement specific product categories of abstract product interfaces, which are produced by the corresponding factory;

Example:

AbstractFactory produces a pen
FactoryA hero pen factory
FactoryB yongsheng pen factory
AbstractProduct pen Product
ProductA1 hero gold
ProductB1 permanent gold pen

ProductA2 hero pen
ProductB2 permanent pen
Code: 

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;

Namespace AbstractFactory
{
// Abstract Factory
Abstract class AbstractFactory
{
Abstract public AbstractWheel CreatWheel ();
Abstract public AbstractOilBox CreatOilBox ();
}

Abstract class AbstractWheel
{
Public encryption actwheel ()
{
// Console. Write ("Create a AbstractProduct ");
}
}

Abstract class AbstractOilBox
{
Public AbstractOilBox ()
{}
}

Class BMWFactory: AbstractFactory
{
Public override maid ()
{
Return new BMWWheel ();
}

Public override maid ()
{
Return new BMWOilBox ();
}
}

Class BMWWheel: AbstractWheel
{
Public BMWWheel ()
{
Console. WriteLine ("Create a BMWwheel ");
}
}

Class BMWOilBox: AbstractOilBox
{
Public BMWOilBox ()
{
Console. WriteLine ("Create a BMWOilBox ");
}
}

Class BORAWheel: AbstractWheel
{
Public BORAWheel ()
{
Console. Write ("Create a BORAWheel ");
}
}

Class BORAOilBox: AbstractOilBox
{
Public BORAOilBox ()
{
Console. Write ("Create a BORAOilBox ");
}
}

Class BORAFactory: AbstractFactory
{
Public override maid ()
{
Return new BORAWheel ();
}

Public override maid ()
{
Return new boraoilbox ();
}

}


Class Program
{
Static void main (string [] ARGs)
{
Abstractfactory factory = NULL;
Factory = new borafactory ();
Factory. CreatWheel ();
Factory. CreatOilBox ();
Console. ReadLine ();

Factory = new BMWFactory ();
Factory. CreatWheel ();
Factory. CreatOilBox ();
Console. ReadLine ();
}
}
}

 

Use Cases:

1. The system does not rely on the creation, composition, and representation of objects to be used.
2. The system needs to configure multiple sets of objects.
3. Object creation and factory creation must be mandatory
4. Focus on exposing interfaces rather than implementing them

Use the Abstract Factory pattern when...
• A system shoshould be independent of how its products are created, composed, and represented.
• A system can be configured with one of multiple families of products.
• The constraint requiring products from the same factory to be used together must be enforced.
• The emphasis is on revealing interfaces, not implementations.

Summary:

Abstract Factory mode is a creation mode to solve the problems caused by Object Instantiation. In reality, the system always needs to change to instantiate different objects. Therefore, the "Create operation" is encapsulated into the abstract factory, and the specific factory is used to instance the corresponding objects. The abstract factory mode is interface-oriented and encapsulates operations on instantiated objects. Operations on instance objects are implemented in different factories.
In the system, we often encounter the creation of "a group of mutually dependent objects". At the same time, due to changes in requirements, more series of objects are often created. In the face of this problem, avoid using the conventional object creation method, and use "encapsulate the instance China object" to avoid the tight coupling between the system and the "Multi-series object creation work.
Stable demand. There is no difference between loose coupling and tight coupling when the system remains unchanged. However, the demand changes require system "decoupling" in some aspects ", abstract Factory decouples Object Instantiation from the system.
Design Pattern: provides an interface to create a series of "related or interdependent objects" without specifying their specific classes. Abstract Factory mode:
1. If the requirement for "Multi-series object building" is not changed, the abstract factory mode is not necessary.
2. "series object" refers to the relationship between objects that are mutually dependent or act.
3. The Abstract Factory mode mainly aims to cope with the changes in the needs of the new series. The disadvantage is that it is difficult to cope with changes in the demand for "new objects. It should be noted that, as mentioned above, if we want to add other series classes, the code will be greatly changed.
4. The Abstract Factory mode is often combined with the factory method mode to cope with the changes in the "Object creation" requirement.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.