本书未发布

设计模式 本文未发布 发布文章

未匹配的标注

常用设计模式

1.创建型模式

单例模式

概念:简单来说就是,确定只创建特定类的一个对象
实现方式:将所有能创建对象的方法私有化,即将构造函数设为私有,禁用克隆,禁用扩展并创建静态变量以容纳实例
代码:

final class President
{
    private static $instance;

    private function __construct()
    {
        // Hide the constructor
    }

    public static function getInstance(): President
    {
        if (!self::$instance) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    private function __clone()
    {
        // Disable cloning
    }

    private function __wakeup()
    {
        // Disable unserialize
    }
}

工厂模式

概念:简单工厂只是为客户端生成一个实例,而不会向客户端公开任何实例化逻辑
应用场景:当创建一个对象不仅仅是一些分配而且涉及一些逻辑时,将它放在专用工厂中而不是在任何地方重复相同的代码是有意义的
实现:一个门界面和实现,
代码:

//1.首先定义一个门界面和他的实现
interface Door
{
    public function getWidth(): float;
    public function getHeight(): float;
}

class WoodenDoor implements Door
{
    protected $width;
    protected $height;

    public function __construct(float $width, float $height)
    {
        $this->width = $width;
        $this->height = $height;
    }

    public function getWidth(): float
    {
        return $this->width;
    }

    public function getHeight(): float
    {
        return $this->height;
    }
}

//2.定义门工厂并返回它
class  DoorFactory 
{ 
     public  static  function  makeDoor($width,  $height): Door 
     {  
         return  new  WoodenDoor($width,  $height);  
     }  
}

// 使用
$door  =  DoorFactory::makeDoor(100,  200);  
echo  'Width: '  .  $door->getWidth();  
echo  'Height: '  .  $door->getHeight();  
// Make me a door of 50x100  
$door2  =  DoorFactory::makeDoor(50,  100);

2.结构型模式

装饰

概念:Decorator 模式允许您通过将对象包装在装饰器类的对象中来动态更改对象在运行时的行为
现实例子:您经营一家提供多种服务的汽车服务店。现在你如何计算收费账单?您选择一项服务并动态地向其添加所提供服务的价格,直到您获得最终成本。这里的每种服务都是装饰者
代码:

//简单的coffee类
interface Coffee
{
    public function getCost();
    public function getDescription();
}

class SimpleCoffee implements Coffee
{
    public function getCost()
    {
        return 10;
    }

    public function getDescription()
    {
        return 'Simple coffee';
    }
}

添加一些组件装饰

class MilkCoffee implements Coffee
{
    protected $coffee;

    public function __construct(Coffee $coffee)
    {
        $this->coffee = $coffee;
    }

    public function getCost()
    {
        return $this->coffee->getCost() + 2;
    }

    public function getDescription()
    {
        return $this->coffee->getDescription() . ', milk';
    }
}

使用

$someCoffee = new SimpleCoffee();
echo $someCoffee->getCost(); // 10
echo $someCoffee->getDescription(); // Simple Coffee

$someCoffee = new MilkCoffee($someCoffee);
echo $someCoffee->getCost(); // 12
echo $someCoffee->getDescription(); // Simple Coffee, milk

桥接

概念:桥模式是关于优先于继承的组合。实现细节从层次结构推送到具有单独层次结构的另一个对象。旨在 “将抽象与其实现分离,以便两者可以独立变化”
现实例子:考虑您有一个包含不同页面的网站,您应该允许用户更改主题。你会怎么做?为每个主题创建每个页面的多个副本,或者您只是创建单独的主题并根据用户的首选项加载它们?桥模式允许你做第二个
代码:

//WebPage 层次结构
interface WebPage
{
    public function __construct(Theme $theme);
    public function getContent();
}

class About implements WebPage
{
    protected $theme;

    public function __construct(Theme $theme)
    {
        $this->theme = $theme;
    }

    public function getContent()
    {
        return "About page in " . $this->theme->getColor();
    }
}

class Careers implements WebPage
{
    protected $theme;

    public function __construct(Theme $theme)
    {
        $this->theme = $theme;
    }

