架构体系(Architectural pattern)
在软件工程领域,软件架构或架构体系,是对软件体系结构主体的总称。它始终一致地以“严格描述、简单使用”为根本目标,注重于重用性、性能考量、安全性和扩展性、稳定性等架构指标。并对元素、关系类型、使用约束进行描述,规范软件体系内部各个组成部分(包括子系统)的责任与关系,解决架构方面的问题。
--以上出自“维基百科”。
注:如果说“系统架构”(SoS)说明了软件是什么的问题,那么“软件架构”则是解决软件该如何做,以达到系统的目的。两者的差别,是决不能混淆!
绪:我们说编程就是对机器进行控制。而IoC的原则总体上是反其道而行,即系统控制程序。--个人理解。
一、IoC
架构体系在编程领域的具体应用,体现为具有普遍意义的设计制式(模型一说,稍显死板!),用于指导具体实现的策略。GoF进一步将其归纳为23种制式。
在架构领域,必须提到的是依循"Hollywood Principle"、有着广泛而深入应用的"隐式调用"(implicit invocation)。更因其在虚拟化等领域广为使用,所以尤为独特和重要。
这也是我们通常所说的"控制反转"--IoC(Inversion of Control)。
IoC符合高凝聚低耦合(high cohesion and loose coupling)的原则:
在编程设计领域,它以Observer制式出现;在代码实现领域,它展现为Callback技术。
2004年,Martin Fowler(PEAA的作者)对IoC进行了专文论述。详见以下链接。
在文中,作者并没有指出service locator和dependency injection两种模式孰优孰劣,仅以一句“具体使用因场景而变化”便一带而过。但却于文中强调了两者的共性部分,即“服务的配置和它的使用相独立”这一理念。这就是,把系统的集成和配置从应用程序中分离出来,简称“依赖注入”(dependency injection)。
注:HollywoodPrinple,来源于好莱坞对业余等待试镜的演员的答复。“don't call us,we'll call you”.具体在编程领域,它是指“insted of a program running the system,the system runs a program”的方法论.面向对象的事件驱动程序即是此一原则的应用。
二、IoC的应用之一,事件始末
事件的本质,其一是把对事件处理延迟(或称为后置),而在请求方(或发生方)保留配置,在实现方或应答方进行注册或注销,从而将配置与实现分离。具体见示例。
using System;
namespace EventExample
{
///<summary>
/// MainClass : 主应用程序类
///</summary>
class MainClass
{
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
static void Main(string[] args)
{
EventPublishor Publishor = new EventPublishor();
EventSubscribor1 Registor1 = new EventSubscribor1(Publishor);
EventSubscribor2 Registor2 = new EventSubscribor2(Publishor);
Publishor.DoSomthing();
Console.WriteLine("This program already finished!");
Console.ReadLine();
}
}
///<summary>
/// EventPublishor : 事件的发布器。
///</summary>
public class EventPublishor
{
// 第一步是申明委托
public delegate int sampleEventDelegate(string messageInfo);
// 第二步是申明与上述委托相关的事件
public event sampleEventDelegate sampleEvent;
public EventPublishor()
{
}
public void DoSomthing()
{
/* ... */
// 激发事件
if (this.sampleEvent != null)
{
this.sampleEvent("hello world!");
}
/* ... */
}
}
///<summary>
/// EventSubscribor1 : 事件的注册器1。
///</summary>
public class EventSubscribor1
{
public EventSubscribor1(EventPublishor Publishor)
{
Publishor.sampleEvent +=
new EventExample.EventPublishor.sampleEventDelegate(ResponseEventMethod);
}
private int ResponseEventMethod(string msg)
{
Console.WriteLine(msg + " --- This is from Registor1");
return 0;
}
}
///<summary>
/// EventSubscribor2 : 事件的注册器2。
///</summary>
public class EventSubscribor2
{
public EventSubscribor2(EventPublishor Publishor)
{
Publishor.sampleEvent +=
new EventExample.EventPublishor.sampleEventDelegate(ResponseEventMethod);
Publishor.sampleEvent +=
new EventExample.EventPublishor.sampleEventDelegate(ResponseEventMethod);
}
private int ResponseEventMethod(string msg)
{
Console.WriteLine(msg + " --- This is from Registor2");
Console.WriteLine("Please:down enter key!");
Console.ReadLine();
Console.WriteLine("ok");
return 0;
}
}
}
输出:
三、IoC的应用之二,委托的异步回调
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace AsyncCallbackDelegate
{
public delegate int MyDelegate(int x);
public class MyClass
{
//A method to be invoke by the delegate
public int MyMethod(int x)
{
//semulate a long runing proccess
Thread.Sleep(10000);
return x * x;
}
}
class Program
{
static void Main(string[] args)
{
MyClass myClass1 = new MyClass();
MyDelegate del = new MyDelegate(myClass1.MyMethod);
//Invoke our methe in another thread
IAsyncResult async = del.BeginInvoke(5, new AsyncCallback(MyCallBack), "A message from the main thread");
Console.WriteLine("Proccessing the Operation....");
Console.ReadLine();
}
static void MyCallBack(IAsyncResult async)
{
AsyncResult ar = (AsyncResult)async;
MyDelegate del = (MyDelegate)ar.AsyncDelegate;
int x = del.EndInvoke(async);
//make use of the state object.
string msg = (string)async.AsyncState;
Console.WriteLine("{0}, Result is: {1}", msg, x);
}
}
}
其它:
依赖关系在类关系图(UML)中是重要关系之一,而UML在应用场景中才有意义。依赖的本意即dependency,是依赖、受控于的意思,也就是“必要条件”(关联关系是“充分条件”);IoC控制反转的“神奇”在于,与DI联手,实现解耦以至对象在上下文的扁平化。
参考:
http://martinfowler.com/articles/injection.html.
http://www.kuqin.com/dotnet/20080511/8322.html
小结:
IoC和对象嵌套,是初涉OO领域时,较易忽略的两处。
- 大小: 6.5 KB
分享到:
相关推荐
最近有个需求就是一个抽象仓储层接口方法需要SqlServer以及Oracle两种实现方式,为了灵活我在依赖注入的时候把这两种实现都给注入进了依赖注入容器中,但是在服务调用的时候总是获取到最后注入的那个方法的实现,这...
控制反转 依赖注入的c#实现,很好的教程。
较为框架式的演示了DI服务依赖注入 适用于熟悉C#中的继承,基本的Core命令使用等. 该示例演示了以服务器集群为背景的各种业务的依赖注入. 示例来自于B站杨中科老师的.NET Core(6.0)教学.
对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的 依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程(将依赖关系注入到对象中) ,spring的...
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
✨一个轻量级的PHP依赖注入框架,让你自定义的方法也可以使用依赖注入.
javaEE 开发中 现在最成熟的框架之一应该就是spring了 spring框架最强大的地方就是实现了依赖注入 也叫控制反转 最近的一个项目中用的就是 spring框架 spring框架是工厂模式的应用结合了MVC的设计思想 大家可以...
[Manning Publications] 依赖注入 实战 (英文版) [Manning Publications] Dependency Injection (E-Book) ☆ 图书概要:☆ Dependency Injection is an in-depth guide to the current best practices for using ...
Entity Framework Repository(含依赖注入)
Unity为微软推出的一个轻量级依赖注入容器,内带了一个名为StopLight的快速入门示例,基于WindowsForm技术构建,里面有大量的冗余代码。我将全部程序用WPF技术进行了重构,保留使用Unity的全部技术和程序的全部功能...
IoC 依赖注入 技术总结 IoC 依赖注入 技术总结
EFcore Repository 依赖注入方式实现数据库基本操作
网上下载整理的关于依赖注入的介绍。循序渐进,非常不错。
Spring Ioc 注解 依赖注入
Angular6依赖注入Demo,包含懒加载实现。 Angular6依赖注入Demo,包含懒加载实现。
本项目为Java项目,使用Spring实现依赖注入,实现采用ApplicationContext,不再直接使用XmlBeanFactory方式。适合初学者了解Spring依赖注入的实现。
简介:一个小的依赖注入容器
spring依赖注入底层详解,很不错的资源,欢迎大家来下载学习。
该资源使用vs2017编写的一个关于如何使用Ninject进行依赖注入的事例,具体的说明步骤可以参考博客:https://blog.csdn.net/liudexin1985?t=1&orderby=UpdateTime
最近在熟悉反射与依赖注入,特意写了一下简单的实例,希望可以帮助到你我他