In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 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 combination pattern of Java design pattern". In the daily operation, I believe many people have doubts about how to understand the combination 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 combination pattern of Java design pattern". Next, please follow the editor to study!
First, what is the combination mode
Definition: objects are organized in a tree structure to achieve a "part-whole" hierarchy, which makes the client's use of individual objects and combined objects consistent.
Motivation (Motivation)
The client code depends too much on the complex internal implementation structure of the object container, and the change of the internal implementation structure of the object container (rather than the abstract interface) will cause frequent changes of the client code, resulting in some disadvantages such as maintainability and expansibility of the code.
How to decouple "client code from complex object container structures"? Let the object container implement its own complex structure, so that the client code handles the complex object container like a simple object?
Intention (Intent)
Objects are combined into a tree structure to represent a "part-whole" hierarchy. Composite enables users to use single objects and combined objects consistently.
The combination pattern, now learning is that virtual classes inherit virtual classes, and then add virtual methods. The final real class inherits the second virtual class and overrides all virtual methods.
Second, the structure of the combinatorial model
Structure diagram description:
(1) Component: objects in the composition declare interfaces, implement default behavior common to all classes where appropriate, and declare an interface for accessing and managing subcomponents of Component. Define an interface in the recursive structure to access a parent part and implement it where appropriate. (optional)
(2) Leaf: the leaf node is represented in the combination, and the leaf node has no child node, which defines the basic behavior of the object.
(3) Composite: define the behavior of those components with subassemblies, store subassemblies and implement operations related to subassemblies in the Component interface.
(4) Client: objects that operate composite components through the Component interface.
Third, the use scene of the combination mode
1. When the demand important reflects the hierarchical structure of the part and the whole
two。 You want the user to ignore the difference between the composite object and the single object, and the user will use all the objects in the composite structure uniformly.
Fourth, the advantages and disadvantages of the combination mode
Advantages
1. Make the client call simple, the client can consistently use the composite structure or a single object, users do not have to care about whether they are dealing with a single object or the whole composite structure, which simplifies the client code.
two。 It is easier to add object components to the assembly body. The client does not have to change the code because a new object part is added.
Shortcoming
The composition pattern does not easily limit the components in the combination.
Fifth, the realization of the combination mode.
There are two ways to realize the combination mode, one is the transparent combination mode, and the other is the safe combination mode. Here I would like to elaborate on what is "transparent" and what is "safe". The so-called transparent means that the interface behavior set defined by "abstract component role" consists of two parts, one is the behavior contained in the leaf object itself (such as Operation), and the other is the management sub-object behavior (Add,Remove) contained in the container object itself. This abstract component must contain all the behaviors of these two types of objects at the same time before the client code can use it transparently. No matter whether the container object or the leaf object is called, the interface method is the same. This is transparency, which is transparent to the client code, but it also has its own problem. The leaf object will not contain its own child objects. Why should there be similar methods such as Add,Remove? It is possible to call a method such as a leaf object (note: what I am saying here is possible, because some people will implement these methods as null, do nothing, and of course no exception will be thrown, do not raise the bar) will throw an exception, so it is not safe, and then people put forward the "safe combination mode". The so-called security means that the "abstract component role" only defines the method of the leaf object, exactly that the abstract component only defines the common behavior of the two types of objects, and then the method of the container object is defined on the "branch component role", so that the leaf object has the method of the leaf object, and the container object has the method of the container object, so the responsibility is very clear, of course, the call will not throw an exception. You can choose whether to implement it as "perspective" or "secure" according to your own situation. We will implement both of these situations as follows:
The implementation of namespace transparent composition pattern {/ this abstract class is the definition of folder abstract interface, which is equivalent to abstract component Component type / public abstract class Folder {/ / add folder or file public abstract void Add (Folder folder); / / delete folder or file public abstract void Remove (Folder folder) / / Open a file or folder-- this operation is equivalent to the Operation method public abstract void Open () of type Component } / the Word document class is the definition of the leaf component, which is equivalent to the Leaf type, and cannot include sub-objects / / public sealed class Word: Folder {/ / add folders or files public override void Add (Folder folder) {throw new Exception ("Word documents do not have this feature") } / / Delete folder or file public override void Remove (Folder folder) {throw new Exception ("Word document does not have this feature");} / / Open file-this operation is equivalent to the Operation method public override void Open () {Console.WriteLine of type Component ("Open Word document and start editing") }} / SonFolder type is a branch component. Because we use "transparent" type, Add,Remove inherits from Folder type / public class SonFolder: Folder {/ / add folder or file public override void Add (Folder folder) {Console.WriteLine ("File or folder has been added successfully") } / / Delete folder or file public override void Remove (Folder folder) {Console.WriteLine ("File or folder deleted successfully") } / / Open folder-this operation is equivalent to the Component type Operation method public override void Open () {Console.WriteLine ("the current folder is already open");}} public class Program {static void Main () {Folder myword = new Word (); myword.Open () / / Open the file and handle the file myword.Add (new SonFolder ()); / / throw an exception myword.Remove (new SonFolder ()); / / throw an exception Folder myfolder = new SonFolder (); myfolder.Open (); / / Open the folder myfolder.Add (new SonFolder ()) / / successfully add files or folders myfolder.Remove (new SonFolder ()); / / successfully delete files or folders Console.Read ();}
The above code is the "transparent composite mode" implementation, and the following code is the "secure composite mode" implementation:
Implementation of namespace secure composition pattern {/ this abstract class is the definition of the folder abstract interface, which is equivalent to the abstract component Component type / public abstract class Folder / / this type lacks the definition of container object management sub-objects. In the branch component, that is, the SonFolder type {/ / open a file or folder-- this operation is equivalent to the Operation method public abstract void Open () of type Component } / the Word document class is the definition of the leaf component, which is equivalent to the Leaf type Cannot include sub-object / public sealed class Word: Folder / / this type is now clean {/ / open the file-- this operation is equivalent to the Operation method public override void Open () {Console.WriteLine of type Component ("Open the Word document and start editing") }} / SonFolder type is a branch component. Now because we use "safe", Add,Remove is defined from here / public abstract class SonFolder: Folder / / it can be an abstract interface, and you can add folders or files public abstract void Add (Folder folder) according to your own situation. / / Delete a folder or file public abstract void Remove (Folder folder); / / Open a folder-- this operation is equivalent to the Operation method of type Component public override void Open () {Console.WriteLine ("current folder has been opened") }} / NextFolder type is the implementation class of the tree component / public sealed class NextFolder: SonFolder {/ / add folder or file public override void Add (Folder folder) {Console.WriteLine ("file or folder has been added successfully") } / / Delete folder or file public override void Remove (Folder folder) {Console.WriteLine ("File or folder deleted successfully") } / / Open folder-this operation is equivalent to the Component type Operation method public override void Open () {Console.WriteLine ("the current folder has been opened");}} public class Program {static void Main () {/ / this is the safe combination mode Folder myword = new Word () Myword.Open (); / / Open the file and process the file Folder myfolder = new NextFolder (); myfolder.Open (); / / Open the folder / if add and delete functions are used here, transformational operations are required, otherwise ((SonFolder) myfolder) .add (new NextFolder ()) cannot be used. / / successfully add files or folders ((SonFolder) myfolder) .Remove (new NextFolder ()); / / successfully delete files or folders Console.Read ();} VI. Net applications in combined mode
The Panel object in ASP.Net is a Composite object, and the Button object is the Leaf object. Both Button and Panel inherit from the System.Web.UI.Control class. It actually adds a Controls attribute to the Panel, and then the Controls property is a collection property with Add and Remove methods.
This is true in ASP.Net, where every control has a Controls property, which means that each control is a container control (except for LiteralControl). This approach addresses all our security concerns in containers (that is, Controls in ASP.Net).
The most typical application of this pattern in .NET is in the development of applications and WinForms and Web. In the .NET class library, many existing controls are provided for these two platforms. However, the System.Windows.Forms.Control class in System.Windows.Forms.dll applies the composite pattern, because the controls include simple controls such as Label and TextBox, which can be understood as leaf objects. At the same time, it also includes composite controls such as GroupBox and DataGrid, or container controls, each of which needs to call the OnPaint method to display the control. in order to represent the hierarchical structure of the whole and part of the objects, Microsoft applies the composition pattern to the implementation of the Control class (to be exact, it applies the transparent composition pattern).
At this point, the study on "how to understand the composition 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.