Design Patterns learning 03-Abstract Factory patterns

Source: Internet
Author: User
1. Motivation and definition
In the factory mode, a factory can only provide one or more products. When there are many product types, a series of GUI controls are formed (for example, we want to create cross-platform buttons, menus, text boxes, and so on:
Using the factory model alone will produce a large number of factories, and the maintenance is not convenient in the future. We can find the rules from the products. If the product level is relatively fixed, only new product families will be added in the future, then we can place the entire product family in a factory for creation, and it will be very convenient to add other system product families in the future. For example:
This mode is the abstract factory. The factory method mode is for a product level structure, while the abstract factory mode needs to face multiple product level structures, A factory level structure can be used to create multiple product objects in different product levels.
2. Structure and class diagram
A general example is shown in the following figure: the example Class legend for creating GUI at the beginning of the text is shown below. Here, only button and textarea are created:

The product class and interface code is as follows (for convenience of demonstration, logic is omitted, etc ):
interface Button {}interface TextArea {}class WindowButton implements Button {}class LinuxButton implements Button {}class WindowTextArea implements TextArea {}class LinuxTextArea implements TextArea {}
Factory Code:
Public interface iguifactory {// product button creation method button createbutton (); // product textarea creation method textarea createtextarea (); // There are N product levels, for N creation methods} class linuxguifactory implements iguifactory {@ override public button createbutton () {return New linuxbutton () ;}@ override public textarea createtextarea () {return New linuxtextarea () ;}} class windowsguifactory implements iguifacments {@ override public button createbutton () {return New windowbutton () ;}@ override public textarea createtextarea () {return New windowtextarea ();}}
Client code:
Public class client {public static void main (string [] ARGs) {// create a factory using the new method for demonstration, it is best to make it a singleton or initialize iguifactory factory = new windowsguifacloud (); button = factory. createbutton (); textarea = factory. createtextarea (); system. out. println (button); system. out. println (textarea );}}
How many product families are created, how many factories are created, and how many product levels are provided in the factory.
3. Applicable scenarios and effects (advantages and disadvantages)
In fact, the strengths of the factory model are basically the same as that of the abstract factory. Here we talk about the unique strengths of the abstract factory: 1. Constraints on the internal elements of the product family to control the relationship between products within the product family, quantity. For example, if a and B are products in the product family, one a is required to generate two B instances. At this time, because they are all in a factory implementation class, so it is easy to control. 2. It is easy to expand product families and factories. For example, in the above example, you need to expand a class of system guis, such as Android, add? A set of GUI controls and the android control factory. 3. Reduce client inference. When products in a product family work together, the abstract factory ensures that the client always only uses products in the same product family, instead, you don't need to deduce which product to use based on the current environment. For example, if you use windowsguifacloud, the client does not need to deduce the operating system when using the control. You just need to use windowsguifacloud to obtain the product. The disadvantage is also obvious. The biggest drawback is that it is very difficult to expand the product level. In the above example, to add a control, you must add a method to all the factories, a serious violation of the open and closed principles, interface changes, all client code also need to be affected, so the abstract factory must ensure that the product level is not changed or rarely changed. In fact, this is also a design trade-off. Sometimes, if you want to make the expansion and use easy on the one hand, it will lead to a difficult expansion on the other hand. Assuming that there is such a Skew in the business, we can do this design. There is no absolute good design, and there is only a suitable design. In applicable scenarios, when the detailed products required by the factory provided by the system are not a simple object, we will consider using the abstract factory model when there are multiple detailed products that belong to different types in different product levels. In combination with the advantages and disadvantages above, let's take a few detailed application scenarios: 1. When one or more product families exist in the system, each time only one product family is used, for example, some desktop programs need to replace theme, skin, and other functions. 2. When the system provides a product library, all products appear in the interface mode and abstract factories can be considered; 3. the same type of product family of the system should be put together for use. It is best to associate such constraints in the design and be able to use abstract factories.
4. Mode Scaling
1. Factory model conversion: when there is only one product grade, abstract factories can be degraded into common factories. For example, the implementation is simpler than simplified, and there are not many product classes, so normal factories will be degraded into simple factories. 2. Factory combination. Abstract factories and general factories generate many factory classes when there are too many products. We can consider using simple factories to encapsulate the factory classes, it is more convenient for the client to obtain the factory. In fact, the factory model is the most widely used model in the program. It is often used in combination with other models. No model can use a certain environment by 100%, and only has the flexibility of the model, A Multi-Mode hybrid architecture can produce excellent designs.

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.