Simple pattern Mode

Source: Internet
Author: User

First, Simple Factory (Factory ) mode

The simple Factory pattern returns an instance of a class in several possible classes, based on the data supplied to it. Typically, the class it returns has a common parent class and a public method.

Simple Factory mode is not actually a member of the Gof 23 design patterns.

Second, Simple Factory (Factory ) mode role and Structure

Factory class role creator (Lightsimplefactory): The factory class creates a product object under direct control of the client (create method).

Abstract Product role Product (light): defines the parent class of objects created by simple factories or interfaces that they collectively own. Can be a class, abstract class, or interface.

Specific product role concreteproduct (Bulblight, tubelight): defines the object that the factory specifically processes.

Third, Simple Factory (Factory ) Pattern Evolution

1) Simple Factory model Evolution (i)

In addition to the above usage, in some cases simple factory can have abstract product role playing, an abstract product class is also a sub-class factory.

2) Simple Factory model Evolution (II.)

All three characters are merged: similar to singleton mode (Singleton), but different.

Four, a bit of a disadvantage

    • Advantages:

The factory class contains the necessary judgment logic to decide when to create an instance of the product class, and the client can dispense with the responsibility to create the product object directly, but only "consume" the product. The simple factory model achieves the separation of responsibilities through this approach.

    • Disadvantages:

When the product has complex multilayer hierarchy structure, the factory class only own, Status quo, is the disadvantage of the pattern. Because the factory class centralizes all product creation logic, the entire system is affected once it does not work properly.

At the same time, the system is difficult to expand, once adding new products will have to modify the factory logic, may cause the factory logic is too complex. In addition, the simple factory pattern typically uses a static factory approach, which makes it impossible to subclass inheritance, resulting in a factory role that cannot form an inheritance-based hierarchy

Source code:http://pan.baidu.com/s/1qWl8T2G Password: bbwx  

Simple pattern Mode

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.