, this also has some disadvantages. You cannot use inheritance to change the creation method behavior.
Redo the PizzaStore class
Public class PizzaStore {// Add a reference to SimplePizzaFactory factory to PizzaStore; // The constructor of PizzaStore. A factory is required as the parameter public
implementation details are inseparable from this programming habit.
Two factory models
The factory pattern defines an interface for creating objects, but subclasses decide which class to instantiate. The factory method defers the instantiation of the class to the subclass.
It is also an example of pizza, because people in different regions have different tastes, the same chesspizza in different areas, such as New York's Pizzastore offers pan
(the batch store specializes in producing batch stores, but it cannot provide batch stores by itself and needs to be called by operators)public class PizzaStore { SimplePizzaFactory factory; public PizzaStore(SimplePizzaFactory factory) { this.factory = factory; } public Pizza orderPizza(String type) { Pizza pizza; pizza = factory.createPizza(type); pizza.prepare();
= null; if ("cheese".equals(type)) { pizza = new CheesePizza(); } else if ("greek".equals(type)) { pizza = new GreekPizza(); } else if ("clam".equals(type)) { pizza = new ClamPizza(); } return pizza; }}
Pizzastore class (client ):
public class PizzaStore { public Pizza orderPizza(String type) { Pizza pizza = SimplePizzaFactory
Factory Method Mode
English name
Factory Method Pattern
definition
Defines an interface for creating objects, but subclasses decide which class to instantiate, and the factory method pattern allows the class to defer instantiation to subclasses.
Principles
1. A variable cannot hold a reference to a specific class
2. Do not let classes derive from specific classes
3. Do not overwrite the implemented methods in the base class
Understanding
1. An abstract method is defined in an abstract inter
at the same time. at this time, multiple specific factory classes need to be provided accordingly, and each specific factory is responsible for producing the corresponding specific product.As shown in the activity sequence of the factory method mode, the Client first creates the ConcreteCreator object, and then calls the Factory method factoryMethod () of the ConcreteCreator object to "produce" the required ConcreteProduct object.
Here is a specific case:If you open a Pizza store (
sauce";
Toppings.add ("shredded mozzarella Cheese");
} void Cut () {System.out.println ("cutting the pizza into square slices"); }
}
3. Creator parent Class (creator), the specific use method, the method that invokes the Product class parent (product), provides a different specific product class (concrete product) based on the parameters.
Code:
/**
* @time May 26, 2014 * * *
package factory;
/**
* @author C.l.wang
*
*
/Public abstract class
abstract classes, and the abstract class has many concrete subclasses, each of which has its own specific implementation.
The test procedure is as follows:
Report zbo_dp_004_re.
* Factory method pattern used interface and class
Include zbo_dp_004_if_cl.
* This data used to create two type of concrete creator
Data:
Ny_ref type ref to pizzastore,
Chi_ref type ref to pizzastore.
* The different pizza have the
;
Factory method modeCopy codeThe Code is as follows: Abstract class PizzaStore {Public function orderPizza ($ type ){$ Pizza = $ this-> createPizza ($ type );
$ Pizza-> prepare ();$ Pizza-> bake ();$ Pizza-> cut ();$ Pizza-> box ();Return $ pizza;}
Public abstract function createPizza ($ type );}Class NYPizzaStore extends PizzaStore {Public function createPizza ($ type ){If ($ type = "cheese "){Return new
() {System.out.println ("cutting the pizza into square slices");}}3. Creator parent Class (creator), detailed usage, invokes the method of the Product class parent class (Products), providing a different detailed product class (concrete products) based on the parameters.Code:/** * @time May 26, 2014 */package factory;/** * @author C.l.wang * */public abstract class Pizzastore {public Pizza Orderpi Zza (String Item) {Pizza Pizza;pizza = Createpizza (i
__construct () {$this->fly_obj = new Flynoway ();$this->quack_obj = new Mutequack ();}Public Function display () {echo "I ' m a model duck!\n";}}$model = new Modelduck ();$model->performfly ();$model->performquack ();To provide new capabilities$model->setflybehavior (New flyrocketpowered ());$model->setquackbehavior (New Squeak ());$model->performfly ();$model->performquack ();?>
Single-piece mode
Copy the Code code as follows:
/*** Single-piece mode* Ensure that a class has only one insta
();
Var_dump ($myClass);
?>
Factory method Mode
Copy CodeThe code is as follows:
Abstract class Pizzastore {
Public Function Orderpizza ($type) {
$pizza = $this->createpizza ($type);
$pizza->prepare ();
$pizza->bake ();
$pizza->cut ();
$pizza->box ();
return $pizza;
}
public abstract function Createpizza ($type);
}
Class Nypizzastore extends Pizzastore {
Public Function Createpizza ($type) {
if ($typ
method modeCopy codeThe Code is as follows:Abstract class PizzaStore {Public function orderPizza ($ type ){$ Pizza = $ this-> createPizza ($ type );$ Pizza-> prepare ();$ Pizza-> bake ();$ Pizza-> cut ();$ Pizza-> box ();Return $ pizza;}Public abstract function createPizza ($ type );}Class NYPizzaStore extends PizzaStore {Public function createPizza ($ type ){If ($ type = "cheese "){Return new NYStyleChees
From: http://blog.csdn.net/ericzhong83/article/details/7604728 factory method Mode (Factory pattern):Defines an interface to create an object, but subclasses decide which of the classes to instantiate, that is, to create objects from subclasses.Principle:To rely on abstraction, do not rely on specific classes.Case:First explain what the factory is:If you open a pizza shop (Pizzastore abstract class) sell various flavors of pizza (pizza subclass), then
05 _ factory mode, 05 factory Mode
Factory Model
1. Simple Factory: another class or object is usually used to encapsulate the instantiation operation.
2. Factory mode: The real factory mode is to define an abstract factory method and postpone the Instantiation to the subclass! (Interfaces play a key role !)
Here is a simple example to illustrate how to use the factory model: Suppose you need to open several Pizza stores. Each Pizza store has several different types of Pizza for customers to c
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->// Simple factory Mode
Public abstract class pizza
{}
Public class pizzaA: pizza
{}
Public class pizzaB: pizza
{}
// Currently, this pizza can serve many customers who need pizza Information
Public class simplePizzaFactory
{
Public pizza createPizza (string type)
{
Pizza p = null;
If (type = "")
{
P = new pizzaA ();
}
Else if (type = "B ")
{
P = new pizzaB ();
}
Return p;
}
}
Public class
-world applications, it is common to have multiple specific product classes at the same time, where the corresponding need to provide a number of specific factory classes, each specific factory is responsible for the production of specific products.The activity sequence of the factory method pattern, as shown, is that the client clients first create the Concretecreator object and then call the factory method of the Concretecreator object FactoryMethod (), which is responsible for "production" Th
Factory methodThe factory method uses inheritance. The object creation is delegated to the subclass, And the subclass determines which one to instantiate.Put the same method together.Abstract Product factoryMethod (String type)Factory methods are abstract;A product object must be returned;You must input a type to determine which object to create;This method is implemented after the class is inherited.Example of modifying the factory method in the workshop method mode:Put createPizza back in
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.