[Design mode] study NOTE 4: simple factory)

Source: Internet
Author: User

From http://blog.csdn.net/shuangde800


I have learned "programming for interfaces rather than implementing programming" before. However, when "new" is used, a specific class is indeed instantiated, so the specific implementation is used, instead of interfaces.

Duck duck = new MallardDuck( );

The above is a common new method for instantiation.


What's wrong with "new?

Technically, "new" is correct. It is a "change" error.

For interface programming, you can isolate a lot of changes that may occur in the system in the future. If it is written for an interface, it can implement this interface with any new class through polymorphism.

However, when a large number of specific classes are used, it is difficult because the Code must be changed once a new specific class is added. That is to say, your code is not "Closed for modification ". The new class must be re-opened.



Understanding the simple factory Model

Suppose we want to open a pizza store and get many different types of pizzas, we can write this method to get the pizza objects:

Pizza orderPizza(String type){    Pizza pizza;    if(type.equals("chees")){        pizza = new CheesePizza();    }else if(type.equals("greek")){        pizza = new GreekPizza();     }else if(type.equals("pepperoni")){        pizza = new PepperoniPizza();    }    pizza.prepare();    pizza.bake();    pizza.cut();    pizza.box();    return pizza;}


When the PISA type is changed or deleted, the above Code will be changed, not in line with the design principles.


Therefore, we move the created object out of orderpizza () and create a pizza with this object.


Simplepizzafactory only does one thing: Create a pizza for its customers.

public class SimplePizzaFactory {public Pizza createPizza(String type) {Pizza pizza = null;if (type.equals("cheese")) {pizza = new CheesePizza();} else if (type.equals("pepperoni")) {pizza = new PepperoniPizza();} else if (type.equals("clam")) {pizza = new ClamPizza();} else if (type.equals("veggie")) {pizza = new VeggiePizza();}return pizza;}}

The above object is the factory, which processes the details of the created object.


Redo the pizza store below:


Pizzastore class, using simplepizzafactory object to create a pizza

Public class pizzastore {simplepizzafactory factory; Public pizzastore (simplepizzafactory factory) {This. factory = factory;} public pizza orderpizza (string type) {pizza; pizza = factory. createpizza (type); // note that the new method is used to create a factory! No longer use specific instantiation! Pizza. Prepare (); pizza. Bake (); pizza. Cut (); pizza. Box (); Return pizza ;}}




Define simple factory Mode

Simple factory is not a design pattern, but a programming habit, but it is often used.

Simple factory pattern is an innovative class mode, also known as static factorymethod pattern. It is used to create instances of other classes by defining a specific class, the created instance usually has a common parent class.


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.