iOS often uses design Patterns-factory methods (simple Factory mode, factory method mode, abstract Factory mode)

Source: Internet
Author: User

1. Simple Factory mode


How to understand simple factory, factory method. Abstract factory three design patterns?

A simple factory life scene. A small vendor selling breakfast. He gives you buns, steamed buns, gutter oil pancakes and so on, Hawker is a factory. It produces buns, steamed buns, and gutter oil-branded pancakes.

The corresponding UML diagram for this scenario is seen in the following:


Figure 1: Simple Factory mode UML diagram


Participants in the Simple factory model:

Factory ( Factory role: Accepts client requests and is responsible for creating the corresponding product objects through the request.

Abstract Products ( Abstract Product ) Role : is the parent or common interface of the object created by the Factory mode. But abstract classes or interfaces.

Detailed Products ( concreteproduct object: The object created by Factory mode is an instance of this role.

The evolution of the Simple factory model:

1.) when there is only one product in the system, you can omit the abstract product and1 See it. Such Factory roles and detailed products can be combined.

Advantages and disadvantages of the simple factory model:

1.) The factory class contains the necessary logic to create the product. In this way, the client simply needs to request the product, without having to heed the details of the product's implementation.

2.) The factory class has only one, which concentrates the logic of all product creation. It will be the bottleneck of the whole system, which makes the system difficult to expand at the same time.

3.) Simple Factory mode typically uses a static factory approach, which makes it impossible for a factory class to inherit from subclasses, which makes it impossible for a factory role to form an inheritance-based hierarchy.


2. Factory Method Mode

Factory Method Use OOP Polymorphism , the factory and the product are abstracted out of a base class, and a unified interface is defined in the base class. Then create the detailed product in the detailed factory. Factory method of life scene, Unilever to produce "Hazeline" and "Qingyang " two shampoo, it will build a production "Hazeline " of the factory and a production of "Qingyang " 's factory.


Figure 2: UML Diagram of the factory method

Participants in the factory method model:

Abstract Factory Role: regardless of the application, any factory that creates objects in the schema must implement this interface.


Detailed factory role: implements a detailed class of abstract factory interfaces. Contains logic that is closely related to a reference and is called by the application to create a Product object.
Abstract Product Roles: The super type of the product object created by the factory method, which is the common parent or shared interface of the Product object.
Detailed Product roles: This role implements an interface that is known to abstract product roles. Each detailed product object created by a factory method is an instance of a detailed product role.

Advantages and disadvantages of the factory approach:

1.)reduced cohesion in the plant class. It satisfies the hierarchical relationship between classes, and conforms to the single responsibility principle in object-oriented design very well. This facilitates the expansion of the program. Three of what you see:

Figure 3: Extension of the factory method UML diagram

Summary: put " Commonality " extracted, based on the respective " Personality " The realization of establishing their respective inheritance commonalities


3. Abstract Factory design pattern

The so-called abstract factory refers to a factory hierarchy that can create all the objects in a product family that belong to different product hierarchies. For example, create Unix controls and Windows controls. We need an abstract factory. Below there are two sub-factories, one called unixfactory, for the production of Unix -family controls, one called Winfactoryfor the production of Win - family controls. The difference between an abstract factory and a factory method is that a detailed factory in a factory method typically produces only one or several control objects, whereas a detailed factory in an abstract factory produces a family of control objects.

4 of what you see.


Figure 4: Abstract Factory design Pattern UML diagram


The participants in the abstract factory:

Abstract Factory ( Abstract Factory ) Role: This role is at the heart of the factory approach model, which is independent of the business logic of the application system.

Detailed Factory ( Concrete Factory ) Role: This role creates an instance of the product directly under the call of the client. This role contains the logic to choose the right product object, which is closely related to the business logic of the application system.

Abstract Products ( Abstract Product ) Role: The classes that assume this role are the parent classes of objects created by the factory method pattern, or the interfaces they share.

Detailed Products ( Concrete Product ) Role: The abstract factory pattern creates an instance of a detailed product class, regardless of the product object.

This is what the client finally needs. Its interior must be filled with the business logic of the application system.

Usage scenarios for abstract factories:

    • A system should not rely on the details of how a product class instance is created, combined, and expressed, which is important for the full-form factory model.
    • The system has more than one product family, while the system consumes only one product family.
    • Products belonging to the same product family are used together, and this constraint must be reflected in the design of the system.
    • The system provides a library of product classes, with all products appearing on the same interface. So that the client does not depend on the implementation.

The difference between the abstract factory model and the factory method model

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



Under the Uikit framework, we have implemented two demo models using factory methods and abstract factory design patterns, and do not understand how the two design patterns can be implemented by a friend to download here:


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

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



Articles:

Simple 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




iOS often uses design Patterns-factory methods (simple Factory mode, factory method mode, 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.