No nonsense C # design pattern 13: Decorator

Source: Internet
Author: User
Tags abstract inheritance

Intention

Dynamically add some additional responsibilities to an object. Decorator mode is more flexible than generating subclasses in terms of adding functionality.

Scene

When designing a weapon system for a network game, the weapon's strength and wear are not taken into account at first. After that, the planners say they want to add enhanced systems and repair systems to the game, so our weapon types need to be reinforced, worn, repaired, and so on. This change is the last thing we want to see, and according to the design principle, we want the extension of the function to not modify the original program as much as possible. You may think of using inheritance to achieve it, but the planners need a weapon that can be worn and repaired, not hardened, some weapons can be hardened, but not worn, and some weapons can be hardened and worn and repaired. In this case, the inherited scheme may not be appropriate, there may be many levels of inheritance, and the number of subclasses may be many.

Therefore, the introduction of decorative patterns to solve this problem. The decorative pattern allows us to flexibly assign additional responsibilities to the class and make the design and inheritance more reasonable.

Sample code

using System;





using System.Collections.Generic;





using System.Text;





namespace Decoratorexample





{





Class Program





    {





static void Main (string[] args)





        {





Weapon w = new Rifle ();





W.showinfo ();





Enhance enhancedweapon = new enhance (w);





Enhancedweapon.enhanceammo ();





Enhancedweapon.showinfo ();





Wear wornweapon = new Wear (w);





Wornweapon.wearbyrate (0.8);





Wornweapon.showinfo ();





        }





    }





Abstract class Weapon





    {





private double ammo;





public Double Ammo





        {





get {return ammo;}





set {ammo = value;}





        }





private double attack;





public Double Attack





        {





get {return attack;}





set {attack = value;}





        }





private double speed;





public Double Speed





        {





get {return speed;}





set {speed = value;}





        }





private string name;





public string Name





        {





get {return name;}





set {name = value;}





        }





public abstract void Showinfo ();





    }





class Rifle:weapon





    {





public Rifle ()





{





this. ammo = 100;





this. Attack = 10;





this. Speed = 5;





this. Name = "Rifle";





        }





public override void Showinfo ()





        {





Console.WriteLine (String. Format ("ammo\t{0}", ammo));





Console.WriteLine (String. Format ("attack\t{0}", attack));





Console.WriteLine (String. Format ("speed\t{0}", speed));





Console.WriteLine (String. Format ("name\t{0}", name));





        }





    }





Abstract class Decorator:weapon





    {





protected Weapon W;





Public Decorator (weapon W)





        {





THIS.W = w;





        }





public override void Showinfo ()





        {





W.showinfo ();





        }





    }





class Enhance:decorator





    {





public Enhance (weapon W): Base (w) {}





public void Enhanceammo ()





        {





W.ammo + + 20;





Console.WriteLine (">>>>>>>>>>>>enhanced");





        }





    }





class Wear:decorator





    {





Public Wear (Weapon W): Base (w) {}





public void wearbyrate (double rate)





        {





w.speed = w.speed * rate;





w.attack = w.attack * rate;





Console.WriteLine (">>>>>>>>>>>>worn");





        }





    }





}

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.