最近在复习设计模式和数据结构以及基础得算法,把自己得笔记和心得记录下来
发现大多数的phper和我一样都是看的Java版的设计模式
自己想搞些PHP的设计模式的学习路线给大家分享
其实没有设计模式我们也能完成开发工作。那我们为啥需要设计模式呢?
- 让你的代码看起来牛皮
- 让你的代码看起来层次分明,可读性强
- 让你的代码易于扩展和维护,提高开发效率,有更多的时间去摸鱼和划水
设计模式也是对系统进行合理重构的指南针,重构就是在不改变现有的功能的情况下,通过调整程序代码改善软件的质量、性能,使其程序的设计模式和架构更加合理,提高代码的扩展性和维护性。(应对产品的无理要求)
原则如下:
单一职责
对于类来说,一个类应该只负责一个职责,假设A类有两个职责,如果我们现在要修改职责1,很有可能职责2会执行错误
<?php
class Transportation
{
public function run($transportation)
{
echo $transportation.'在公路上飞驰';
}
}
$tran = new Transportation();
$tran->run('摩托车');
$tran->run('汽车');
$tran->run('飞机');
问题
飞机怎么能在公路上飞驰???
解决方案
- 根据交通工具的不同,分解成不同的类
优化代码
<?php
class RoadTransportation
{
public function run($transportation)
{
echo $transportation.'在公路上飞驰';
}
}
class AirTransportation{
public function run($transportation)
{
echo $transportation.'在天空上飞行';
}
}
class WaterTransportation{
public function run($transportation)
{
echo $transportation.'在水上运行';
}
}
$r_tran = new RoadTransportation();
$a_tran = new AirTransportation();
$w_tran = new WaterTransportation();
$r_tran->run('摩托车');
$r_tran->run('汽车');
$a_tran->run('飞机');
$w_tran->run('轮船');
总结
- 降低类的复杂度,一个类只负责一项职责
- 提高类的可读性,可维护性
- 降低变更引起的风险
- 通常情况下,我们应当遵守单一职责原则,只要代码足够简短,才可以在代码级违反单一职责原则:类中的方法足够少,可以在方法级别保持单一职责原则
接口隔离原则
客户端不应该依赖他不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上
<?php
/**
* 类A通过接口Interface1依赖类B,但是只会用到类B的1,2,3
* 类C通过接口Interface1依赖类D,但是只会用到类D的1,4,5
* 如果接口Interface1不是类A和类C的最小接口,类B和类D需要实现不需要的方法
*/
interface Interface1
{
public function method1();
public function method2();
public function method3();
public function method4();
public function method5();
}
class A
{
public function use(B $class)
{
$class->method1();
$class->method2();
$class->method3();
}
}
class B implements Interface1{
public function method1()
{
// TODO: Implement method1() method.
}
public function method2()
{
// TODO: Implement method2() method.
}
public function method3()
{
// TODO: Implement method3() method.
}
public function method4()
{
// TODO: Implement method4() method.
}
public function method5()
{
// TODO: Implement method5() method.
}
}
class C{
public function use(D $class)
{
$class->method1();
$class->method4();
$class->method5();
}
}
class D implements Interface1{
public function method1()
{
// TODO: Implement method1() method.
}
public function method2()
{
// TODO: Implement method2() method.
}
public function method3()
{
// TODO: Implement method3() method.
}
public function method4()
{
// TODO: Implement method4() method.
}
public function method5()
{
// TODO: Implement method5() method.
}
}
问题
如果接口Interface1不是类A和类C的最小接口,类B和类D需要实现不需要的方法
解决方案
拆分接口
优化方案
<?php
interface Interface1
{
public function method1();
}
interface Interface2
{
public function method2();
public function method3();
}
interface Interface3
{
public function method4();
public function method5();
}
class A
{
public function use(B $b)
{
$b->method1();
$b->method2();
$b->method3();
}
}
class B implements Interface1, Interface2
{
public function method1()
{
// TODO: Implement method1() method.
}
public function method2()
{
// TODO: Implement method2() method.
}
public function method3()
{
// TODO: Implement method3() method.
}
}
class C
{
public function use(D $d)
{
$d->method1();
$d->method4();
$d->method5();
}
}
class D implements Interface1, Interface3
{
public function method1()
{
// TODO: Implement method1() method.
}
public function method4()
{
// TODO: Implement method4() method.
}
public function method5()
{
// TODO: Implement method5() method.
}
}
总结
- 使用多个接口,类实现不同的接口方法,来实现最小依赖原则(接口隔离语言)
未完待续