How to build your own design pattern

Source: Internet
Author: User

Design pattern general Beginner, intermediate, senior programmers like to hang on the words. I guess we all have a lot of books about design patterns around here. Design pattern is the solution set accumulated by the programmer's old-timers according to their project experience. Therefore, there is no need to take the design model as a hard thing, other people's experience can be used to understand the design ideas, why this scenario to use this model. Perhaps the older generation of programmers is indeed much better than the current programmer, after all, it is difficult to find their own online design model.

The worm is a first.

A brief introduction to 2 patterns that are often used in my project

1. Robot Plug-in Mode

What the so-called robot plug-in, you can understand this. You have a robot, but it's not certain what you need this robot to do. Inserting different cards allows the robot to behave differently. Like AOP, AOP is a system-level perspective, and the plug-in pattern is based on behavior.

Look at the code directly

Define behavior:

public static event eventhandler<robotcanceleventargs> Deleteingrobot; protected virtual void Ondeleteingrobot (Robotcanceleventargs e) {Eventhandler<robotcanceleventargs
    
            > tmp = Deleteingrobot;
    
        if (TMP!= NULL) TMP (this, e);
        public static event eventhandler<robotcanceleventargs> Bhingrobot; <summary>///robot action before triggering event///</summary> protected virtual void Onbhingrobot (ROBOTC
            Anceleventargs e) {eventhandler<robotcanceleventargs> tmp = Bhingrobot;
        if (TMP!= NULL) TMP (this, e);
        public static event eventhandler<eventargs> Bhedrobot;  <summary>///robot action triggers///</summary> protected virtual void Onbhedrobot (EventArgs
            e) {eventhandler<eventargs> tmp = Bhedrobot; if (tmp!= nuLL) TMP (this, e);
        public static event eventhandler<servingeventargs> Servingrobot; <summary>///triggers///</summary> protected virtual void onserving when calling robot behavior (Servingev
            Entargs e) {eventhandler<servingeventargs> tmp = Servingrobot;
        if (TMP!= NULL) TMP (this, e); }

Behavior Examples:

public bool Bhrobot (out string message)
        {message
            = string. Empty;
            Robotcanceleventargs e = new Robotcanceleventargs ();
            Onserving (e);
            if (!e.cancel)
            {
                var v = robotservice.bhrobot (this);
    
                if (v)
                {
                    Onbhedrobot (eventargs.empty);
                    return true;
                }
                return false;
            }
            else
            {message
                = E.message;
                return false;
            }
        }

Registration card:

[Extension ("", "1.0", "late-night Bugs")]
    public class Cardrobot
    {
    
        static Cardrobot ()
        {
            
            Robot.servingrobot + = new eventhandler< Servingeventargs> (comment_servingdelegate);
        }
    
        static void Comment_servingdelegate (object sender, Servingeventargs e)
        {
            try
            {
              //do something
            }
            catch (Exception ex)
            {
                //log4n.warnlog ("Badwordfilterextension Exception:", ex);
            }
        }
    

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.