HeadFirst design mode Note: (4) Factory mode-the essence of baking OO

Source: Internet
Author: User
Identify changing aspects

Assuming you have a pizzeria, your code might be written like this:

Pizza orderPizza () {
Pizza pizza = new Pizza (); // In order to make the system flexible, we very much hope this is an abstract class or interface. But if so, these classes or interfaces cannot be instantiated directly.
The
pizza.prepare ();
pizza.bake ();
pizza.cut ();
pizza.box ();
The
return pizza;
}
But you need more pizza types ...
Therefore, some code must be added to "decide" the appropriate pizza type, and then "make" this pizza:
Pizza orderPizza (String type) {// Now pass the pizza type to orderPizza ().
Pizza pizza = new Pizza ();
The
// According to the type of pizza, we instantiate the correct concrete class and then assign it to the pizza instance variable. Note that any pizza here must implement the Pizza interface.
if (type.equals ("cheese")) {
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 ();
The
return pizza;
}
But the pressure comes from adding more pizza types
Pizza orderPizza (String type) {
Pizza pizza = new Pizza ();
The
// This is the changing part. Over time, the pizza menu has changed, and it has to be changed over and over again. ↓↓↓
if (type.equals ("cheese")) {
pizza = new CheesePizza ();
} / * else if (type.equals ("greek")) {// delete some pizzas
pizza = new GreekPizza ();
} * / else if (type.equals (pepperoni)) {
pizza = new PepperoniPizza ();
}
// Add some pizzas
else if (type.equals ("clam")) {
pizza = new ClamPizza ();
} else if (type.equals ("veggie")) {
pizza = new VeggiePizza ();
}
// This is the changing part. Over time, the pizza menu has changed, and it has to be changed over and over again. ↑↑↑
The
// This is where we don't want to change. ↓↓↓
pizza.prepare ();
pizza.bake ();
pizza.cut ();
pizza.box ();
// This is where we don't want to change. ↑↑↑
The
return pizza;
}
Obviously, if you instantiate "some" concrete classes, it will make orderPizza () go wrong, and you won't be able to make orderPizza () close to the modification; however, we now know which ones will change and which will not. It's time to use encapsulation.
Encapsulate the code that creates the object

To move the code for creating pizza to another object, this new object creates pizza full-time.

Pizza orderPizza (String type) {
Pizza pizza = new Pizza ();
The
// Extract the code for creating the object from orderPizza (). Then move this part of the code to another object, this new object is just how to create pizza.
/ * if (type.equals ("cheese")) {
pizza = new CheesePizza ();
} else if (type.equals ("greek"))
pizza = new GreekPizza ();
} else if (type.equals (pepperoni)) {
pizza = new PepperoniPizza ();
} else if (type.equals ("clam")) {
pizza = new ClamPizza ();
} else if (type.equals ("veggie")) {
pizza = new VeggiePizza ();
}
* /
pizza.prepare ();
pizza.bake ();
pizza.cut ();
pizza.box ();
The
return pizza;
}
We call this new object "factory".
Build a simple pizza factory

// Do only one thing and help its customers create pizza.
public class SimplePizzaFactory {

// First, define a createPizza () method in this factory. All clients use this method to instantiate new objects.
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;
}
}
Redo PizzaStore class
public class PizzaStore {
// Add a reference to SimplePizzaFactory for PizzaStore.
SimplePizzaFactory factory;
 
// The constructor of PizzaStore requires a factory as a parameter.
public PizzaStore (SimplePizzaFactory factory) {
this.factory = factory;
}
 
public Pizza orderPizza (String type) {
Pizza pizza;
// The orderPizza () method uses a factory to create a pizza by simply passing in the order type.
pizza = factory.createPizza (type);
 
pizza.prepare ();
pizza.bake ();
pizza.cut ();
pizza.box ();

return pizza;
}

}
Define a simple factory
Simple factory is not actually a design pattern, but more like a habit. Look at the class diagram of the pizzeria.
Related Article

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.