PHP design mode-Abstract Factory mode

Source: Internet
Author: User
Tags abstract define definition interface return sleep
Under what circumstances should the abstract factory pattern be used: 1, a system should not rely on the details of how a product class instance is created, assembled, and expressed, which is important for all forms of the factory model. 2, the system's products have an excess of the product family, and the system consumes only one of the products of a family. 3. Products belonging to the same product family are used together, this constraint must be embodied in the design of the system. 4, the system provides a library of product classes, all products with the same interface, so that the client does not rely on implementation.     Case 1: Take the farm as an example. Our farm is divided into a number of product lines, one is dedicated to sell vegetables and fruits in the north. A vegetable and fruit specially sold in the south. We can try not to write, here is not to show examples, UML class diagram you can try to draw.     Case 2: Computer For example computer is divided into PC computer, Mac computer. The PC computer has the CPU, motherboard, and keyboard mac that he needs to assemble, but the models are different, and here is the UML class diagram for this example.           code example:   This example is based on Nu WA made people and wrote.   Copy Code   1 <?php   2    3//This example is based on Nu WA made people write   4    5///First define a global factory interface   6//by Yin Rope Yang to inherit all to achieve lifting rope This method   7 interface nvwarope{  8     Public Function liftpeoplerope ($name);   9 &N Bsp   Public Function Liftanimalrope ($name); &NBSP;10}  11   12//define the Yin string to inherit Nvwarope  13 class Yinrope implements nvwarope{ 14     Public Function Liftpeoplerope ($name) { 15         return new Girl ($name);  16     } &NBsp;17   18     Public Function Liftanimalrope ($name) { 19         return new Muanimal ($name);  20    }  21}  22   23//Definition Yang   &NBSP;24 class Yangrope implements nvwarope{&nbs P;25     Public Function Liftpeoplerope ($name) { 26         return new boy ($name); &nbs p;27    }  28   29     Public Function Liftanimalrope ($name) { 30     & nbsp   return new Gonganimal ($name);  31    }  32}  33   34   35//define a person's abstract interface  36 interface people{ 7     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       &N Bsp Echo $this->name;  55         return $this->name;  56    }  57   58    /check for chest  59     Public Function checkchest ( { 60         return $this->havechest;  61    }  62}  63  &nb Sp;64 class Boy extends abstractpeople{ 65     Public $haveChest = false;//boy has no chest  66}  67&nbsp ;  68 class Girl extends abstractpeople{ 69     Public $haveChest = true;//girl has chest  70}  71&nb Sp  72   73//define an animal's abstract interface  74 interface animal{ 75     public function eat (); &NBsp;76     Public Function sleep (); &NBSP;77}  78   79 abstract class Abstractanimal implements animal{ 80     private $name;  81   82     Public function __construct ($name) { 83         $this->n ame = $name;  84    }  85   86     public Function eat () { 87         Echo $this->name. "Eating";  88    }  89   90     public Function sleep () { 91       &NBS P Echo $this->name. "Sleeping";  92    }  93   94     Public Function getName () { 95       &N Bsp Echo $this->name;  96         return $this->name;  97    }  98}  99  100//define the class of a parent animal Muanimal extends abstractanimal{102   & nbsp Public $xingbie = "Mu"; 105&NB} 104 Sp 106//Definition of a male animal class Gonganimal extends abstractanimal{108     Public $xingbie = "Gong"; 109} 110  1 11  112  113//Use 114  115//First get the example of the factory is the example of the yin rope or yang rope 116 $yinRope = new Yinrope (); 117  118 $people = $yinRope->liftpeoplerope ("Zhangsan"); 119 echo $people->eat (); echo $people->getname (); 121 Var_dump ($people->checkchest ()); 122  123 $animel = $yinRope->liftanimalrope ("Horse"); 124 echo $animel->eat ();

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.