思考并回答以下问题:
- Mediators speak to the rest of the app。怎么理解?
StangeIOC
Strange attractors create predictable patterns, often in chaotic systems.
Strange is a super-lightweight and highly extensible Inversion-of-Control (IoC) framework, written specifically for C# and Unity. We’ve validated Strange on web, standalone, and iOS, and Android.
It contains the following features, most of which are optional:
- A core binding framework that pretty much lets you bind one or more of anything to one or more of anything else.
- Dependency Injection
- Map as singleton, value or factory (get a new instance each time you need one)
- Name injections and/or supply specific implementations to specific consumer classes
- Perform constructor or setter injection
- Tag your preferred constructor
- Tag a method to fire after construction
- Inject into MonoBehaviours
- Bind polymorphically (bind any or all of your interfaces to a single concrete class)
- Reflection binding dramatically reduces overhead of employing reflectivity
- Two shared event bus systems, EventDispatcher and Signals. Both allow you to:
- Dispatch events to any point in your application
- Map local events for local communication
- Map events to Commands classes to separate business logic
- EventDispatcher transmits data payloads as primitives or ValueObjects
- Signals transmits data in bindable, type-safe parameters
- MonoBehaviour mediation
- Facilitate separation of a view from the application using it
- Keep Unity-specific code isolated from the rest of the app
- Optional MVCS (Model/View/Controller/Service) structure
- Multiple contexts
- Allow subcomponents (separate Scenes) to function on their own, or in the context of larger apps.
- Allow communication between contexts.
- Promises
- Similar to Javascript Q-Promises, these help control flow and error handling
- Promises also fit some common signal use cases much more cleanly!
- Annotated ‘Implicit’ Bindings
- Reduce boiler plate code written in your Context and Mediators!
- JSON-driven bindings
- Dynamically load your bindings at runtime!
- Don’t see what you need? The core binding framework is simple to extend. Build new Binders like:
- A different type of dispatcher, like AS3-Signals <- WAIT A MOMENT! WE DID EXACTLY THAT!!!
- An entity framework
- A multi-loader
In addition to organizing your project into a sensible structure, Strange offers the following benefits:
- Designed to play well with Unity3D. Also designed to play well without it.
- Separate UnityEngine code from the rest of your app.
- Improves portability
- Improves unit testability
- A common event bus makes information flow easy and highly decoupled. (Note: Unity’s SendMessage method does this, of course, but it’s dangerous as all get-out. I may write a whole article on just this topic at some point.)
- The extensible binder really is amazing (a friend used to tell me “it’s good to like your own cookin’”). The number of things you can accomplish with the tiny core framework would justify Strange all on its own.
- Multiple contexts allow you to “bootstrap” subcomponents so they operate fine either on their own or as an integrated part. This can hugely speed up your development process and allow developers to work in isolation, then integrate in later stages of development.
- Get rid of platform-specific #IF…#ELSE constructs in your code. IoC allows you to write whole concrete implementations correctly, then bind the ones you want to use at compile time or at run time. (As with other forms of binding, #IF…#ELSE clauses can be isolated into a single class and away from the rest of your code.)
最简单的案例
The MyFirstProject example is a great place to start learning.
Inside is everything you need to create a very simple strange app which demonstrates the Root (MyFirstProjectRoot, start here), the Context (ExampleContext), which is where your bindings go,
a Command (StartCommand), a View (ExampleView) and a Mediator (ExampleMediator).
文件目录
初始化与客户端
MyFirstProjectRoot.cs
1 | /// The Root is the entry point to a strange-enabled Unity3D app. |
MyFirstContext.cs
1 | /// The Context is where all the magic really happens. |
View
ExampleView.cs
1 | /// An example view |
ClickDetector.cs
1 | /// Just a simple MonoBehaviour Click Detector |
ExampleMediator.cs
1 | /// Example mediator |
Model
IExampleModel.cs
1 | using System; |
ExampleModel.cs
1 | /// Example Model |
Service
IExampleService.cs
1 | using System; |
ExampleService.cs
1 | /// Example Service |
Controller
StartCommand.cs
1 | /// An example Command |
ExampleEvent.cs
1 | using System; |
CallWebServiceCommand.cs
1 | /// An Asynchronous Command |