(C #) Decorative patterns of design patterns

Source: Internet
Author: User

1. Decoration mode
  Dynamically adding some additional responsibilities to an object, the adornment pattern is more flexible than generating subclasses for adding functionality.
* Decoration mode is a way to add more functions to an existing function dynamically.
* Decoration mode separates the core duties of the original class from the decoration function. Simplifies the original class, that is, to remove the repetitive decorative logic of the class.
* Decoration mode puts each decoration function in a separate class and lets the class decorate the object it is decorating.

Namespace design mode {class Program {static void Main (string[] args) {person Andy Lau = new Person (" Andy Lau "); Console.WriteLine ("Start Dress Up"); Tshirt Tshirt = new Tshirt (); Jonson jonson = new Jonson (); Shose shose = new Shose ();
Layers of packaging, constantly adding new decorative tshirt to the original class. Decrator (Andy Lau); Jonson. Decrator (Tshirt); Shose. Decrator (Jonson); Shose. Show (); }}///<summary>///specific needs to be decorated class, also need to enrich the function of the class////</summary> class Person {public Pers On () {} private string name; Public person (string name) {this.name = name; } public virtual void Show () {Console.WriteLine ("Dress up {0}", name); }}///<summary>///Dress class///</summary> class Finery:person {protected Person compo nent; Methods of DecoratingPublic void Decrator (person component) {this.component = component; public override void Show () {if (component!=null) {component. Show (); }}}///<summary>///Specific costumes///</summary> class Tshirt:finery {public ove rride void Show () {Console.WriteLine ("T-shirt");base. Show ();}} class Jonson:finery {public override void Show () {Console.WriteLine ("Jeans"); Base. Show (); }} class Shose:finery {public override void Show () {Console.WriteLine ("Bull Shoes"); Base. Show (); } }}

* Each functional layer is loaded together, then the inner function is called by the topmost instance and the functional layer is expanded according to the base class base method.

  

(C #) Decorative patterns of design patterns

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.