    public function getContent()
    {
        return "Careers page in " . $this->theme->getColor();
    }
}

和单独的主题层次结构

interface Theme
{
    public function getColor();
}

class DarkTheme implements Theme
{
    public function getColor()
    {
        return 'Dark Black';
    }
}
class LightTheme implements Theme
{
    public function getColor()
    {
        return 'Off white';
    }
}
class AquaTheme implements Theme
{
    public function getColor()
    {
        return 'Light blue';
    }
}

两个层次结构组合

$darkTheme = new DarkTheme();

$about = new About($darkTheme);
$careers = new Careers($darkTheme);

echo $about->getContent(); // "About page in Dark Black";
echo $careers->getContent(); // "Careers page in Dark Black";

适配器

概念:适配器模式允许您在适配器中包装其他不兼容的对象,以使其与另一个类兼容,简单来说,存储卡中有一些照片,需要将它们传输到计算机上。为了传输它们,您需要某种与您的计算机端口兼容的适配器,以便您可以将存储卡连接到您的计算机。在这种情况下,读卡器是适配器
实现:实际就是用一个类来封装它,使得它也有其他类同样的方法
代码:

//1.有一个 Lion 所有类型的狮子必须实现的接口
interface Lion
{
    public function roar();
}

class AfricanLion implements Lion
{
    public function roar()
    {
    }
}

class AsianLion implements Lion
{
    public function roar()
    {
    }
}
//猎人期望任何 Lion 接口的实现都可以进行搜索
class Hunter
{
    public function hunt(Lion $lion)
    {
        $lion->roar();
    }
}

现在在我们的游戏中添加一个dog,以便猎人也可以追捕它,不得不创建一个兼容的适配器

class WildDog
{
    public function bark()
    {
    }
}

// Adapter around wild dog to make it compatible with our game
class WildDogAdapter implements Lion
{
    protected $dog;

    public function __construct(WildDog $dog)
    {
        $this->dog = $dog;
    }

    public function roar()
    {
        $this->dog->bark();
    }
}

而现在 WildDog 可以在我们的游戏中使用 WildDogAdapter

$wildDog = new WildDog();
$wildDogAdapter = new WildDogAdapter($wildDog);

$hunter = new Hunter();
$hunter->hunt($wildDogAdapter);

3.行为型模式

观察者模式

概念:观察者模式(Observer),当一个对象的状态发生改变时,依赖他的对象会全部收到通知,并自动更新
现在例子:场景:一个事件发生后,要执行一连串更新操作.传统的编程方式,就是在事件的代码之后直接加入处理逻辑,当更新得逻辑增多之后,代码会变得难以维护.这种方式是耦合的,侵入式的,增加新的逻辑需要改变事件主题的代码
代码:

/**
 * 观察者接口类
 * 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
{

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

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

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

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

职责模式

概念:它有助于构建一系列对象。请求从一端进入并继续从一个对象到另一个对象,直到找到合适的处理程序

## 性能优化

mysql

mysql 查询优化、redis缓存、接口优化
索引、索引的数据结构、存储引擎、数据库锁、数据库事务、
数据库分库分表、中间件、分布式事务、分布式寻址、分布式锁

mysql 技能点

mysql 数据类型
  • 聚集索引与非聚集索引的区别
  • 主键索引、唯一索引、外键索引(避免使用)、全文索引、联合索引
  • 建立索引的原则(目的是减少io):
  • 大型模型(mycat)
  • 复杂sql的考察

    子查询、联合查询、条件逻辑(case/when/then/else/end)

redis

  • redis 数据类型、redis与memcache区别
  • redis分布式锁、redis优化、redis持久化、redis过期策略
  • 哨兵机制、redis的特点(不同版本)集群

    redis持久化

    aof 基于语句追加文件、rdb定时快照、虚拟内存diskstore

    接口优化

  • 接口缓存
  • 接口认证
  • 上下游接口性能监控
  • 接口代码优化

    高并发

  • 并发 :多个程序在同一台机器同一个时间点,只有一个程序运行
  • 高并发 :在同一个时间点,并发访问
  • QPS :每秒的http 请求数
  • pv: 页面点击量,一个用户在24小时内任意访问的网页数量
  • uv:独立访客
  • 峰值每秒请求数:qps=(pv80%)/(63600*20%)
  • 带宽 = pv/(360024)平均页面大小*8

QPS优化

qps 50 不需要优化
qps 100 数据库缓存、数据库负载均衡
qps 800 带宽 硬件
qps 1000 分布式 redis 、 业务分离(业务分离、消息通信)
qps 100

接口性能优化的标准

机器:cpu、内存、io、带宽
优化功能:执行内存、内存
术语:吞吐量、平均响应时间、tps、qps
监控性能:监控软件、访问日志、统计分析

性能优化的流程

  • 压力测试、性能测试
  • 影响性能的问题:缓存、并发用户、业务的依赖关系(第三方接口出问题)

协议

  • 报文体系
    • 抓包工具:httpwatch、f12、fiddler
    • 发包工具:curl 、postman、httpclient
  • 报文结构
    • request:body、header
    • reponse:header、header

web项目、api接口服务、异步网络脚本服务

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
Luson
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
发起讨论 只看当前版本


暂无话题~