Blogger Information
Blog 28
fans 0
comment 0
visits 16422
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
容器注入和Facaded--2018年05月31日
植树青年小江同志的博客
Original
713 people have browsed it

## 容器注入

容器,也称服务容器,简称和(IOC),深入理解其实应是控制反转。

一言蔽之,即最大化简化外部对象的调用,即插即用,最终实现代吗的高内聚低耦合。


步骤:

1. 创建容器,将类之间的实例化过程绑定到容器中(不局限于类,也可以是接口或者闭包等)

2. 服务注册,将可能用到的工具类绑定到容器当中。

3. 容器依赖:也就是一依赖注入容器,调用工作类是直接传入容器对象,而工具类的实例化由容器完成。


> 总结,改方式就是为了依赖实例们提供一个容器,它担任全局的注册树,可以方便的通过它来获取依赖的实例,提高代码可用性。


## Facade 外观模式

外观模式,也就是门面模式

1. 一言蔽之,就是将操作进行封装,对外提供统一接口

2. 通过容器可以很好的将不同的类封装起来,将不同的操作统一到一块儿

3. 两者结合就能够实现统一的接口

实例

<?php





/**
 * 工具类
 */

class Db
{
	public function connect()
	{
		return '数据库连接成功<br>';
	}
}


class Validate
{
	//数据验证
	public function check()
	{
		return '数据验证成功<br>';
	}
}

class View
{
	//内容输出
	public function display()
	{
		return '用户登录成功';
	}
}

/**
 * 创建容器类
 */

 class Container
 {
	
	 protected $instances;

	 public function bind($abstract, $process)
	 {
		 if ($process instanceof Closure) {
			 $this->instances[$abstract] = $process;
		 } 
	 }

	 public function make($abstract, $params = [])
	 {
		 array_unshift($params, $this);

		 return call_user_func_array($this->instances[$abstract],$params);
	 }
 }

/**
 * 工作类
 */

 class User
 {
	 public function login(Container $container)
	 {
		echo $container->make('db')->connect();
		echo $container->make('validate')->check();
		echo $container->make('view')->display();
	 }
 }


/**
 * 服务绑定
 */

 $container = new Container();

//  注册当前工作类

 $container->bind('user', function ($container, $moduleName) {
	 return new User($container->make($moduleName));
 });

//  注册工具类

$container->bind('db', function($container) {
	return new Db();
});

$container->bind('validate', function($container) {
	return new Validate();
});

$container->bind('view', function($container) {
	return new View();
});

// var_dump($container);

$user = new User();
echo $user->login($container);


/**
 * facaded
 */

 class Facade
 {
	protected static $container = null;

	public static function initialize(Container $container)
	{
		static::$container = $container;
	}

	public static function connect()
	{
		return static::$container->make('db')->connect();
	}

	public static function check()
	{
		return static::$container->make('validate')->check();
	}

	public static function display()
	{
		return static::$container->make('view')->display();
	}
 }

 Facade::initialize($container);

 echo '<hr>';

echo Facade::connect();
echo Facade::check();
echo Facade::display();

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!