Analysis of the implementation of abstract factory pattern in design pattern in IOS application development _ios

Source: Internet
Author: User
Tags inheritance php book

Overview
Abstract Factory pattern is the object creation pattern, it is the further promotion of the factory method pattern.

Suppose a subsystem needs some product objects, and these products belong to more than one product hierarchy. In order to separate the responsibility for consuming these product objects from the responsibility to create them, abstract factory patterns can be introduced. In this way, one side of the consumer product does not need to directly participate in the creation of the product, but only to request the desired product to a common factory interface.

By using the abstract factory pattern, you can work with creating problems with product objects in multiple product families in the same (or similar) hierarchy. As shown in the following illustration:

According to the product role structure diagram, it is not difficult to give the factory role of the structural design diagram.
As you can see, each factory role has two factory methods that are responsible for creating product objects that are part of a different product hierarchy.

The function of an abstract factory is to create an interface for a series of related objects or interdependent objects. It is important to note that the method inside this interface is not a random stack, but a series of related or interdependent methods. For example, the motherboard and CPU in the example above are all meant to assemble a computer related object. Different installed programs, representing a specific computer series.


Because the abstract factory defines a series of objects that are usually related or interdependent, these product objects form a product family, which is the abstract factory that defines a product family.

This brings a lot of flexibility when switching the product family, as long as it provides a different abstract factory implementation, which means that it is now switched to a product family as a whole.

Core
first of the previous picture:

We still take the bitter process ape for example, some core concepts of abstract factory pattern. You can see from the diagram above that the vertical two-dimensional coordinate can determine the single point on the plane, which is the core of the abstract factory.

Product Hierarchy structure: is the inheritance structure. Like above android,ios,php these skills are inherited from an abstract skill class (such as the previous icode), and this abstract class and these subclasses form the product hierarchy. The same Android books, C language books, script books inherit from a reference book class, this reference book abstract class and these subclasses constitute a hierarchical structure.

Product Family: The product family in the abstract factory model the official definition refers to a group of products produced by the same factory, located in different product hierarchy. For example, the above Android is in the skill level structure, the Android book is in the tool hierarchy, the Android skills and the Android book are a set of products in different product structures, but any program ape needs skills and reference books, For example, an Android program ape needs to have Android skills and Android books, so the Android Ape is a product family.

Concept: Provides an interface to create a series of related or interdependent objects without having to specify their specific classes. The abstract factory pattern, also known as the kit pattern, is an object-creation pattern.

Key: Abstract factory model structure important core module:

Abstract Factory:

Declares a set of methods for creating a family of products, each of which corresponds to a product.

Specific factory:

Implements a method of creating a product declared in an abstract factory, generating a set of specific products that form a family of products in which each product is located in a product hierarchy.

Abstract Products:

It declares the interface for each product and declares the business method of the product in the abstract product.

Specific Products:

Define specific product objects for specific factory production to implement the business methods declared in the abstract product interface.

Usage scenarios:

Abstract factory patterns can be used when an object that needs to be created is a series of interconnected or interdependent product families. Vernacular meaning is an inheritance system, if there are multiple hierarchical structure (that is, there are many abstract classes, such as the above skills and reference books), and the various hierarchical structure of the implementation of the class has a certain degree of association or constraints, you can use the abstract factory model. Of course, the same thing is that if there is no association or constraint between the implementation classes in each hierarchy, multiple separate factories are used to create the product.

Program instance
The following is an example of the above figure on the text interpretation of the implementation code, no longer explained:

Copy Code code as follows:

Package Yanbober.github.io;
/* Skill Level structure Part * *
Interface Icode {
void coding ();
}

Class Codeimplandroid implements Icode {
@Override
public void Coding () {
SYSTEM.OUT.PRINTLN ("Coding android!");
}
}

Class Codeimplphp implements Icode {
@Override
public void Coding () {
SYSTEM.OUT.PRINTLN ("Coding php!");
}
}
/* Reference book Grade structure * *
Interface Ineedbook {
void Lookbook ();
}

Class Needbookimplandroid implements Ineedbook {
@Override
public void Lookbook () {
System.out.println ("Look Android book!");
}
}

Class Needbookimplphp implements Ineedbook {
@Override
public void Lookbook () {
System.out.println ("Look PHP book!");
}
}
/* Product Family * *
Interface Iabstractfactory {
Icode Getcodingskill ();
Ineedbook Getneedbook ();
}

Class Factoryimplandroid implements Iabstractfactory {
@Override
Public Icode Getcodingskill () {
return new Codeimplandroid ();
}

@Override
Public Ineedbook Getneedbook () {
return new Needbookimplandroid ();
}
}

Class Factoryimplphp implements Iabstractfactory {
@Override
Public Icode Getcodingskill () {
return new codeimplphp ();
}

@Override
Public Ineedbook Getneedbook () {
return new needbookimplphp ();
}
}

public class Main {
public static void Main (string[] args) {
Iabstractfactory factory = new Factoryimplandroid ();
Icode code = Factory.getcodingskill ();
Ineedbook book = Factory.getneedbook ();
Code.coding ();
Book.lookbook ();

Factory = new factoryimplphp ();
Code = Factory.getcodingskill ();
Book = Factory.getneedbook ();
Code.coding ();
Book.lookbook ();
}
}


Tips: You can still use configuration and reflection to achieve automatic adaptation.


to sum up a
the advantages of abstract Factory mode:

As before, isolate the build of specific classes so that customers do not need to know what is created.
Add new product family is very convenient, no need to modify the existing system, in line with the "open-closing principle."
Disadvantages of abstract Factory pattern:

Add new product class structure trouble, need to make large changes to the original system, even need to modify the abstract layer code, violate the "open and closed principle."

Related Article

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.