Anyone who has bought a mobile phone on the internet knows that merchants usually use several packages for their mobile phones to allow customers to choose freely. now there is a store that sells mobile phones and three packages are available for all mobile phones, the standard package is 4999 yuan, bare metal is the standard package minus 200 yuan, the full set of configuration for the standard package plus 100 yuan, we will implement it using php. Anyone who has bought a mobile phone on the internet knows that merchants usually use several packages for their mobile phones to allow customers to choose freely. now there is a store that sells mobile phones and three packages are available for all mobile phones, the standard package is 4999 yuan, bare metal is the standard package minus 200 yuan, the full set of configuration for the standard package plus 100 yuan, we will implement it using php.
Info;} public function getPrice () {return $ this-> price;} class bareIphone extends iphone {private $ info = 'bare metal (mobile phone) '; public function getInfo () {return $ this-> info;} public function getPrice () {return parent: getPrice ()-200 ;}} class fullSetIphone extends iphone {private $ info = 'set (mobile phone + Power Supply + data cable + headset + mobile phone case + film + mobile power supply) '; public function getInfo () {return $ this-> info;} public function getPrice () {return parent: getPrice () + 100 ;}} class lumia920 extends mobile {private $ price = 4399; private $ info = 'official standard (mobile phone + Power + data cable + headset) '; public function getInfo () {return $ this-> info;} public function getPrice () {return $ this-> price ;}} class bareLumia920 extends lumia920 {private $ info = 'bare metal (mobile phone) '; public function getInfo () {return $ this-> info ;} public function getPrice () {return parent: getPrice ()-200 ;}} class fullSetLumia920 extends lumia920 {private $ info = 'set (mobile phone + Power Supply + data cable + headset + mobile phone case + film + mobile power supply) '; public function getInfo () {return $ this-> info;} public function getPrice () {return parent: getPrice () + 100 ;}$ iphone = new fullSetIphone (); echo 'iPhone full configuration Price :'. $ iphone-> getPrice (). ', configured :'. $ iphone-> getInfo (); echo'
'; $ Lumia920 = new bareLumia920 (); echo 'lumia920 full Set configuration Price :'. $ lumia920-> getPrice (). ', configured :'. $ lumia920-> getInfo ();?>
We have set three packages for the iphone and lumia920 respectively, but we can find that the bare methods for the iphone and lumia920 are identical with the fullSet method, which leads to a coupling, we can completely separate the bare method from the fullSet method, which requires the decoration mode. the code is as follows:
Info;} public function getPrice () {return $ this-> price ;}} class lumia920 extends mobile {private $ price = 4399; private $ info = 'official standard (mobile phone + Power + data cable + headset) '; public function getInfo () {return $ this-> info;} public function getPrice () {return $ this-> price ;}}// decoration class abstract class decorateIphone extends mobile {protected $ iphone; public function _ construct (mobile $ iphone) {$ this-> iphone = $ iphone;} class bare extends decorateIphone {private $ info = 'bare metal (mobile phone) '; public function getInfo () {return $ this-> info;} public function getPrice () {return $ this-> iphone-> getPrice ()-200 ;}} class fullSet extends decorateIphone {private $ info = 'set (mobile phone + Power Supply + data cable + headset + mobile phone case + film + mobile power supply) '; public function getInfo () {return $ this-> info;} public function getPrice () {return $ this-> iphone-> getPrice () + 100 ;}} $ iphone = new fullSet (new iphone (); echo 'iPhone full configuration Price :'. $ iphone-> getPrice (). ', configured :'. $ iphone-> getInfo (); echo'
'; $ Lumia920 = new bare (new lumia920 (); echo' lumia920 bare metal price :'. $ lumia920-> getPrice (). ', configured :'. $ lumia920-> getInfo ();?>
The above is php object-oriented development-decorative mode content. For more information, see PHP Chinese network (www.php1.cn )!