1.单例模式
单例模式是指只创建一个资源(对象、数据库链接等),防止外部实例+判断是否有返回或创建后返回对象。
三个要点:
1.需要一个保存类的唯一实例的静态成员变量
2.构造函数和克隆函数必须声明为私有的,防止外部程序new类从而失去单例模式的意义
3.必须提供一个访问这个实例的公共的静态方法(通常为getInstance方法),从而返回唯一实例的一个引用
优点:
1.单例模式减少资源浪费,保证整个环境只有一个实例对象,特别适合编写资源连接类
代码:
class RedisServer
{
private static $instance=[];
private $redis;
private $host='127.0.0.1';
private $post=6379;
private $timeout=0;
private $table=0;
private $connect=1;
private function __construct($config){
$this->redis = new Redis();
if(isset($config['host'])) $this->host = $config['host'];
if(isset($config['post'])) $this->post = $config['post'];
if(isset($config['timeout'])) $this->timeout = $config['timeout'];
if(isset($config['table'])) $this->table = $config['table'];
if(isset($config['connect'])) $this->connect = $config['connect']>=1 ?1:0;
if($this->connect){
$this->redis->pconnect($this->host,$this->post,$this->timeout);
}else{
$this->redis->connect($this->host,$this->post,$this->timeout);
}
if($this->table>15 || $this->table<0 || !is_int($this->table) ){
echo '数据库表只能填写0~15的整数';
exit;
}
$this->redis->select($config['table']);
}
public static function getInstance($config=[]){
$table = array_key_exists('table',$config) ?$config['table']:0;
if(!isset(self::$instance[$table])){
echo '重新新建一个表为'.$table.'的对象!';
self::$instance[$table] = new self($config);
}
return self::$instance[$table];
}
private function __clone(){}
public function close(){
return $this->redis->close();
}
public function getString($str){
return $this->redis->get($str);
}
public function setString($key,$value){
return $this->redis->set($key,$value);
}
}
$config =[
'host'=>'127.0.0.1',
'post'=>'6379',
'timeout'=>'0',
'table'=>1,
'connect'=>0,
];
$config1 =[
'host'=>'127.0.0.1',
'post'=>'6379',
'timeout'=>'0',
'table'=>2,
'connect'=>0,
];
$Server = RedisServer::getInstance($config);
$Server1 = RedisServer::getInstance($config1);
2.工厂模式
工厂模式其实是一种类,它具有创建对象的某些方法。我们可以使用工厂类来创建对象,而不直接使用 new。这样做的好处是,如果你想要更改所实例化的类名,则只需更改该工厂方法内容即可,不需要逐一寻找代码中具体实例化的地方来修改了。为系统结构提供灵活的动态扩展机制,减少了耦合。
优点:
帮助封装:简单工厂虽然简单,但是非常友好地帮助我们实现了组件的封装,然后让组件外部能真正面向接口编程。
解耦 :通过简单工厂,实现了客户端和具体实现类的解耦。
缺点:
可能增加客户端的复杂度。
工厂模式其实可以划分为:简单工厂模式、工厂方法模式、抽象工厂模式等。
1.简单工厂模式
定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂模式使一个类的实例化延迟到其子类。一般用在具体产品很少扩展的情况下,不用经常修改,且不修改代码的话是不能实现扩展的。
例子:去商店卖手机 一个是苹果厂商提供的手机 一个是小米厂商提供的手机。
interface Mobile
{
public function mobile();
public function charger();
}
class Iphone_Mobile implements Mobile {
public function __construct()
{
echo '苹果旗舰店:';
}
public function mobile(){
echo '手机;';
}
public function charger(){
echo '充电器;';
}
}
class XiaoMI_Mobile implements Mobile {
public function __construct()
{
echo '小米旗舰店:';
}
public function mobile(){
echo '手机;';
}
public function charger(){
echo '充电器;';
}
}
class Shopping
{
public static function create($class){
switch ($class)
{
case 'Iphone_Mobile':
return new Iphone_Mobile();
break;
case 'XiaoMI_Mobile':
return new XiaoMI_Mobile();
default: echo '不好意思,我这里没有 '.$class.' 这个手机卖!';
}
}
}
$Iphone = Shopping::create('Iphone_Mobile');
$Iphone->mobile();
$Iphone->charger();
$XiaoMI = Shopping::create('XiaoMI_Mobile');
$XiaoMI->mobile();
$XiaoMI->charger();
$HUAWEI = Shopping::create('HUAWEI_Mobile');
2.工厂方法模式
例子:去店铺买手机。发现只有苹果的柜台和小米的柜台。而且柜台只有手机和充电器买。
通过定义了一个抽象出来的核心工厂类,并同时定义创建产品对象的接口,而创建具体实例的工作延迟到工厂子类中去完成
interface Mobile
{
public function mobile();
public function charger();
}
class Iphone_Mobile implements Mobile {
public function __construct()
{
echo '苹果手机:';
}
public function mobile(){
echo '手机;';
}
public function charger(){
echo '充电器;';
}
}
class XiaoMI_Mobile implements Mobile {
public function __construct()
{
echo '小米手机:';
}
public function mobile(){
echo '手机;';
}
public function charger(){
echo '充电器;';
}
}
abstract class Taobao{
abstract static function shop();
}
class Iphone function extends Taobao{
public static function shop(){
return new Iphone_Mobile();
}
}
class XiaoMI function extends Taobao{
public static function shop(){
return new XiaoMI_Mobile();
}
}
$one = Iphone::shop();
$one->mobile();
$one->charger();
$two = XiaoMI::shop();
$two->mobile();
$two->charger();
3.抽象工厂模式
提供一个创建一系列相关或相互依赖对象的接口。
例子:去淘宝买电子产品 只有两个电脑和手机两个种类的产品。 而且电脑产品只有:电脑和鼠标卖,手机产品只有:手机和充电器买。
interface Mobile
{
public function mobile();
public function charger();
}
class IPhone implements Mobile
{
public function mobile()
{
echo "购买苹果手机";
}
public function charger()
{
echo "购买苹果充电器";
}
}
interface Computer
{
public function computer();
public function mouse();
}
class XiaomiComputer implements Computer
{
public function computer()
{
echo "购买小米电脑";
}
public function mouse()
{
echo "购买小米鼠标";
}
}
abstract class Shop
{
abstract public static function createMobile();
abstract public static function createComputer();
}
class Taobao extends Shop{
public static function createMobile()
{
return new IPhone();
}
public static function createComputer()
{
return new XiaomiComputer();
}
}
$one = Taobao::createMobile();
$one->mobile();
$one->charger();
$two = Taobao::createComputer();
$two->computer();
$two->mouse()
3.策略模式
定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。
代码示例:
interface Time
{
public function timeString();
}
class Year implements Time{
public function timeString(){
echo date('Y',time()).'年';
}
}
class Month implements Time{
public function timeString(){
echo date('m',time()).'月';
}
}
class Day implements Time{
public function timeString(){
echo date('Y-m-d',time());
}
}
class Times {
private $class;
public function timess(Time $class){
$this->class = $class;
return $this->class->timeString();
}
}
$Year = new Times();
$Year->timess(new Year());
$Year->timess(new Month());
$Year->timess(new Day());
4.适配器模式
将一个类的接口转成我们希望的另外一个接口,使得原本接口不兼容不能在一起工作的类可以在一起工作。
优点:灵活性和扩展性都非常好,符合开闭原则。
缺点:过多地使用适配器,会让系统非常零乱,不易整体进行把握。
代码示例:
interface Cache
{
public function connect();
public function set($key,$value,$time=0);
public function get($key);
public function del($key);
public function close();
}
class Rediss implements Cache
{
private $redis;
public function __construct()
{
$this->connect();
}
public function connect()
{
$this->redis = new Redis();
return $this->redis->connect('127.0.0.1','6379');
}
public function set($key,$value,$time=0)
{
if($time==0){
return $this->redis->set($key,$value);
}else{
return $this->redis->setex($key,$time,$value);
}
}
public function get($key)
{
return $this->redis->get($key);
}
public function del($key)
{
return $this->redis->delete($key);
}
public function close()
{
return $this->redis->close();
}
}
class Memcaches implements Cache
{
private $memcache;
public function __construct()
{
$this->connect();
}
public function connect()
{
$this->memcache = new Memcache();
return $this->memcache->connect('127.0.0.1','11211');
}
public function set($key,$value,$time=0)
{
return $this->memcache->set($key,$value,false,$time);
}
public function get($key)
{
return $this->memcache->get($key);
}
public function del($key)
{
return $this->memcache->delete($key);
}
public function close()
{
return $this->memcache->close();
}
}
if($cache_config == 'redis'){
$cache = new Rediss();
}else{
$cache = new Memcaches();
}
$cache->set('key','value');
$cache->get('key'));
$cache->del('key');
$cache->close();
5.注册模式
注册树模式通过将对象示例注册到一颗全局的对象树上, 需要的时候从对象树上采摘的模式设计方法。
代码示例:
class Register
{
private static $objects;
public static function set($key,$class)
{
if(!isset(self::$objects[$key])){
self::$objects[$key] = $class;
}
return true;
}
public static function get($key){
if(!isset(self::$objects[$key])){
return false;
}
return self::$objects[$key];
}
public static function unset_class($key){
if(!isset(self::$objects[$key])){
return false;
}
unset(self::$objects[$key]);
return true;
}
public static function all(){
return self::$objects;
}
}
class Apple{
public function __construct(){
echo '苹果手机:';
}
public function open(){
echo '开机';
}
public function close(){
echo '关机';
}
}
class HuaWei{
public function __construct(){
echo '华为手机:';
}
public function open(){
echo '开机';
}
public function close(){
echo '关机';
}
}
Register::set('apple',new Apple());
Register::set('huawei',new HuaWei());
echo Register::get('apple')->close();
echo Register::get('huawei')->close();
var_dump(Register::all());
6.观察者模式
观察者模式(Observer Pattern)也叫做发布订阅模式(Publish/subscribe),它是一个在项目中经常使用的模式定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新。
优点:降低了类对象之间的耦合度。
缺点:观察者较多时,可能会花费一定的开销来发消息,但这个消息可能仅一个观察者消费。
代码示例:
interface Send{
public function sendMsg();
}
class Shop implements Send{
public function sendMsg(){
echo '<pre> 用户下单成功 </pre>';
}
}
class User implements Send{
public function sendMsg(){
echo '<pre> 购买商品成功 </pre>';
}
}
class Loggs implements Send{
public function sendMsg(){
echo '<pre> xx用户在xx商户购买xx商品成功 </pre>';
}
}
interface obeject{
public function addObeject($key);
}
class Order implements obeject{
private $obeject;
public function addObeject($key){
$this->obeject[]=$key;
}
public function addOrder(){
foreach ($this->obeject as $value){
$value->sendMsg();
}
}
}
$order = new Order();
$order->addObeject(new Loggs());
$order->addObeject(new Shop());
$order->addOrder();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)