`
cdragon
  • 浏览: 76915 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

依赖注入

阅读更多

       架构体系(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
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics