依赖注入

       架构体系(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领域时,较易忽略的两处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 JavaScript 编写的记忆游戏(附源代码)   项目:JavaScript 记忆游戏(附源代码) 记忆检查游戏是一个使用 HTML5、CSS 和 JavaScript 开发的简单项目。这个游戏是关于测试你的短期 记忆技能。玩这个游戏 时,一系列图像会出现在一个盒子形状的区域中 。玩家必须找到两个相同的图像并单击它们以使它们消失。 如何运行游戏? 记忆游戏项目仅包含 HTML、CSS 和 JavaScript。谈到此游戏的功能,用户必须单击两个相同的图像才能使它们消失。 点击卡片或按下键盘键,通过 2 乘 2 旋转来重建鸟儿对,并发现隐藏在下面的图像! 如果翻开的牌面相同(一对),您就赢了,并且该对牌将从游戏中消失! 否则,卡片会自动翻面朝下,您需要重新尝试! 该游戏包含大量的 javascript 以确保游戏正常运行。 如何运行该项目? 要运行此游戏,您不需要任何类型的本地服务器,但需要浏览器。我们建议您使用现代浏览器,如 Google Chrome 和 Mozilla Firefox, 以获得更好、更优化的游戏体验。要玩游戏,首先,通过单击 memorygame-index.html 文件在浏览器中打开游戏。 演示: 该项目为国外大神项目,可以作为毕业设计的项目,也可以作为大作业项目,不用担心代码重复,设计重复等,如果需要对项目进行修改,需要具备一定基础知识。 注意:如果装有360等杀毒软件,可能会出现误报的情况,源码本身并无病毒,使用源码时可以关闭360,或者添加信任。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值