php中6种常见的设计模式

  • 单例模式
  • 观察者模式
  • 策略模式
  • 工厂模式
  • 注册模式
  • 适配器模式

单例模式

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
Db.php
<?php /** * 单例模式 */ class Db { private static $instance=null; //私有的构造方法防止外界实例化对象 private function __construct($className) { } //私有的克隆方法防止克隆 private function __clone(){ } //单例模式统一入口 public static function getInstance(){ if (!(self::$instance instanceof self)) { self::$instance = new self(); } return self::$instance; } } ?>

工厂模式

简单工厂模式,又称为静态工厂模式,在其工厂类中通过一个公有的静态方法返回每个类的实例。
<?php 
/**
* 工厂模式:
*简单工厂模式,又称为静态工厂模式,在其工厂类中通过一个公有的静态方法返回每个类的实例。
*实现接口的时候必须实现接口中定义的方法
*/
//定义一个交通运输接口
interface Transport{
	public function go();
}
//实现接口
class Bus implements Transport{
	public function go(){
		echo "这是bus";
	}
}
//实现接口
class Car implements Transport{
	public function go(){
		echo  "这是car";
	}
}
//实现接口
class Bike implements Transport{
	public function go(){
		echo  "这是bike";
	}
}
class transFactory
{
	public static function factory($transport){
		switch ($transport) {
			case 'bus':
				return new Bus();
				break;
			case 'car':
				return new Car();
				break;
			case 'bike':
				return new Bike();
				break;
		}
	}		
}
$transport=transFactory::factory('bike');
$transport->go();
 ?>

 策略模式

  

<?php
/**
 * 策略抽象类
 */
interface Strategy
{
  public function calPrice ($price);
}
/**
 * 普通会员策略类
 */
class PrimaryStrategy implements Strategy
{
  public function calPrice ($price)
  {
    echo "普通会员无折扣";
    return $price;
  }
}
/**
 * 中级会员策略类
 */
class MiddleStrategy implements Strategy
{
  public function calPrice ($price)
  {
    echo "中级会员8折优惠";
    return $price * 0.8;
  }
}
/**
 * 高级会员策略类
 */
class HighStrategy implements Strategy
{
  public function calPrice ($price)
  {
    echo "高级会员7折优惠";
    return $price * 0.7;
  }
}
/**
 * Context实现类
 */
class Price
{
  /**
   * 具体的策略类对象
   */
  private $strategyInstance;
  /**
   * 构造函数,传入一个具体的策略对象
   */
  public function __construct ($instance)
  {
    $this->strategyInstance = $instance;
  }
  /**
   * 计算货品的价格
   */
  public function quote ($price)
  {
    return $this->strategyInstance->calPrice($price);
  }
}
/**
 * 客户端操作
 */
$high = new HighStrategy();
$priceClass = new Price($high);
$price = $priceClass->quote(400);
echo $price;
?>

观察者模式

/**
 * 事件产生类
 * Class EventGenerator
 */
abstract class EventGenerator
{
    private $ObServers = [];

    //增加观察者
    public function add(ObServer $ObServer)
    {
        $this->ObServers[] = $ObServer;
    }

    //事件通知
    public function notify()
    {
        foreach ($this->ObServers as $ObServer) {
            $ObServer->update();
        }
    }

}

/**
 * 观察者接口类
 * Interface ObServer
 */
interface ObServer
{
    public function update($event_info = null);
}

/**
 * 观察者1
 */
class ObServer1 implements ObServer
{
    public function update($event_info = null)
    {
        echo "观察者1 收到执行通知 执行完毕!\n";
    }
}

/**
 * 观察者1
 */
class ObServer2 implements ObServer
{
    public function update($event_info = null)
    {
        echo "观察者2 收到执行通知 执行完毕!\n";
    }
}

/**
 * 事件
 * Class Event
 */
class Event extends EventGenerator
{
    /**
     * 触发事件
     */
    public function trigger()
    {
        //通知观察者
        $this->notify();
    }
}

//创建一个事件
$event = new Event();
//为事件增加旁观者
$event->add(new ObServer1());
$event->add(new ObServer2());
//执行事件 通知旁观者
$event->trigger();

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