In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the definition, advantages and disadvantages of .NET Decoration pattern". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the definition, advantages and disadvantages of .NET Decoration pattern.
Definition of Decoration pattern:
Decoration mode is to dynamically extend the function of an object without changing the original class file and using inheritance. It wraps the real object by creating a wrapper object, that is, decoration.
Decorator pattern structure diagram:
Decorator pattern role:
(1) Abstract component (Component) role: gives an abstract interface to standardize objects that are ready to receive additional responsibilities.
(2) concrete Concrete Component role: define a class that will receive additional responsibilities.
(3) Decorator role: hold an instance of a Component object and implement an interface consistent with the interface of the abstract component.
(4) Concrete Decorator role: responsible for adding additional responsibilities to component objects.
Achieve:
Citing examples from life, we bought a new mobile phone and slowly added new decorations to the phone, such as stickers and pendants.
1. First, the mobile phone class is abstracted as an abstract component in the decorator pattern:
/ Mobile abstract class, that is, abstract component class in Abstract pattern / public abstract class Phone {/ print method / public abstract void Print ();}
two。 Suppose we buy an iPhone now, inherit from the abstract class, and implement the concrete component class:
/ iPhone, that is, the method / public override void Print () {Console.WriteLine ("I have an iPhone") decorated with specific component classes in the pattern / / public class ApplePhone:Phone {/ rewrite the base class;}}
3. Now I want to add new decoration to this phone, let's abstract the decoration class first:
/ / decorate abstract classes to completely replace abstract components, so you must inherit Phone / / public abstract class Decorator:Phone {private Phone p; / / the Phone component entity object public Decorator (Phone p) {this.p = p to which the decoration object is decorated } public override void Print () {if (this.p! = null) {p.Print ();}
4. Concrete decoration object, inheritance decoration abstract class: here concrete film decoration, and pendant decoration:
/ paste the film, specific decorator / public class Sticker:Decorator {public Sticker (Phone p): base (p) {} public override void Print () {base.Print (); / / add behavior AddSticker () } / New behavior method / public void AddSticker () {Console.WriteLine ("now iPhone has a patch") }} / Mobile pendant, that is, the specific decorator / public class Accessories:Decorator {public Accessories (Phone p): base (p) {} public override void Print () {base.Print (); / / add a new behavior AddAccessories () } / New behavior method / public void AddAccessories () {Console.WriteLine ("now the iPhone has a beautiful pendant");}}
5. Call:
/ Design pattern-decorator pattern / / class Program {static void Main (string [] args) {Phone ap = new ApplePhone (); / / bought a new iPhone Decorator aps = new Sticker (ap); / / prepare the film module aps.Print (); Decorator apa = new Accessories (ap); / / add the pendant component apa.Print () after a few days Sticker s = new Sticker (ap); / / prepare the film module Accessories a = new Accessories (s); / / prepare the pendant a.Print ();}}
Class structure diagram:
(1) Abstract component (Component) role: gives an abstract interface to standardize objects that are ready to receive additional responsibilities. > this is the Phone interface
(2) concrete Concrete Component role: define a class that will receive additional responsibilities. > ApplePhone referred to here
(3) Decorator role: hold an instance of a Component object and implement an interface consistent with the abstract component interface. > refers to the Decorator
(4) Concrete Decorator role: responsible for adding additional responsibilities to component objects. > refers to Accessories and Sticker
Advantages:
1. Both the Decorator schema and inheritance relationships are designed to extend the functionality of objects, but Decorator can provide more flexibility than inheritance.
two。 By using different specific decoration classes and the arrangement and combination of these decoration classes, designers can create many combinations of different behaviors.
Disadvantages:
1. This feature, which is more flexible than inheritance, also means more complexity.
two。 Decoration patterns can lead to many small classes in the design, which can complicate the program if overused.
3. The Decoration pattern is programmed against abstract component (Component) types. However, if you want to program against specific components, you should rethink your application architecture and whether the decorator is appropriate. Of course, you can also change the Component interface to add new public behavior to achieve a "translucent" decorator mode. Make the best choice in the actual project.
At this point, I believe you have a deeper understanding of the definition, advantages and disadvantages of .NET Decoration pattern, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.