In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand the decoration pattern of Java design pattern". In daily operation, I believe many people have doubts about how to understand the decoration pattern of Java design pattern. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to understand the decoration pattern of Java design pattern". Next, please follow the editor to study!
I. Preface
In fact, the decoration mode is a practical way of replacing inheritance by combination, which has been advocated all the time. I think that in order to understand the decorator pattern, we need to understand why combination is needed instead of inheritance, and why inheritance is abhorrent.
Why is it recommended to use composition instead of inheritance?
Object-oriented features have inheritance and encapsulation, but there is a little contradiction between them. Inheritance means that the subclass depends on the implementation in the parent class, and once the implementation is changed in the parent class, it will affect the subclass, which is a manifestation of breaking the encapsulation. Combination is the skillful use of encapsulation to achieve code reuse of inherited functions.
Second, what is the decoration mode 1. Definition:
Decorator mode is also known as Wrapper mode. The decorator pattern extends the functionality of objects in a way that is transparent to the client and is an alternative to inheritance relationships.
two。 Intention
Dynamically add some additional responsibilities to an object. In terms of added functionality, the Decorator pattern is more flexible than generating subclasses.
3. Alias
Wrapper Wrapper
4. motivation
Sometimes we want to add some functionality to an object rather than to the entire class. For example, a graphical user interface toolkit allows you to add components, such as borders, or behaviors, such as window scrolling, to any user interface component.
5. Action
Make the class perform better without modifying the original interface.
6. problem
Of course, there are some problems with inheritance.
Inheritance will lead to strong coupling between the superclass and the subclass, and when the superclass changes, the subclass will change.
The internal details of the superclass are visible to the subclass, and inheritance is often considered to break the encapsulation
Third, the structure of the decoration pattern
The roles in decorator mode are:
Abstract component (Component) role: gives an abstract interface to objects that are standardized to receive additional responsibilities.
Concrete ConcreteComponent role: define a class that will receive additional responsibilities
Decorator role: holds an instance of a Component object and defines an interface consistent with the abstract component interface.
ConcreteDecorator role: responsible for "pasting" additional responsibilities to component objects.
Fourth, the use scene of decoration mode
1. You need to extend the functionality of a class or add additional responsibilities to a class.
two。 You need to add functions to an object dynamically, and these functions can be undone dynamically.
3. A very large number of functions generated by the arrangement and combination of some basic functions need to be added.
Fifth, the advantages and disadvantages of decoration mode.
Advantages:
1. The purpose of decorating this pattern and inheritance is to extend the functionality of objects, but the decorator pattern is more flexible than inheritance.
two。 By using different specific decoration classes and the arrangement and combination of these classes, designers can create many combinations of different behaviors.
3. The decorator mode is very scalable.
Disadvantages:
The decorator pattern will lead to a lot of small objects in the design, and if overused, it will make the program more complex. And it will be difficult for more objects to make mistakes, especially when they all look alike.
Sixth, the implementation of the decoration pattern / the abstract class of the mobile phone, that is, the abstract component class / public abstract class Phone {public abstract void Print () in the decorator pattern. } / iPhone, that is, decorated with the concrete component class in the pattern / public class ApplePhone:Phone {/ rewrite the base class method / public override void Print () {Console.WriteLine ("start executing specific objects-- iPhone") }} / decorate abstract classes. In order for decoration to completely replace abstract components, it must inherit from Photo / public abstract class Decorator:Phone {private Phone phone; public Decorator (Phone p) {this.phone = p } public override void Print () {if (phone! = null) {phone.Print () } / public class Sticker: Decorator {public Sticker (Phone p): base (p) {} public override void Print () {base.Print () / / add a new behavior AddSticker ();} / New behavior method / public void AddSticker () {Console.WriteLine ("now iPhone has a patch") }} / Mobile pendant / 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");}}
Client code
Class Customer {static void Main (string [] args) {/ / I bought an iPhone Phone phone = new ApplePhone (); / / now I want to patch Decorator applePhoneWithSticker = new Sticker (phone); / / extend the sticker behavior applePhoneWithSticker.Print () Console.WriteLine ("- -\ n"); / / now I think there is a pendant Decorator applePhoneWithAccessories = new Accessories (phone); / / extend the behavior of the mobile pendant applePhoneWithAccessories.Print () Console.WriteLine ("- -\ n"); / / now I have both a sticker and a mobile pendant Sticker sticker = new Sticker (phone); Accessories applePhoneWithAccessoriesAndSticker = new Accessories (sticker); applePhoneWithAccessoriesAndSticker.Print (); Console.ReadLine ();}
As can be seen from the client code above, the client can dynamically add mobile phone accessories to the mobile phone. If you need to add a mobile phone shell, you only need to add a mobile phone shell class that inherits Decorator. Thus, the decoration mode is also very scalable.
7. Net application of decoration mode.
There is also an implementation of the decorator pattern in the .NET class library, which is called System.IO.Stream.
MemoryStream memoryStream = new MemoryStream (new byte [] {95Magnec 97Power98); / / extended buffer function BufferedStream buffStream = new BufferedStream (memoryStream); / / add encryption function CryptoStream cryptoStream = new CryptoStream (memoryStream,new AesManaged (). CreateEncryptor (), CryptoStreamMode.Write); / / add compression function GZipStream gzipStream = new GZipStream (memoryStream, CompressionMode.Compress, true) At this point, the study on "how to understand the decorative patterns of Java design patterns" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.