Common iOS design modes-factory method (simple factory mode, factory method mode, abstract factory mode) and ios Design Mode

Source: Internet
Author: User

Common iOS design modes-factory method (simple factory mode, factory method mode, abstract factory mode) and ios Design Mode

1. Simple factory Model


How can we understand three design modes: simple factory, factory method, and abstract factory?

 

Simple factory life scenarios, small vendors selling breakfast, he provides you with steamed buns, steamed buns, digou oil-branded pancakes, etc. vendors are a factory that produces steamed buns, steamed buns, slice oil-branded pancakes. The UML diagram corresponding to this scenario is as follows:


Figure 1: simple factory mode UML diagram


Participants in the simple factory model:

 

Factory role: accepts client requests and creates corresponding product objects through requests.

 

Abstract Product role: it is the parent class or a common interface of the object created in the factory mode. Abstract classes or interfaces.

 

Product (ConcreteProduct) object: The objects created in factory mode are instances of this role.

 

Evolution of simple factory models:

1) when there is only one product in the system, the abstract product can be omitted, as shown in 1. In this way, the factory roles and specific products can be combined.

 

Advantages and disadvantages of the simple factory model:

1.) The factory class contains the logic of the product to be created. In this way, the client only needs to request the desired product, regardless of the product implementation details.

2) There is only one factory class, which integrates the logic for creating all products. It will be the bottleneck of the entire system and make the system difficult to expand.

3) The simple factory mode usually uses the static factory method, which makes the factory class unable to inherit from the subclass, which makes the factory role unable to form a hierarchy based on inheritance.


2. Factory method mode

Factory methods use OOP polymorphism to abstract factories and products into a base class, define unified interfaces in the base class, and then create a specific product in a specific factory. For the life scenarios of the factory method, Unilever wants to produce "xiasilian" and "Qingyang" shampoo. It will build a factory that produces "xiasilian" and a factory that produces "Qingyang.


Figure 2: UML diagram of the factory Method

Participants in the factory method mode:

 

Abstract Factory role:It has nothing to do with the application. Any factory that creates an object in the mode must implement this interface.
Specific factory roles:A specific class that implements the abstract factory interface, contains the logic closely related to the reference, and is called by the application to create product objects.
Abstract Product role:The super type of the product object created by the factory method, that is, the common parent class or interfaces of the product object.
Specific product roles:This role implements the interface known as the abstract Product role. Each specific product object created by the factory method is an instance of a specific product role.

 

 

Advantages and disadvantages of the factory method:

 

1) reduces the cohesion of factory classes, satisfies the hierarchical relationship between classes, and well complies with the single responsibility principle in object-oriented design. This facilitates program expansion, as shown in Figure 3:

Figure 3: Extended UML diagram of factory methods

Summary: extract "commonalities" and establish the implementation of inheritance commonalities based on their respective "personalities"


3. Abstract Factory Design Model

Abstract Factory refers to a factory level structure that allows you to create all objects in a product family with different product levels. The following example describes how to create a Unix control and a Windows Control, we need two sub-factories under an abstract factory. One is UnixFactory, which is used to produce Unix control and the other is WinFactory, which is used to produce Win control. The difference between the abstract factory and the factory method is that the specific factory in the factory method generally only produces one or several control objects, while the specific factory in the abstract factory produces a family of control objects. 4.


Figure 4: Abstract Factory Design Pattern UML diagram


Participants in the abstract factory:

Abstract Factory role: this role is at the core of the Factory method model and has nothing to do with the business logic of the application system.

Concrete Factory role: this role directly creates a product instance under the client call. This role contains the logic for selecting appropriate product objects, which is closely related to the business logic of the application system.

Abstract Product role: the class that assumes this role is the parent class of the object created in the factory method mode, or the interfaces they share.

Concrete Product role: Any Product object created in the abstract factory model is an instance of a specific Product category. This is what the client eventually needs, and it must be filled with the commercial logic of the application system.

 

