Set up an event-driven shelf: The previous chapter, the Origin

Source: Internet
Author: User

Origin

Previous sticked experience was developed primarily on an "interface-oriented" basis, exposing business rules and definitions through an interface approach. The hierarchy of dependencies is grafted with the class library through an interface approach.

This model is suitable for the business specification is clear and the induction simple module, but for some small granularity, the business is not easy to generalize the function and the module, wants to establish the principle and the restraint will become the extravagant hope, because it is difficult to draw out obvious rules and constraints.

This is why we want to isolate the vertical pipeline, instead of the horizontal object-oriented building business.

Business-driven channel

The previous method (on the right) is grafted according to the rules, and the left (yellow) is bridged by establishing a business-driven pipeline. The characteristic is: The event is triggered at one end of the whole channel, the service is checked in the channel, and the channel result end receives the business filter result to carry on the corresponding operation.

After thinking about it, it felt like something called "event-driven."

Event-driven

Event drivers contain three elements:

Event Source: The source body that can receive external events.

Listener: An object that can receive event source notifications.

Event handlers: An object that is used to handle events.

Looking at, for the "event-driven" use of the most classic Windows operating system, the simplest understanding is that the user interface click on a button, the button is the event source (can cause the transaction of the object), and generate event parameters events, including event source information;

Some modules in the whole system are called "listeners" with the ability to perceive the click of a button, and the listener has been declared to be interested in an event, so that he will listen specifically for an event and have his own implementation.

The event itself has a complete declaration cycle, so connecting events to form an "event set" cycle.

Simple example

Simple code Surface Ideas

   Public interface doevent    {        String GetName ();    }    Public interface Listener    {        void handleevent (Doevent e);    }    Public interface EventSource    {        void Addlisterner (Listener l);        void Removelisterner (Listener l);        void FireEvent (Doevent e);    }

Declaration time three elements "event source", "Listener", "event pipeline";

Event

    public class Click:doevent    {public        string GetName ()        {            return "click";        }    }    public class Doubleclick:doevent    {public        string GetName ()        {            return "double click";        }    }

Event Source, button is a transaction that has an event raised

    public class Button:eventsource    {        private list<listener> listeners=new list<listener> ();         public void Addlisterner (Listener l)        {            listeners. ADD (l);        }        public void Removelisterner (Listener l)        {            listeners. Remove (l);        public void FireEvent (Doevent e)        {            foreach (var listener in listeners)            {                listener. Handleevent (e);}}    

Event Pipeline Monitor Listener

    public class Desktop: Listener    {public        void Handleevent (doevent e)        {            System.Console.WriteLine ("I belong to the desktop, The name is {0} ", E.getname ());}    }    public class Notebook: Listener    {public        void Handleevent (doevent e)        {            System.Console.WriteLine ("I belong to a notebook , the name is {0} ", E.getname ());}    }    public class Mobile: Listener    {public        void Handleevent (doevent e)        {            System.Console.WriteLine ("I belong to the phone , the name is {0} ", E.getname ());}    }

Call

        static void Main (string[] args)        {            Desktop tai=new Desktop ();            Notebook Bi=new notebook ();            Mobile phone shou=new mobile phone ();            button Btn=new button ();            Btn.addlisterner (Tai);            Btn.addlisterner (BI);            Btn.addlisterner (shou);            Btn.fireevent (New Click ());            System.Console.WriteLine ("------------------------");            Btn.removelisterner (shou);            Btn.fireevent (New DoubleClick ());            System.Console.Read ();        }

More complete thinking and use in the future to continue to introduce, here is just a reflection.

Set up an event-driven shelf: The previous chapter, the Origin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.