Php design patterns-Abstract Factory patterns, design patterns Factory

Source: Internet
Author: User

Php design patterns-Abstract Factory patterns, design patterns Factory

Before learning this chapter, read the simple factory mode and factory method mode.

Under what circumstances should the abstract factory model be used:
1. A system should not depend on the details of how product instances are created, combined, and expressed. This is important for all factory models.
2. The product of this system has an additional product family, and the system only consumes products of one of them.
3. products belonging to the same product family are used together. This constraint must be reflected in the system design.
4. The system provides a product library. All products use the same interface, so that the client does not rely on implementation.


Case 1:
Take farm as an example.
Our farm is divided into multiple product lines. One is dedicated to selling vegetables and fruits from the north. A vegetable and fruit that specializes in selling vegetables and fruits in the south. You can try not to write it down. Here we will not give an example. You can try to draw a uml class diagram.


Case 2:
Take a computer as an Example
Computers are divided into PC computers and mac computers. The pc computer has the cpu, motherboard, and keyboard required for its assembly. | the mac computer also needs these components, but the models are different. The following is a uml class diagram of this example.

 

Sample Code:

This example is based on the creation of a girl.

1 <? Php 2 3 // This instance is written according to 4 5 by nvwa. // first, define a global factory interface. 6 // The factory interface must be inherited by the Yin rope. method 7 interface nvwaRope {8 public function liftPeopleRope ($ name ); 9 public function liftAnimalRope ($ name); 10} 11 12 // defines the Yin rope to inherit nvwaRope 13 class yinRope implements nvwaRope {14 public function liftPeopleRope ($ name) {15 return new girl ($ name); 16} 17 18 public function liftAnimalRope ($ name) {19 return new muAnimal ($ name ); 20} 21} 22 23 // defines the yangxian 24 class yangRope implements nvwaRope {25 public function liftPeopleRope ($ name) {26 return new boy ($ name ); 27} 28 29 public function liftAnimalRope ($ name) {30 return new gongAnimal ($ name ); 31} 32} 33 34 35 // define a person's abstract interface 36 interface people {37 public function eat (); 38 public function getName (); 39} 40 41 42 abstract class abstractPeople implements people {43 private $ name; 44 45 public function _ construct ($ name) {46 $ this-> name = $ name; 47} 48 49 public function eat () {50 echo $ this-> name. "eat"; 51} 52 53 public function getName () {54 echo $ this-> name; 55 return $ this-> name; 56} 57 58 // check whether there is a chest 59 public function checkChest () {60 return $ this-> haveChest; 61} 62} 63 64 class boy extends define actpeople {65 public $ haveChest = false; // boy has no chest 66} 67 68 class girl extends define actpeople {69 public $ haveChest = true; // girl breast 70} 71 72 73 // define an animal's abstract interface 74 interface animal {75 public function eat (); 76 public function sleep (); 77} 78 79 abstract class implements actanimal implements animal {80 private $ name; 81 82 public function _ construct ($ name) {83 $ this-> name = $ name; 84} 85 86 public function eat () {87 echo $ this-> name. "eating"; 88} 89 90 public function sleep () {91 echo $ this-> name. "sleeping"; 92} 93 94 public function getName () {95 echo $ this-> name; 96 return $ this-> name; 97} 98} 99 100 // defines the class 101 class muAnimal extends of a mother animal {102 public $ xingbie = "mu "; 103} 104 105 106 // define a public animal class 107 class gongAnimal extends actanimal {108 public $ xingbie = "gong "; 109} 110 111 112 113 // usage 114 115 // first obtain the factory instance, that is, the Yin or Yang rope instance 116 $ yinRope = new yinRope (); 117 118 $ people = $ yinRope-> liftPeopleRope ("zhangsan"); 119 echo $ people-> eat (); 120 echo $ people-> getName (); 121 var_dump ($ people-> checkChest (); 122 123 $ animel = $ yinRope-> liftAnimalRope ("Ma"); 124 echo $ animel-> eat ();

What are the benefits of the php design model?

If UserFactory is used, you do not need to know the existence of the User class. The abstract class helps you hide it. If there are more classes in the future, this mode will be easier to maintain, we recommend that you understand the abstract factory mode, factory mode, and factory method mode, all of which are for reusable programming.

The difference between the factory method mode and the abstract factory Mode

Factory method mode:
An abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
Each factory class can only create one instance of a specific product class.

Abstract Factory mode:
Multiple abstract product classes. Each abstract product class can be derived from multiple specific product classes.
An abstract factory class can be derived from multiple factory classes.
You can create multiple product instances for each specific factory class.

Differences:
The factory method mode has only one abstract product class, while the abstract factory mode has multiple.
The factory method mode can only create one instance for a specific product type, while the abstract factory mode can create multiple instances.

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.