Simple Factory mode of design mode Factory (four-created)

Source: Internet
Author: User

    1. Introduction to Factory mode.

      The factory model is dedicated to instantiating a large number of classes with common interfaces

      Factory mode can dynamically decide which class to instantiate, without knowing in advance which class to instantiate each time.

      There are three types of factory models:

      1, simple factory model Factory, also known as the Static factory method mode

      2, the factory method mode factory methods, but also multiprocessing the state of the Factory mode

      3, Abstract factory model Factory, also known as the Toolbox mode

2. What is simple Factory mode

The Simple Factory mode is the creation mode of the class. is determined by a factory object to create an instance of which product class, which is a special implementation of different factory method patterns. A factory object determines which instance of the product class is created, and is responsible for instantiating a large number of classes that have common interfaces.

3. usage Examples

A farm company that specializes in selling all kinds of fruit to the market. The following fruit needs to be described in the system:

1 Apples apple

2 Grape Grape

3 Strawberry Strawberry

Unlike other plants, the fruit can be used to create a variety of fruit interfaces that are suitable for all fruits, such as planting plant (), growing Grow (), and Harvesting harvest ().

The class diagram is as follows:

The specific implementation classes are as follows:

Fruit class:

/*** @FileName fruit.java* @Package com.ali.pattern.factory.simplefactory* @Description todo[what the file to do]* @Auth or Ali Blog:Http://www.cnblogs.com/accipiter* @Date January 19, 2016 afternoon 8:15:49* @Version V1.0.1*/ Packagecom.ali.pattern.factory.simplefactory;/*** @ClassName Fruit * @Description A variety of fruits are suitable for fruit interface, the interface of the fruit must have a method * @Date pm 8:15:49*/ Public InterfaceFruit { Public voidplant ();  Public voidgrow ();  Public voidHarverst ();}

Fruitgardener class:

/*** @FileName fruitgardener.java* @Package com.ali.pattern.factory.simplefactory* @Description todo[what the file to do]* @Author Ali Blog:Http://www.cnblogs.com/accipiter* @Date January 19, 2016 afternoon 8:32:34* @Version V1.0.1*/ Packagecom.ali.pattern.factory.simplefactory;Importcom.ali.pattern.factory.simplefactory.impl.Apple;ImportCom.ali.pattern.factory.simplefactory.impl.Grape;ImportCom.ali.pattern.factory.simplefactory.impl.Strawberry;/*** @ClassName Fruitgardener * @Description TODO * @Date pm 8:32:34*/ Public classFruitgardener { Public StaticFruit Factory (String which) {if(Which.equals ("Apple")){            return NewApple (); }Else if(Which.equals ("Grape")){            return NewGrape (); }Else if(Which.equals ("Strawberry")){            return NewStrawberry (); }Else{            return NULL; }    }}

Apple class:

/*** @FileName apple.java* @Package com.ali.pattern.factory.simplefactory.impl* @Description todo[what the file to do]* @Author Ali Blog:Http://www.cnblogs.com/accipiter* @Date January 19, 2016 afternoon 8:25:14* @Version V1.0.1*/ PackageCom.ali.pattern.factory.simplefactory.impl;ImportCom.ali.pattern.factory.simplefactory.Fruit;/*** @ClassName Apple * @Description TODO * @Date pm 8:25:14*/ Public classAppleImplementsFruit {@Override Public voidplant () {System.out.println ("Plant Apple."); } @Override Public voidgrow () {System.out.println ("Grow apple."); } @Override Public voidHarverst () {System.out.println ("Harverst Apple."); }}

4, the Simple Factory model features:

In the simple Factory mode, you can return instances of different classes depending on the parameters. The simple factory model specifically defines a class to be responsible for creating instances of other classes, and the instances that are created typically have a common parent class.

Separating object creation from the business process of the object itself can reduce the coupling of the system, making both of them relatively easy to modify.

When calling the factory method of plant class, because the factory method is static method, it is convenient to use, can be called directly through the class name, and only need to pass in a simple parameter, in the actual development, you can also save the parameters passed in XML format in the landlord file, when called. There is no need to modify any Java source code when modifying parameters.

The biggest problem with the simple factory model is that the responsibilities of the factory class are relatively heavy, and adding new products requires modifying the judgment logic of the factory class, which is contrary to the open and closed principle.

The point of the simple factory model is that when you need something, just pass in the correct parameter, and you get the object you want without having to know its creation details.

Advantages:

1, the core of the model is the factory class, the class contains the necessary judgment logic, you can decide when to create an instance of which product class, the client can be exempted from the direct creation of product object responsibility, but only responsible for "consumption" products.

2. The simple factory model realizes the division of responsibility.

Disadvantages:

    1. As the factory class centralizes all product creation logic, the entire system is affected once it does not work properly.

2, the use of simple Factory mode will increase the number of classes in the system, in a certain program to increase the complexity of the system and the difficulty of understanding.

3, the system is difficult to expand, once added new products will have to modify the factory logic, in the product type is more, it is possible to cause the factory logic is too complex, not conducive to the expansion and maintenance of the system.

4, the simple Factory mode due to the use of static factory methods, resulting in the factory role can not form a hierarchy based on inheritance.

======== Blog Park Administrators do not seem to like the design pattern of the article, perhaps too much, but all of the study ====================================

Simple Factory mode of design mode Factory (four-created)

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.