Net Design Pattern of Study Notes series (2): Abstract Factory)

Source: Internet
Author: User

Motivation)

In software systems, there are often "a series of mutually dependent objects". At the same time, due to changes in requirements, more series objects are often created.
How to deal with this change? How to bypass the conventional object creation method (new) and provide an "encapsulation mechanism" to avoid customersProgramIs it tightly coupled with this "Multi-series object creation work?

Intent)
Provides an interface for this interface to create a series of "related or interdependent objects" without specifying their specific classes.
-- Design Pattern gof

Abstract Factory mode provides an interface to the client, so that the client creates product objects in multiple product families without specifying the specific product type.

Applicability

Gof95 indicates that the factory model should be considered in the following situations:
A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.
This system has more than one product family, and the system only consumes one of them.
Products belonging to the same product family are used together. This constraint must be reflected in the system design.
The system provides a product library. All products use the same interface, so that the client does not rely on implementation.

Structure)

 CodeImplementation

1 Public   Abstract   Class Producta
2 {
3
4}
5
6 Public   Class Producta1: producta
7 {
8
9}
10
11 Public   Class Producta2: producta
12 {
13
14}
15
16 Public   Abstract   Class Productb
17 {
18
19}
20
21 Public   Class Productb1: productb
22 {
23
24}
25
26 Public   Class Productb2: productb
27 {
28
29}

 

1 Public   Abstract   Class Creator
2 {
3}
4
5 Public   Abstract   Class Concretecreatora: Creator
6 {
7 Public Producta factorya ()
8 {
9Return NewProducta1 ();
10}
11
12 Public Productb factoryb ()
13 {
14Return NewProductb1 ();
15}
16 }
17
18 Public   Abstract   Class Concretecreatorb: Creator
19 {
20 Public Producta factorya ()
21 {
22Return NewProducta2 ();
23}
24
25 Public Productb factoryb ()
26 {
27Return NewProductb2 ();
28}
29 }

Abstract Factory mode
? If the requirement for "Multi-series object building" is not changed, the abstract factory mode is not necessary. In this case, a simple static factory is enough.
? "Series objects" refer to the relationships between these objects, such as the dependencies between "roads" and "Houses" in game development scenarios, "road" and "authentic" dependencies.
? Abstract Factory mode is mainly used to cope with changes in the needs of the new series. The disadvantage is that it is difficult to cope with the changes in the demand for "new objects.
? Abstract Factory mode is often combined with the factory method mode to cope with 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.