Simple factory design mode--java design mode (i)

Source: Internet
Author: User

A concept:

The simple factory pattern is to dynamically create the corresponding class from a factory class based on the parameters.

Two cases

  We take the car as an example, in real life the car is just an abstract product, many types of cars are specific products, such as Mercedes-Benz, BMW, Porsche and so on (of course, there are various versions under each brand, this is not discussed, assuming that each brand corresponds to a car). The simple class diagram is as follows:

When we go to a mixed-type store and try to drive a car, the simplest and most straightforward way for a salesperson is to take out their best-selling car and let you test it. The simple code is as follows:

 Public class cartest {        publicstaticvoid  main (string[] args) {        new Benzcar ();        Car.driver ();    }}

If you want to test-drive a BMW, just like the code above, you will get the brand's car directly. However, when customers want to change the brand, they have to re-change the client code and then re-out of the corresponding car, so it is very troublesome.

However, the simple factory can solve this problem, all the car's brand statistics, we just pass the brand this parameter can create the corresponding car instance, so as to avoid the customer every time changing the time to change the code. As follows

1  Public classCarfactory {2     /**3 * @description Car factory method to create an instance of the vehicle according to the car brand type4      * @paramType Brand5      * @return6      * @authorYuanfy7 * @date August 5, 2017 morning 11:22:538      * @version1.09      */Ten      Public StaticCar Newcar (String type) { One         Switch(type) { A          Case"Benz": -             return NewBenzcar (); -          Case"BMW": the             return NewBenzcar (); -          Case"Porsche": -             return NewBenzcar (); -         default: +             return NewCar (); -         } +     } A } at  Public classCartest { -      -      Public Static voidMain (string[] args) { -Car car = Carfactory.newcar ("Benz"); - car.driver (); -     } in}

It is convenient to use a simple factory approach for this requirement. (Feel this example a little reluctant, I hope you can understand)

Three advantages and disadvantages

Advantages: Optimize the client code, clarify the respective responsibilities and authority, in which the factory class is the key to the whole model.

Cons: The factory class centralizes the creation logic of all instances, the classes it can create can only be considered beforehand, and if new classes need to be added, the factory class needs to be changed. 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 kind of 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;

 

Simple Factory design mode--java design mode (i)

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.