StrangeIoc框架的详细介绍

这是接上面的博文讲到的StrangeIoc的简要介绍后(参考http://blog.csdn.net/leoleocs/article/details/47664221),介绍一下extension中详细的内容,以及如何在Unity3D中使用该框架。

Extensions

该框架的应用是以extension的扩展为基础的,主要的扩展如下:

Injection extension

Injection的扩展可以说是松耦合绑定的基础,使逻辑和显示分离得以实现,正如官方文档所言,很多人将StrangeIoc这套框架成为Dependency Injection框架, 这个其实是由Injection扩展来是实现的,具体的类图如下: 
这里写图片描述

Injection是利用反射来实现Injection功能,比如下面的例子代码:

    public interface ISpaceship
    {
    void input(float angle, float velocity);
    IWeapon weapon{get;set;}
    }
    public class Spaceship : ISpaceship
    {
        public void input(float angle, float velocity)
        {
        //do stuff here
        }
        public IWeapon Weapon{get;set;}
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

我们对接口编程,有一个飞船的接口,他拥有武器接口IWeapon,这个可能是需要其他的类去set了,这个”其他的类” 就有依赖了。

那么如何去解除这样的依赖呢? 我们可以用Injection中的扩展来解除这样的依赖,具体的做法如下:

    public class Spaceship : ISpaceship
    {
        public void input(float angle, float velocity)
        {
        //do stuff here
        }
        //增加Inject的属性
        **[Inject]**
        public IWeapon Weapon{get;set;}
    }

//用下面的方法来增加绑定
injectionBinder.Bind<IWeapon>().To<PhaserGun>();
injectionBinder.Bind<ISpaceship>().To<Spaceship>();
// 下面的方法可以生成SpaceShip类对象,并且其会自动帮我们设置Weapon属性
// 简单吧
ISpaceship spaceShip = injectionBinder.GetInstance<ISpaceship>() as ISpaceship;

// 如果你想换武器,就修改绑定,将PhaserGun换成其他的实现了IWeapon的具体类
injectionBinder.Bind<IWeapon>().To<Cannon>();
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在实际Injection的应用中,有两种不同的Injection属性的应用,【Inject】和【construct】,【Construct】是在构造函数中使用,可以用如下方式设置:

    public Spaceship()
    {
    //This constructor gets called by default...
    }
    [Construct]
    public Spaceship(IWeapon weapon)
    {
    //...but this one is marked, so Strange will call it instead
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

除此外,我们还可以使用【PostConstruct】属性,标记有PostConstruct属性的方法会在完成构造和Injection后调用。具体用法如下:

    [PostConstruct]
    public void PostConstruct()
    {
    //Do stuff you’d normally do in a constructor
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

在我们使用Injection的时候,需要注意下列事项

  1. 循环依赖问题,Injection框架可以帮我们检查这样的错误并且抛出异常,但是我们最好在写代码的时候防止这么做,下面就是一个简单的例子,在实际情况中的依赖可能比这个负责,比如,A - >B -> C- >D,而D最后又需要A。
    public class Spaceship : ISpaceship
    {
        public void input(float angle, float velocity)
        {
        //do stuff here
        }
        //增加Inject的属性
        [Inject]
        public IWeapon Weapon{get;set;}
    }
    public class PhaserGun: IWeapon 
    {
        //增加Inject的属性
        [Inject]
        public ISpaceship Spaceship{get;set;}
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2 Injection 是利用反射来实现,反射比较慢,我们可以利用ReflectionBinder中的RefectAll提前来完成反射。 
3 我们必须记住的是,任何的依赖都需要binding去记住mapping,如果我们忘记了增加Binding,就会产生空对象异常, Injection会帮助我们查看这些错误。

Dispatcher extension

Event Dispatcher 和Singal都可以看作是Dispatcher的扩展,其实就是一种Observer设计模式的松耦合实现。基本的类关系图如下: 
这里写图片描述

这里写图片描述

本质上都是Observer的实现方式,下面的Command extension 和Mediator extension都会用Dispatch将MVC设计模式中的M,V,C联系到一起。

具体的例子代码如下:

dispatcher.AddListener("FIRE_MISSILE", onMissileFire);
dispatcher.AddListener(AttackEvent.FIRE_MISSILE, onMissileFire);
dispatcher.UpdateListener(true, AttackEvent.FIRE_MISSILE, onMissileFire);

// callback funciton without payload
private void onMissileFire()
{
//this works...
}
// callback funciton with payload
private void onMissileFire(IEvent evt)
{
    //...and so does this.
    Vector3 direction = evt.data as Vector3;
}
// 无参数的onMissileFire将会被调用
dispatcher.Dispatch(AttackEvent.FIRE_MISSILE); 
Vector3 orientation = gameObject.transform.localRotation.eulerAngles;

// 带参数的onMissileFire将会被调用
dispatcher.Dispatch(AttackEvent.FIRE_MISSILE, orientation);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Command extension

Dispatch是将Event绑定在方法上,而Command extension是将Event绑定到具体的Command上,Command的使用应该是MVCS设计模式中的Controller(C),基本类之间的关系如下: 
这里写图片描述

CommandBinder可以将具体的Event或者Signal来绑定到具体的Command上,其负责利用Event或者Signal来找到,或者创建一个Command。我们可以看下面的简单的Command,


public class StartGameCommand : EventCommand
{
    [Inject]
    public ITimer gameTimer{get;set;}
    override public void Execute()
    {
        gameTimer.start();
        dispatcher.dispatch(GameEvent.STARTED);
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

接下来,我们可以下面的方式将event和Command绑定起来

commandBinder.Bind(ServerEvent.StartGame).To<StartGameCommand>();
//  解除绑定
commandBinder.Unbind(ServerEvent.StartGame);
 
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

如果想执行StartGameCoammnd,只需要调用 dispatcher.dispatch(ServerEvent.StartGame);

当然,我们也可以将Signal和Command绑定起来,在用Signal绑定以前,我们需要使用SignalCommandBinder类,代码如下:

protected override void addCoreComponents()
{
    base.addCoreComponents();
    injectionBinder.Unbind<ICommandBinder>();
    injectionBinder.Bind<ICommandBinder>().To<SignalCommandBinder>   ().ToSingleton();
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如何我们使用了SignalCommandBinder后,就可以用下面的代码简单的绑定起来了

commandBinder.Bind<SomeSignal>().To<SomeCommand>();
 
 
  • 1
  • 1

如果我们要调用SomeCommand,只需要调用SomeSignal.Dispatch () 就可以了

Mediation extension

Mediation 扩展是整个StrangeIoc框架中唯一依赖Unity3D的API的扩展,这是因为mediation是用于处理显示(Views,GameObject)和除显示外的应用程序逻辑的。在这个框架中,其把显示部分分成了View和Mediator,具体的类层次关系如下: 
这里写图片描述

在实际应用中,我们可以做如下实现,

    public class TestView : EventView
    {       
        internal void init()
        {
            // do the init work
        }
        void OnMouseDown()
        {
            dispatcher.Dispatch(CLICK_EVENT);
        }
    }

    public class ExampleMediator : EventMediator
    {
        //This is how your Mediator knows about your View.
        [Inject]
        public ExampleView view{ get; set;}

        public override void OnRegister()
        {       
            //Listen to the view for an event
            view.dispatcher.AddListener(CLICK_EVENT, onViewClicked);

            view.init ();
        }

        public override void OnRemove()
        {
            //Clean up listeners when the view is about to be destroyed
            view.dispatcher.RemoveListener(CLICK_EVENT, onViewClicked);
            Debug.Log("Mediator OnRemove");
        }

        private void onViewClicked()
        {
            Debug.Log("View click detected");
            // 这部分会和其他的应用层逻辑相关联
            dispatcher.Dispatch(REQUEST_WEB_SERVICE);
        }
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

我们实现看上述的View和Mediator后,需要调用下面的方法将他们绑定起来,

mediationBinder.Bind<ExampleView>().To<ExampleMediator>();
 
 
  • 1
  • 1

Context extension

Context extension 是整个strangeIoc 的MVCS框架应用的基础,其组合了前面所有的extension,具体的关系如下图 
这里写图片描述

MVCS框架

我们可以参考官方的图来了解这个MVCS应用框架, 具体的原图在http://strangeioc.github.io/strangeioc/,我原封不动的贴在这里: 
这里写图片描述

所以整个框架的使用主要是应用了上面的扩展来实现松耦合 
我们需要注意下面的事情

  1. 程序的入口点为ContextView,其会创建程序的MVCSContext
  2. 具体程序的MVCSContext将初始化所有的绑定,如Injection, Command,Dispatcher
  3. Dispatcher是通信总线,所有的回调,消息传递都是通过Dispatch来实现的,实际上也就是Observer模式的实现。
  4. Command可以被事件和Signal触发,其控制了具体的程序逻辑。
  5. Model记录了具体的应用程序状态
  6. Service是用于网络交互,和应该程序外的,如Web,Socket等
  7. View是MonoBehaviours,会attach到具体的GameObject上,是控制具体的显示和用户交互
  8. Mediators虽然也是MonoBehaviours,但是其一般作为中介交互View与程序其他部分交互。

总体上来讲,的确可以实现松耦合,能更好的实现大型项目的并行开发。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值