Design Pattern _ Creation Mode-factory method

Source: Internet
Author: User

Factory Methods (Factorymethod): The factory method pattern belongs to the class's creation pattern. In the factory method pattern, the parent class is responsible for defining the factory interface that creates the product object, and the subclass is responsible for generating the concrete object, which is done to defer the instantiation of the class to the subclass, which is the subclass that determines which class should be instantiated.

Abstract Product FactoryMethod(String type)

Abstract: Factory methods are abstracted, relying on subclasses to handle the creation of objects

Product: The factory method returns the method that is defined in the superclass, typically using the return value of the factory method

String: The factory method may require parameters to specify the desired product

The elements of the factory method are:

A) Creator class: An abstract creator class that defines an abstract creator class that defines an abstract method that enables subclasses to implement this method of manufacturing a product; The creator typically contains code that relies on abstract products, which are manufactured by subclasses, and creators do not need to really know which specific product to manufacture Multiple specific classes capable of producing products,

Because the product is implemented in a specific class, a specific class can create a product with its own characteristics.

B) Product Category: An abstract product, a number of specific products;


Factory method Pattern Application Scenario:

1. when a class does not know the class of the object it must create

2. when a class wants to specify the object it creates by its subclasses

3. when a class delegates the responsibility of creating an object to one of several helper subclasses, and you want that helper subclass to be a proxy, this information is localized

The factory approach is a bit more than a simple factory model:

A) factory methods are more in line with the open - closed principle than the simple factory model .

b) when the factory method pattern is implemented, the client needs to decide which factory to instantiate to implement the Operation class, the problem of choosing judgment is still there, that is, the factory method moves the internal logic judgment of the simple factory to the client code, wants to add the function, the simple factory is modifies the factory class, The factory method mode modifies the client.

An abstract product class derives from a number of specific product classes; An abstract factory class derives from a number of specific factory classes; Each specific factory class creates only one instance of a specific product class.

As an example of a pizzeria,Pizzastore is the Pizza Head office (parent category), and nypizzastore is the New York Pizza Branch (subclass)

The following is its UML diagram

Public abstract class Pizzastore {

Abstract Pizza Createpizza (String item);

Public Pizza Orderpizza (String type) {

Pizza Pizza = Createpizza (type);

SYSTEM.OUT.PRINTLN ("---Making a" + pizza.getname () + "---");

Pizza.prepare ();

Pizza.bake ();

Pizza.cut ();

Pizza.box ();

return pizza;

}

}

Factory method Mode: Pizza Honten There are two methods, one is to create the object's Createpizza, andCreatepizza () is an abstract method that only implements the specific type in the subclass.

public class Nypizzastore extends Pizzastore {

Pizza Createpizza (String item) {

if (item.equals ("cheese")) {

return new Nystylecheesepizza ();

} else if (Item.equals ("veggie")) {

return new Nystyleveggiepizza ();

} else if (Item.equals ("clam")) {

return new Nystyleclampizza ();

} else if (Item.equals ("pepperoni")) {

return new Nystylepepperonipizza ();

} else return null;

}

}

Factory method Pattern: Createpizza () must be implemented in a subclass because it is abstract in Pizzastore

The subclass determines which object to instantiate in its sole discretion.

Design Pattern _ Creation Mode-factory method

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.