php解耦的三种境界
我们有三个类,Db,FileSystem,Session;实际业务需求要组合操作这三个类.
一.常规做法
class Db
{
public function read($id)
{ }
} class FileSystem
{
public function writeFile($file)
{ }
} class Session
{
public function setSession($sessionName)
{ }
} // 实际逻辑
class Logic
{
protected $db;
protected $file;
protected $session; public function __construct()
{
$this->db = new Db();
$this->file = new FileSystem();
$this->session = new Session();
} public function handle($id)
{
if ($name = $this->db->read($id)) {
$this->file->writeFile(md5($name) . '.txt');
$this->session->setSession($id);
}
}
} $id = 11;
$client = new Logic();
$client->handle($id);
缺点:
Logic内部高度耦合了,Db,FileSystem,Session
如果这三个类的初始化条件变化,则需要修改Logic的构造方法.
二.参数依赖(类型提示,将新建关联类的放入调用处)
修改Logic代码如下:
class Logic
{
protected $db;
protected $file;
protected $session; public function __construct(Db $db, FileSystem $file, Session $session)
{
$this->db = $db;
$this->file = $file;
$this->session = $session;
} public function handle($id)
{
if ($name = $this->db->read($id)) {
$this->file->writeFile(md5($name) . '.txt');
$this->session->setSession($id);
}
}
} $id = 11;
$db = new Db();
$file = new FileSystem();
$session = new Session();
$client = new Logic($db, $file, $session);
$client->handle($id);
优点:
实现了解耦
缺点:
业务变动,如果要更改Db类到MySql类或初始化条件变动,代码依然要改动.
ps:大部分的设计模式使用了该解耦方法.
三.容器
1.希望DB类,Session类,FileSystem类“拿来即用”,不用每次繁琐的初始化,比如写$db=new DB(arg1,arg2);这类语句。
2.希望DB等类型的对象是“全局”,在整个程序运行期间,随时可以调用。
3.调用DB等类型的程序员不用知道这个类太多的细节,甚至可以用一个字符串的别名来创建这样一个对象。
能够实现以上目标的就是IOC容器,可以把IOC容器简单的看成一个全局变量,并用关联数组把字符串和构造函数做绑定
// 容器
class Container
{
public $binds = array(); public function bind($abstract, $concreate)
{
$this->binds[$abstract] = $concreate;
} public function make($abstract, $params = [])
{
return call_user_func_array($this->binds[$abstract], $params);
}
} // 绑定或注册
$container = new Container();
$container->bind('db', function () {
return new Db();
});
$container->bind('file', function () {
return new FileSystem();
});
$container->bind('session', function () {
return new Session();
}); // 实际逻辑,容器依赖
class Logic
{
protected $db;
protected $file;
protected $session; public function __construct(Container $container)
{
$this->db = $container->make('db');
$this->file = $container->make('file');
$this->session = $container->make('session');
} public function handle($id)
{
if ($name = $this->db->read($id)) {
$this->file->writeFile(md5($name) . '.txt');
$this->session->setSession($id);
}
}
} $id = 11;
$logic = new Logic($container);
$logic->handle($id);
本文参考自:https://www.cnblogs.com/sweng/p/6430374.html
php解耦的三种境界的更多相关文章
- 学习Python的三种境界
前言 王国维在<人间词话>中将读书分为了三种境界:"古今之成大事业.大学问者,必经过三种之境界:'昨夜西风凋碧树,独上高楼,望尽天涯路'.此第一境也.'衣带渐宽终不悔,为伊消得人 ...
- zha男/女的三种境界
本文为chedan贴,谈一谈找对象时渣男/女的三种表现,分别对应三种境界,涉世未深的男生女生可加以小心,自身属于zha类型的可略过本文. 另,本文的恋爱观基于两个原则.一是对象应是从朋友到恋人的 ...
- 【转】从框架看PHP的五种境界及各自的薪资待遇
无意中看到这篇文章,有些触动,作为博客开篇,用来激励自己. 原文地址:点击打开 在撰写此文前首先必须申明的是本人不鄙视任何一种框架,也无意于挑起PHP框架间的战争,更没有贬低某个框架使用者的用意,本文 ...
- 转:从框架看PHP的五种境界及各自的薪资待遇(仅限于二三线城市,一线除外)
在撰写此文前首先必须申明的是本人不鄙视任何一种框架,也无意于挑起PHP框架间的战争,更没有贬低某个框架使用者的用意,本文纯粹个人的看法.你可以认为我无知也好,或者装逼也好,请不要试着在任何情况下,随便 ...
- 在ASP.NET MVC中使用Unity进行依赖注入的三种方式
在ASP.NET MVC4中,为了在解开Controller和Model的耦合,我们通常需要在Controller激活系统中引入IoC,用于处理用户请求的 Controller,让Controller ...
- Linux就这个范儿 第15章 七种武器 linux 同步IO: sync、fsync与fdatasync Linux中的内存大页面huge page/large page David Cutler Linux读写内存数据的三种方式
Linux就这个范儿 第15章 七种武器 linux 同步IO: sync.fsync与fdatasync Linux中的内存大页面huge page/large page David Cut ...
- ASP.NET MVC中使用Unity进行依赖注入的三种方式
在ASP.NET MVC中使用Unity进行依赖注入的三种方式 2013-12-15 21:07 by 小白哥哥, 146 阅读, 0 评论, 收藏, 编辑 在ASP.NET MVC4中,为了在解开C ...
- Struts中的数据处理的三种方式
Struts中的数据处理的三种方式: public class DataAction extends ActionSupport{ @Override public String execute() ...
- Spring IOC以及三种注入方式
IOC是spring的最基础部分,也是核心模块,Spring的其他组件模块和应用开发都是以它为基础的.IOC把spring的面向接口编程和松耦合的思想体现的淋漓尽致. IOC概念 IOC(Invers ...
随机推荐
- django定义模型类
模型类被定义在应用文件夹下的model.py中 模型类必须继承Django的models.Model类 属性名不能用连续的两条下划线__ 主键:primary key,简写 pk 不需要主动定义,dj ...
- win32 listctrl控件右键菜单的实现
HMENU Menu_list,Menu_all; POINT point; HINSTANCE hInstance;//下面代码放到BOOL WINAPI DialogProc下 case WM_C ...
- hdu 5747(数学,贪心)
Aaronson Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- codeforces 739E
官方题解是一个n2logn的dp做法 不过有一个简单易想的费用流做法 对每个小精灵,连边(A,i,1,pi) (B,i,1,ui) (i,t,1,0) (i,t,1,-pi*ui) 最后连边(s,A, ...
- hdu5967
看到合肥赛区的题目都是泪啊,期末考完了来补几道 公正来说,这道题我考场确实写不出来,因为我的lct模板不够完美…… 我在学习lct的时候不知道为什么代码里加边.删边都是用了一个makeroot的操作 ...
- CF 1003D Coins and Queries【位运算/硬币值都为2的幂/贪心】
Polycarp has n coins, the value of the i-th coin is ai. It is guaranteed that all the values are int ...
- Codeforces 1036E. Covered Points
又一次写起了几何.... 特殊处理在于有可能出现多条线段交于一点的情况,每次考虑时,对每条线段与其他所有线段的交点存在一个set里,对每一个set,每次删除set.size()即可 重点在于判断两条线 ...
- Column 1 of table 'xxx' cannot be converted from type 'varchar(33)' to type 'varchar(11)'
mysql主从同步失败.错误日志如下. Column 1 of table 'xxx' cannot be converted from type 'varchar(33)' to type 'var ...
- leetcode122 Best Time to Buy and Sell Stock
题意:有一个数组,第i个数据代表的是第i天股票的价格,每天只能先卖出再买进(可以不卖出也可以不买进),求最大收益. 思路:自己去弄几个数组比划比划就知道了,比如[1,2,5,3,6],第一天买进,第二 ...
- 中文名: 交通事故责任认定, 英文名称: Traffic accident responsibility identification
中文名: 交通事故责任认定, 英文名称: Traffic accident responsibility identification