HeadFirst design mode is a good book to explain the design mode. it is not as boring as F4. it should be easy to understand. Examples in the book are easy to understand, but because they are written by foreign experts, the habits of the examples are not very relevant to Chinese characteristics, and may occasionally look awkward, and the language habits are not Chinese style. Of course, after reading this book, you will be able to deeply understand what problems the design pattern can solve for you and what problems it cannot solve for you (for example, it cannot replace your encoding ).
I changed some of the code in the book to PHP. it is easier to understand the concept of code combination.
Rule mode
The code is as follows:
/**
* Rule mode
* Defines algorithm families and encapsulates them separately so that they can be replaced with each other,
* This mode makes the algorithm changes independent of the customers who use the algorithm.
*/
// Flight behavior interface
Interface FlyBehavior {
Public function fly ();
}
// Call the behavior interface
Interface QuackBehavior {
Public function quack ();
}
// Wings flight
Class FlyWithWings implements FlyBehavior {
Public function fly (){
Echo "I'm flying !! \ N ";
}
}
// Not fly
Class FlyNoWay implements FlyBehavior {
Public function fly (){
Echo "I can't fly! \ N ";
}
}
Class FlyRocketPowered implements FlyBehavior {
Public function fly (){
Echo "I'm flying with a rocket! \ N ";
}
}
Class Qquack implements QuackBehavior {
Public function quack (){
Echo "Quack \ n ";
}
}
Class Squeak implements QuackBehavior {
Public function quack (){
Echo "Squeak \ n ";
}
}
Class MuteQuack implements QuackBehavior {
Public function quack (){
Echo "<Silence> \ n ";
}
}
Abstract class Duck {
Protected $ quack_obj;
Protected $ fly_obj;
Public abstract function display ();
Public function extends mquack (){
$ This-> quack_obj-> quack ();
}
Public function implements mfly (){
$ This-> fly_obj-> fly ();
}
Public function swim (){
Echo "All ducks float, even decoys! \ N ";
}
Public function setFlyBehavior (FlyBehavior $ fb ){
$ This-> fly_obj = $ fb;
}
Public function setQuackBehavior (QuackBehavior $ qb ){
$ This-> quack_obj = $ qb;
}
}
Class ModelDuck extends Duck {
Public function _ 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-> mongomfly ();
$ Model-> queue mquack ();
// Provide new capabilities
$ Model-> setFlyBehavior (new FlyRocketPowered ());
$ Model-> setQuackBehavior (new Squeak ());
$ Model-> mongomfly ();
$ Model-> queue mquack ();
?>
Single-piece mode
The code is as follows:
/**
* Single-piece mode
* Make sure that a class has only one instance and provides a global access point.
*/
Class MyClass {
Private static $ uniqueInstance;
Private function _ construct (){
Factory method mode
The code is as follows:
Abstract class PizzaStore {
Public function orderPizza ($ type ){
$ Pizza = $ this-> createPizza ($ type );
Public abstract function createPizza ($ type );
}
Class NYPizzaStore extends PizzaStore {
Public function createPizza ($ type ){
If ($ type = "cheese "){
Return new NYStyleCheesePizza ();
} Elseif ($ type = "veggie "){
Return new NYStyleVeggiePizza ();
} Elseif ($ type = "clam "){
Return new NYStyleClamPizza ();
} Elseif ($ type = "papperoni "){
Return new NYStylePapperoniPizza ();
} Else {
Return null;
}
}
}
Class ChicagoPizzaStore extends PizzaStore {
Public function createPizza ($ type ){
If ($ type = "cheese "){
Return new chicagstylecheesepizza ();
} Elseif ($ type = "veggie "){
Return new chicagstyleveggiepizza ();
} Elseif ($ type = "clam "){
Return new chicagstyleclampizza ();
} Elseif ($ type = "papperoni "){
Return new chicagstylepapperonipizza ();
} Else {
Return null;
}
}
}
Abstract class Pizza {
Public $ name;
Public $ dough;
Public $ sauce;
Public $ toppings = array ();
Public function prepare (){
Echo "Preparing". $ this-> name. "\ n ";
Echo "Yossing dough... \ n ";
Echo "Adding sauce... \ n ";
Echo "Adding toppings: \ n ";
For ($ I = 0; $ I <count ($ this-> toppings); $ I ++ ){
Echo "". $ this-> toppings [$ I]. "\ n ";
}
}
Public function bake (){
Echo "Bake for 25 minutes at 350 \ n ";
}
Public function cut (){
Echo "Cutting the pizza into diagonal slices \ n ";
}
Public function box (){
Echo "Place pizza in official PizzaStore box \ n ";
}
Public function getName (){
Return $ this-> name;
}
}
Class NYStyleCheesePizza extends Pizza {
Public function _ construct (){
$ This-> name = "NY Style Sauce and cheese Pizza ";
$ This-> dough = "Thin Crust Dough ";
$ This-> sauce = "Marinara Sauce ";
Public abstract function createPizza ($ type );
}
Class NYPizzaStore extends PizzaStore {
Public function createPizza ($ type ){
$ Pizza = null;
$ IngredientFactory = new NYPizzaIngredientFactory ();
If ($ type = "cheese "){
$ Pizza = new CheesePizza ($ ingredientFactory );
$ Pizza-> setName ('New York Style Cheese pizza ');
} Elseif ($ type = "veggie "){
$ Pizza = new VeggiePizza ($ ingredientFactory );
$ Pizza-> setName ('New York Style Veggie pizza ');
} Elseif ($ type = "clam "){
$ Pizza = new ClamPizza ($ ingredientFactory );
$ Pizza-> setName ('New York Style Clam pizza ');
} Elseif ($ type = "papperoni "){
$ Pizza = new PapperoniPizza ($ ingredientFactory );
$ Pizza-> setName ('New York Style Papperoni pizza ');
}
Return $ pizza;
}
}
Class ChicagoPizzaStore extends PizzaStore {
Public function createPizza ($ type ){
If ($ type = "cheese "){
Return new chicagstylecheesepizza ();
} Elseif ($ type = "veggie "){
Return new chicagstyleveggiepizza ();
} Elseif ($ type = "clam "){
Return new chicagstyleclampizza ();
} Elseif ($ type = "papperoni "){
Return new chicagstylepapperonipizza ();
} Else {
Return null;
}
}
}
Interface PizzaIngredientFactory {
Public function createDough ();
Public function createSauce ();
Public function createCheese ();
Public function createVeggies ();
Public function createPepperoni ();
Public function createClam ();
}
Class NYPizzaIngredientFactory implements PizzaIngredientFactory {
Public function createDough (){
Return new ThinCrustDough ();
}
Public function createSauce (){
Return new MarinaraSauce ();
}
Public function createCheese (){
Return new ReggianoCheese ();
}
Public function createVeggies (){
$ Veggies = array (
New Garlic (),
New Onion (),
New Mushroom (),
New RedPepper (),
);
Return $ veggies;
}
Public function createPepperoni (){
Return new SlicedPepperoni ();
}
Public function createClam (){
Return new FreshClams ();
}
}
Class ChicagoPizzaIngredientFactory implements PizzaIngredientFactory {
Public function createDough (){
Return new ThickCrustDough ();
}
Public function createSauce (){
Return new PlumTomatoSauce ();
}
Public function createCheese (){
Return new Mozzarella ();
}
Public function createVeggies (){
$ Veggies = array (
New BlackOlives (),
New Spinach (),
New EggPlant (),
);
Return $ veggies;
}
Public function createPepperoni (){
Return new SlicedPepperoni ();
}
Public function createClam (){
Return new FrozenClams ();
}
}
Abstract class Pizza {
Public $ name;
Public $ dough;
Public $ sauce;
Public $ veggies = array ();
Public $ cheese;
Public $ pepperoni;
Public $ clam;
Public abstract function prepare ();
Public function bake (){
Echo "Bake for 25 minutes at 350 \ n ";
}
Public function cut (){
Echo "Cutting the pizza into diagonal slices \ n ";
}
Public function box (){
Echo "Place pizza in official PizzaStore box \ n ";
}
Public function getName (){
Return $ this-> name;
}
Public function setName ($ name ){
$ This-> name = $ name;
}
Public function _ toString (){
}
}
Class CheesePizza extends Pizza {
Public $ ingredientFactory;
Public function _ construct (PizzaIngredientFactory $ ingredientFactory ){
$ This-> ingredientFactory = $ ingredientFactory;
}
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.