Abstract Factory application scenarios:

  • A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all forms of factory models.
  • This system has more than one product family, and the system only consumes one of them.
  • Products belonging to the same product family are used together. This constraint must be reflected in the system design.
  • The system provides a product library. All products use the same interface, so that the client does not rely on implementation.

 

Differences between Abstract Factory mode and factory method mode

 

Factory method mode: Each abstract product derives from multiple specific product classes, and each abstract factory class derives from multiple specific factory classes. Each factory class is responsible for creating an instance of a specific product;
Abstract Factory mode: Each abstract product derives from multiple specific product classes. Each Abstract Factory derives from multiple specific factory classes, and each factory is responsible for creating multiple (series) instances of specific products.



// Under the UIKit framework, two demos are implemented using the factory method and abstract factory design modes, if you do not understand how to implement the two design modes, you can download them here:


Abstract Factory: http://download.csdn.net/detail/luozhonglan/8008001

Factory method: http://download.csdn.net/detail/luozhonglan/8007973



References:

Factory: http://blog.csdn.net/z251257144/article/details/7476849

Factory method: http://blog.csdn.net/z251257144/article/details/7479183

Abstract Factory: http://blog.csdn.net/z251257144/article/details/7482810





The difference between the factory method mode and the abstract factory Mode

Factory method mode:
An abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.

Abstract Factory mode:
Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class.

Differences:
The factory method mode has only one abstract product class, while the abstract factory mode has multiple.
The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.

What are the differences between the factory model, simple factory model, and abstract factory model?

The advantage of the factory model is that the coupling between the factory and the product is reduced, and the construction process of the specific product is placed in the specific factory class. In the future, it will be much more convenient to expand Products. You only need to add a factory class and a product class to easily add products without modifying the original code. In a simple factory, if you want to add a product, you need to modify the factory class, add the if/else branch, or add a case Branch, the factory mode complies with the OCP principle (open close principle) in software development and is open to extensions and closed to modifications.
Abstract Factory mode: I always feel very similar to the builder mode.
The factory method mode provides a hierarchical model for a product, while the Abstract Factory method provides a hierarchical model for multiple products. Note, multiple specific products are coupled with each other. That is to say, there is a certain relationship between the products provided by the abstract factory here.
Some people make the following comparison:
Factory method mode: an abstract product class that can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.
Abstract Factory mode: Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class.
Difference: the factory method mode has only one abstract product class, while the abstract factory mode has multiple.
The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.
The following is an image metaphor:
Both simple factory mode, factory mode, and abstract factory mode extract unchanged parts and save the variable parts as interfaces to maximize reuse. Take a factory that produces a cup as an example: At first, there is no need for a factory model. before producing a cup, I must know all the characteristics of the material and shape of the cup before it can be produced, this is our new Cup (); this Cup must be specific. The manufacturer finds that the quilt in the same shape has different materials, such as glass and porcelain, but two production lines are required, apparently there is a waste of resources. Now, when the manufacturer produces a cup, it will not let the production line know whether it is made of glass or porcelain. Instead, it will let it do what it can do without knowing the specific material, after it completes the mold, you only need to fill the glass raw materials or porcelain raw materials to create a specific cup of the same shape. But unfortunately, java cannot create an abstract Cup, so there is a simple factory model. It turned out to be Cup cup = new Cup; now it is SimpleCupFactory. createCup (String cupName). The cup is produced according to the Cup name, And createCup returns a specific Cup that implements the Cup interface or abstract class. One problem with the simple abstract factory model is that when I want to produce an iron cup of the same shape, the corresponding processing process is not defined in the factory, and the createCup method can only be changed, this is unreasonable. Now I just want to make an iron cup. You just need to replace the raw glass with iron at the end. Why should you change the entire production line? So we have the factory model. The original production line should also consider whether the mold is made for the glass or the mold made for the iron cup when producing the mold. Now it is no longer necessary. CupFactory. createCup () creates Cup. CupFactory as an interface or abstract class. The specific sub-classes that implement it will create a specific Cup that meets the Cup interface. Now, if the manufacturer wants to produce kettle, it will have to re-create a production line of kettle in the factory mode. Can the factory produce kettle at the same time? This is the abstract factory model.

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.