A comprehensive example application of simple factory model--23 design pattern

Source: Internet
Author: User

The Simple Factory mode is a Factory mode, also called the Static factory method mode, but not one of the 23 gof design patterns. A simple factory model is a factory object that determines which product class instances are created. Simple Factory mode is the simplest and most practical mode in the factory model family, which can be understood as a special implementation of different factory patterns.

The essence of the simple factory model is that a factory class dynamically determines which product class (these product classes inherit from a parent class or interface) should be created, based on the parameters passed in. By specifically defining a class to be responsible for creating instances of other classes, the instances that are created typically have a common parent class.

The model contains roles and their responsibility factory (Creator) roles at the core of the simple factory pattern, which is responsible for implementing the internal logic of creating all instances. The method of creating a product class for a factory class can be called directly by the outside world to create the desired product object. Abstract product role the parent class of all objects created by simple Factory mode, which is responsible for describing common interfaces common to all instances. The concrete product role is the creation goal of the simple factory pattern, and all created objects are instances of a specific class that acts as the role. The advantage of the factory class is the key to the whole pattern. Contains the necessary logical judgments, based on the information given by the outside world, determines which specific class The object should be created. By using the factory class, outsiders can get rid of the embarrassing situation of directly creating specific product objects, just need to be responsible for "consumption" object. Without having to worry about how these objects are created and how they are organized. The respective responsibilities and rights are clarified, which facilitates the optimization of the whole software architecture. Disadvantage because the factory class centralizes all instances of the creation logic, violates the cohesion responsibility allocation principle, the whole creation logic is concentrated in a factory class, it can create the class can only be considered beforehand, if you need to add new classes, you need to change the factory class. As the number of specific product classes in the system increases, there may be a need for factory classes to create different instances based on different conditions. This judgment of the condition and the judgment of the specific product type staggered together, it is difficult to avoid the spread of the module function, the maintenance and expansion of the system is very unfavorable; these shortcomings have been overcome in the factory method model. Using the scene factory class is responsible for creating fewer objects; The customer knows only the parameters of the incoming factory class, and does not care about how to create the object (logic); Since simple factories can easily violate the principle of high cohesion responsibility, they are generally applied only in very simple cases.

A comprehensive example application of simple factory model--23 design pattern

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.