Exploring Abstract Factory and construction modes

Source: Internet
Author: User
Tags ibutton

Among the 23 gof (Gang of Four) design patterns, Singleton, simple factory, and factory Method) abstract Factory, builder, and prototype all belong to the creation mode. But the abstract factory mode and generator mode are the most confusing for beginners. This article uses an example to illustrate the differences between the abstract factory mode and the construction mode and their respective use cases.

Suppose we want to create a form, and the form should support multiple themes. The basic component of form is a panel and three buttons (maximize, minimize, and close buttons ), there is also a status bar,
To make the design simple and easy to understand, we temporarily ignore the title bar, title bar icon, menu bar, and toolbar, because we can call this thing a form even without them. What we need to do now is to create two forms with different themes, and set it as blue XP and freshly baked Vista. Of course, it must also support the "open-close" principle (OCP), which can easily expand more topics.

Step 1: Create an Interface

Create three APIs ipanel, iButton (iButton then generates three sub-interfaces, imaximizebox, iminimizebox, and iclosebox, which correspond to the maximization, minimization, and closure buttons respectively), and istatusbar. Maybe they belong to controls, but this article does not consider these additional things. Then create the final product interface iform.

Class diagram ):

The interface is just abstract. To get the product instance, we must create a specific type for it.

Step 2: Abstract Factory model (form parts manufacturer)

The following is the time for the abstract factory to debut. The abstract factory aims to provide an interface to the client so that the client does not have to specify the specific product type, create Product objects in multiple product families.

Here, the abstract factory class formfactory will create parts of an XP or Vista form, except for the form, because the form does not belong to the zero part here, it is the final product, its creation work is left to the construction mode.

The following is a class diagram of the form part created by the Abstract Factory:

Step 3: construction mode (form assembly plant)

Here, our client can create one or more parts in the factory and add them to our forms. In fact, most forms are composed of these parts, here we only use the build mode to build a basic form consisting of a panel, three buttons (maximize, minimize, and close buttons), and the status bar.

The following is a class diagram of a form assembled in the construction mode:

The formbuilder serves as the client in the preceding figure. Now we can create an XP or Vista style form through formbuilder. Because the design mode supports the first principle of software design OCP, therefore, we can easily expand other third-party themes, so that our users can use their imagination.

It is worth noting that the Construct Method of the formdirector class does not simply execute the buildx method in formbuilder in sequence. We can only buildclossebox instead of buildmaximizebox and buildminimizebox, in this way, we create a form that cannot be maximized or minimized, or execute buildpanel multiple times to create a form composed of multiple panels.

The construction mode hides the product structure and product part construction process from the client, and separates the responsibility for directing the construction process from the responsibility of specific builder parts, achieve responsibility division and encapsulation.

Here we can create a desired form. We mainly want to use this example to illustrate that the abstract factory mode and the construction mode are actually far different in duties and intentions, they can also be combined in this way to complement each other.

The following briefly summarizes the similarities and differences between the two modes.

Similarities:
1. It belongs to the creation mode;
2. Create a pattern for objects of the same product family;

Differences:
1. the abstract factory model focuses on the object creation process. The object here refers to parts or final products. After all, some objects constitute a product we want;
2. The construction model focuses on the final result of product construction. The product here refers to the finished product, that is, what the user finally hopes to get;

The last complete graph. The understanding and kindness of the model are in the learning stage, and there are unreasonable or wrong points in the text. You are welcome to criticize and point out.


References:
《JavaAnd Mode
Design Patterns (Chinese Version)
C # An overview of the object-oriented design model

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.