Design Patterns learning 03-Abstract Factory patterns

Source: Internet
Author: User

1. Motivation and definition
In factory mode, a factory can only provide one or more products. When there are many product types, a series of GUI controls (for example, we want to create cross-platform buttons, menus, text boxes, and so on) are formed:
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, such:
This mode is an 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
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPgogICAgIM2o08PA4M28yOfPwqO6CjxpbWcgc3JjPQ = "http://www.2cto.com/uploadfile/2014/0607/20140607113023970.png" alt = "\"> the example class that begins with 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 button = factory. createButton (); TextArea 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 advantages of the factory model are basically the same as that of the abstract factory. Here we talk about the unique advantages of the abstract factory: 1. Constraints on the internal elements of the product family can control the relationship between products within the product family, quantity, etc. For example, if the product in the product family has products A and B, two B S are generated when one A is produced. At this time, because they are all in A factory implementation class, so it is very easy to control. 2. It is very easy to expand product families and factories. For example, in the above example, you need to expand a class of system guis, for example, android, and add a set of GUI controls and android control factories. 3. Reduce client judgment. When a product in a product family works together, the abstract factory ensures that the client always only uses products in the same product family, you don't need to determine which product to use according to the current environment. For example, if you use windowsguifacloud, the client does not need to determine which operating system to use 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, we need to add a method to all the factories, this seriously violates the open and closed principles, modifies interfaces, and affects all client code. Therefore, the abstract factory must ensure that the product level is not changed or rarely changed. In fact, this is also a trade-off in design. Sometimes it is very difficult to expand and use on the one hand, and if the business has such skew, we can do this design, there is no absolute good design, only suitable design. In applicable scenarios, the specific products required for production by the factory provided by the system are not a simple object, however, when multiple products belong to different types in different product levels, we will consider using the abstract factory model. Based on the advantages and disadvantages above, we will introduce several specific 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 into use together. It is best to associate these constraints in the design and abstract factory can be used.
4. Mode Scaling
1. Factory model conversion. When there is only one product grade, abstract factories can be degraded into common factories. If the implementation is simple and there are not many product classes, 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. You 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 mode is the most widely used mode in the program. It is often used in combination with other modes. No mode can use a certain environment for 100%, and only the mode is flexible and flexible, 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.