Decorator mode that comes to mind when playing football, and decorator mode when playing football

Source: Internet
Author: User

Decorator mode that comes to mind when playing football, and decorator mode when playing football

Recently, I played a 9-person game. In the first half of the game, we used a poor defensive effect. In the second half, we took the initiative to take the game. The strategy we adopted in the last half seems to be implemented in the "decorator" mode.

 

First, it must be an abstract base class.

 

    public abstract class OurStrategy
    {
        public abstract void Play(string msg);
    }

 

Usually, in the first half, we usually use Defensive formations.

 

    public class OurDefaultStategy : OurStrategy
    {
        public override void Play(string msg)
        {
Console. WriteLine ("4-1-2-1 defensive formations in the first half ");
        }
    }

 

In the second half, the formation will be adjusted based on the situation in the first half. That is, the abstract class OurStrategy needs to be implemented. However, we have to abstract an abstract class that implements OurStrategy and acts as a modifier.

 

    public abstract class OurDecorator : OurStrategy
    {
        private OurStrategy _ourStrategy;
        public OurDecorator(OurStrategy ourStrategy)
        {
            this._ourStrategy = ourStrategy;
        }
        public override void Play(string msg)
        {
            if (_ourStrategy != null)
            {
                _ourStrategy.Play(msg);
            }
        }
    }

 

Above, this abstract class acts as the decorator, receives a subclass instance that implements the OurStrategy abstract base class, and executes the Play method of the OurStrategy abstract base class.

 

Next, implement the OurDecorator class to act as the decorator.

 

    public class AttackStategy : OurDecorator
    {
        public AttackStategy(OurStrategy ourStrategy) : base(ourStrategy)
        {
            
        }
        public override void Play(string msg)
        {
            base.Play(msg);
Console. WriteLine ("3-1-3-1 attack in the second half ");
        }
    } 

The above, of course, can also write a lot of derived classes of OurDecorator.

 

The client calls the following code:

 

    class Program
    {
        static void Main(string[] args)
        {
            OurDecorator ourDecorator = new AttackStategy(new OurDefaultStategy());
            ourDecorator.Play("haha");
            Console.ReadKey();
        }
    }

 

 

Above,

○ Use new AttackStategy (new ourdefadefastategy () to assign the new ourdefastatstategy () instance to the _ ourStrategy field of the abstract base class OurDecorator that acts as the decoration wall.

 

○ When ourDecorator is executed. the Play ("haha") method first comes to the Play method of AttackStategy and runs base. play (msg), the base here is the abstract parent class OurDecorator of AttackStategy, and then execute the Play method of OurDecorator. As the value has been assigned to the _ ourStrategy field of OurDecorator, the _ ourStrategy field stores the OurDefaultStategy instance. Therefore, the base. play (msg) Finally executes the Play method of OurDefaultStategy, that is, to display "4-1-2-1 defensive formations in the first half.

 

○ The Console. WriteLine ("3-1-3-1 attack time in the second half") section of the AttackStategy Play method is finally executed to display the "3-1-3-1 attack time in the second half.

Related Article

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.