Advertisement
AlexPolubiakin

q3ex2

Mar 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.08 KB | None | 0 0
  1. <?php
  2.  
  3. class Preferences{
  4.     private $data = [];
  5.     private static $instance = null;
  6.     private function __construct(){
  7.  
  8.     }
  9.  
  10.     public static function getInstance(){
  11.         if(empty(self::$instance))
  12.             self::$instance = new Preferences;
  13.             return self::$instance;  
  14.     }
  15.  
  16.     function getParam($param){
  17.         return $this->data[$param];
  18.     }
  19.  
  20.     function setParam($param,$value){
  21.         $this->data[$param] = $value;
  22.     }
  23.  
  24. }
  25. echo "<h3>Одиночка</h3>";
  26. $pref = Preferences::getInstance();
  27.  
  28. $pref->setParam("approot","this/is/approot");
  29. $pref->setParam("urlroot","http://urlroot.url");
  30. $pref->setParam("sitename","MySiteName");
  31.  
  32. $pref2 = Preferences::getInstance();
  33.  
  34. print_r($pref2->getParam("approot"));
  35. echo "<br>";
  36. print_r($pref2->getParam("urlroot"));
  37. echo "<br>";
  38. print_r($pref2->getParam("sitename"));
  39. echo "<br>";
  40.  
  41.  
  42.  
  43. abstract class Vehicle {
  44.     abstract public function ride();
  45. }
  46.  
  47. abstract class VehicleConstructor {
  48.     abstract public function createCar($model);
  49.     abstract public function createBike($model);
  50. }
  51.  
  52.  
  53. class HondaVehicle extends VehicleConstructor {
  54.     public function createCar($model){
  55.         return new HondaCar($model);
  56.     }
  57.     public function createBike($model){
  58.         return new HondaBike($model);
  59.     }
  60. }
  61.  
  62. class BmwVehicle extends VehicleConstructor {
  63.     public function createCar($model){
  64.         return new BmwCar($model);
  65.     }
  66.     public function createBike($model){
  67.         return new BmwBike($model);
  68.     }
  69. }
  70.  
  71.  
  72. class HondaBike extends Vehicle{
  73.     public $model;
  74.     public function __construct($model){
  75.         $this->model = $model;
  76.         echo "Создан " . __CLASS__ . " модель: " . $model . "<br>";
  77.     }
  78.     public function ride(){
  79.         echo "Едем на " . __CLASS__ . " : " . $this->model . "<br>";
  80.     }
  81. }
  82.  
  83. class HondaCar extends Vehicle{
  84.     public $model;
  85.     public function __construct($model){
  86.         $this->model = $model;
  87.         echo "Создан " . __CLASS__ . " модель: " . $model . "<br>";
  88.     }
  89.     public function ride(){
  90.         echo "Едем на " . __CLASS__ . " : " . $this->model . "<br>";
  91.     }
  92. }
  93.  
  94. class BmwBike extends Vehicle{
  95.     public $model;
  96.     public function __construct($model){
  97.         $this->model = $model;
  98.         echo "Создан " . __CLASS__ . " модель: " . $model . "<br>";
  99.     }
  100.     public function ride(){
  101.         echo "Едем на " . __CLASS__ . " : " . $this->model . "<br>";
  102.     }
  103. }
  104.  
  105. class BmwCar extends Vehicle{
  106.     public $model;
  107.     public function __construct($model){
  108.         $this->model = $model;
  109.         echo "Создан " . __CLASS__ . " модель: " . $model . "<br>";
  110.     }
  111.     public function ride(){
  112.         echo "Едем на " . __CLASS__ . " : " . $this->model . "<br>";
  113.     }
  114. }
  115.  
  116. echo "<h3>Абстрактная фабрика</h3>";
  117. $honda = new HondaVehicle;
  118. $bmw = new BmwVehicle;
  119. $hondaBike = $honda->createBike('CTX700');
  120. $hondaBike->ride();
  121. $hondaCar = $honda->createCar('Accord');
  122. $hondaCar->ride();
  123. $bmwBike = $bmw->createBike('2018 K 1600 B');
  124. $bmwBike->ride();
  125. $bmwCar = $bmw->createCar('Z4');
  126. $bmwCar->ride();
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134. interface Page{
  135.     public function createPage();
  136. }
  137.  
  138. class HtmlPage implements Page{
  139.     public function createPage(){
  140.         echo "Я " . __CLASS__ . " страница<br>";
  141.     }
  142. }
  143.  
  144.  
  145. abstract class PagesDecorator implements Page{
  146.     protected $pages;
  147.     public function __construct(Page $pages){
  148.         $this->pages = $pages;
  149.     }
  150.     public function createPage(){
  151.         $this->pages->createPage();
  152.     }
  153. }
  154.  
  155. class HtmlPageDecorator extends PagesDecorator{
  156.     public function __construct(Page $pages){
  157.         parent::__construct($pages);
  158.     }
  159.     private function videoPage(){
  160.         echo "Я " . __CLASS__ . " страница с видео <br>";
  161.     }
  162.     public function createPage(){
  163.         $this->pages->createPage();
  164.         $this->videoPage();
  165.     }
  166. }
  167. echo "<h3>Декоратор</h3>";
  168. $videoPages = new HtmlPageDecorator(new HtmlPage);
  169. $videoPages->createPage();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement