C # event system (a)

  Every New Year at home, thinking about digging a pit point, but each year after the work is too busy in Motian. Digging as usual this year. In the hope that he can fill.

  Let me talk about the next event. First of all to say that the event is not limited keywords event. But that action or direction to achieve this requirement. Of course, the phrase expression may not be clear. Now I will give a few examples.

Let's think such a scenario, we would have been a business operation processing some data, then we want to expose some of the state to deal with the outside, for example, completed a number of data processing or have no data that can be processed in this state. Of course, we can put this state to set on a property exposed, but there will be a problem. this property is access to the outside should be at what time.

  This scenario is common. For example, you grab tickets when the order has been submitted. When I did not vote for you, no matter how you refresh the page there will not be a new status to you. Or when you download large files back when the time. No matter how long you look at whether the download is complete. very ugly once knew has been completed. the above said scene is actually polling. to determine the current status through continuous access. so the best way is when the state should change there is a mechanism to notify I'm done. For example, you have to grab votes in the success of the SMS alert, there is the sound bite prompts the download is complete.

  By the example above, we naturally think of when business needs somewhere to run external notification, you can call an external notification method. This means that the event is actually a way of data transfer. He has direction by active internal to the external.

  Said exterior with a notice above method, this method is certainly an external approach. In C #, when we need to pass a method will naturally think of Action, Func or something, a packaging these are commissioned. So it seems that event keyword is easy to understand, we are exposed outside of the property through a special keyword. With this property, we can pass or remove a delegate to the inside. this event is invoked by calling those external to internal indirect method .

  Of course, there will be when it comes to decouple ah, what to say package. Let us not discuss them here. We want to discuss is how to design a system that allows internal and external data transfer becomes more concise and more convenient. It Closer to home, an event the system is definitely you have to have an event, an event we first abstract base class. first the code.

    /// <summary>
    /// 事件方法
    /// </summary>
    public abstract class EventMethod
    {
        #region Field
        /// <summary>
        /// 事件句柄
        /// </summary>
        private string handle;
        #endregion

        #region Property
        /// <summary>
        /// 事件句柄
        /// </summary>
        public string Handle { get => handle; }
        #endregion

        #region Construction
        ///  <Summary> 
        /// to create a new instance of class EventMethod
         ///  </ Summary> 
        public EventMethod () => = handle GetHandle ();
         #endregion 

        #region Method,
         ///  <Summary> 
        /// Get tie given event handler
         ///  </ Summary> 
        protected  Virtual  String GetHandle () => the GetType () the Name;.
         ///  <Summary> 
        /// wake event
         ///  </ Summary> 
        Internal  abstract  void the Awake ( the params  Object [] Parameters);
         #endregion
    }

   Event handler is to get a Key events in the event of the container. Here are the main Awake method. When the system triggers an event, will be acquired in a container by Kye out all the event method, a method in turn calls Awake Event parameter uncertainty, so here is a params parameter of Awake other specific events to re reconstructed by rewriting Awake. here we reconstruct a button click event as an example.

    ///  <the Summary> 
    /// button click event
     ///  </ the Summary> 
    public  class btnClick: EventMethod 
    { 
        #region Field,
         ///  <the Summary> 
        /// callback delegate
         ///  </ the Summary> 
        Private Action Action ;
         #endregion 

        #region Construction
         ///  <Summary> 
        /// to create a new instance of class btnClick
         ///  </ Summary> 
        public btnClick (the Action Action) => the this .action = Action;
         #endregion 

        #region Method,
        /// <summary>
        /// 唤醒事件
        /// </summary>
        /// <param name="parameters"></param>
        internal override void Awake(params object[] parameters) => action?.Invoke();
        #endregion
    }

  Above, we completed the design of an event. Other section we discuss later in the article

Guess you like

Origin www.cnblogs.com/kent-apple/p/12238977.html