Design Patterns Learning Notes (11: Abstract Factory mode)

Source: Internet
Author: User

1.1 Overview

provides an interface to create a series of or interdependent objects without specifying their specific classes. This is the definition of the abstract factory pattern.

Some systems may need to be designed to provide users with a range of related objects, but the system does not want the user to instantiate these objects directly using the new operator, but rather the system should control the creation of those objects, otherwise the user should not only know clearly which classes to use to create the objects. It must also be clear how the objects relate to each other, so that the user's code is tightly coupled with these classes, lacks elasticity, and is not conducive to maintenance.

For example, the Army will provide soldiers (users) with machine guns, pistols and corresponding bullets, but the army system does not want to be supplied by soldiers to produce machine guns, pistols and bullets, but by the corresponding factories dedicated to supporting production, namely, a factory dedicated to the production of machine guns, pistols and bullets. The exact relationship is as follows:

Photo: Arsenal and Firearms, Bullet relations (PS: Just as, don't tangle yo)

Abstract Factory method patterns can be used to design a system when the system is ready to provide a series of related objects to the user and does not want to create a coupling between user code and the class that creates the objects. The key to an abstract factory pattern is to define several abstract methods in an abstract class or interface that return an instance of a class, which allows its subclasses or classes that implement the interface to override these abstract methods to provide a series of related objects to the user.

Structure of the 1.2 pattern

The structure of the abstract factory pattern consists of four roles:

(1) abstract product: An abstract class or interface responsible for defining the methods that a specific product must implement;

(2) specific products (concreteproduct): The specific product is a class, if product is an abstract class, then the specific product is to implement the products if product is an interface, then the specific product is the class that implements the product interface;

(3) abstract Factory (abstractfactory): An interface or abstract class responsible for defining a number of abstract methods;

(4) specific factory (concretefactory): If the abstract factory is an abstract class, the concrete factory is the subclass of the abstract factory, and if the abstract factory is an interface, then the concrete factory is the class that implements the abstract factory interface. The abstract method in a specific factory rewrite abstract factory is that the method returns an instance of the specific product.

the class diagram for the abstract factory pattern is shown in two:

Figure two: Abstract factory pattern Structure class diagram

1.3 Abstract Factory advantages of the model

(1) Abstract factories can create a series of related objects for the user to decouple the user from the class that created the objects.

(2) using the abstract Factory mode makes it easy to create a series of objects for the user. Users can get a set of related objects using a different factory, while avoiding the need for users to mix objects in a series.

(3) in abstract Factory mode, the "specific factory" can be added at any time to provide users with a set of related objects.

1.4 Fit using the abstract factory pattern of the scene

(1) The system needs to provide multiple objects to the user, but does not want the user to instantiate the objects directly with the new operator, that is, to decouple the user from the creation object.

(2) The system needs to provide a series of objects to the user, but only need to know what methods are available for these objects, without requiring the user to know the creation process of these objects.

Design Patterns Learning Notes (11: Abstract Factory mode)

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.