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)05/31 Report--
This article mainly introduces "case analysis of combinatorial pattern of Java design pattern". In daily operation, I believe that many people have doubts about the case analysis of combinatorial pattern of Java design pattern. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "case analysis of combinatorial pattern of Java design pattern". Next, please follow the editor to study!
1. Basic introduction
1) combination pattern (Composite Pattern), also known as partial whole pattern, creates a tree structure of groups of objects, which combines objects into a tree structure to represent the "whole-part" hierarchical relationship.
2) the combination pattern combines objects according to the tree structure, which is used to represent the partial and overall levels.
3) this type of design pattern belongs to structural pattern.
4) the combination mode enables users to have consistent access to individual objects and combined objects, that is, the combination allows customers to deal with individual objects and combined objects in a consistent manner.
two。 structure
The combination mode consists of three main roles:
Abstract root node (Component): defines common methods and properties of objects at all levels of the system, and can predefine some default behaviors and properties
Branch nodes (Composite): define the behavior of branch nodes, store child nodes, combine branch nodes and leaf nodes to form a tree structure
Leaf node (Leaf): the leaf node object, under which there are no more branches, is the smallest unit of traversal in the system hierarchy.
3. The problems solved by the combination pattern
1) the composition pattern solves the problem that when the object we are dealing with can generate a tree structure, and when we want to operate on the nodes and leaves on the tree, it can provide a consistent way, regardless of whether it is a node or a leaf.
2) corresponding schematic diagram
4. The combination model solves the display of colleges and departments.
1) Application example requirements
Write a program to show the structure of a school department: the need is like this, to show the composition of the school's departments on one page, a school has multiple colleges, a college has multiple departments.
2) thought analysis and illustration (class diagram)
3) Code implementation
Component composite object declaration interface
Package com.zte;public abstract class OrganizationComponent {private String name;// name private String des;// indicates that public String getName () {return name;} public String getDes () {return des;} protected void add (OrganizationComponent organizationComponent) {/ / implements throw new UnsupportedOperationException () by default } protected void remove (OrganizationComponent organizationComponent) {/ / default implementation throw new UnsupportedOperationException ();} / / Constructor public OrganizationComponent (String name, String des) {this.name = name; this.des = des;} / / method print, abstract method protected abstract void print ();}
Composite non-leaf node
Package com.zte;import java.util.ArrayList;import java.util.List;// University is Composite and can manage Collegepublic class University extends OrganizationComponent {List organizationComponentList = new ArrayList (); / / Constructor public University (String name, String des) {super (name, des);} / / rewrite add @ Override protected void add (OrganizationComponent organizationComponent) {organizationComponentList.add (organizationComponent) } / / rewrite remove @ Override protected void remove (OrganizationComponent organizationComponent) {organizationComponent.remove (organizationComponent);} @ Override protected void print () {System.out.println ("=" + getName () + "="); for (OrganizationComponent organizationComponent: organizationComponentList) {organizationComponent.print ();}} @ Override public String getName () {return super.getName () @ Override public String getDes () {return super.getDes ();}}
Composite non-leaf node
Package com.zte;import java.util.ArrayList;import java.util.List;public class College extends OrganizationComponent {/ / list stores department List organizationComponentList = new ArrayList (); public College (String name, String des) {super (name, des);} / / rewrite add @ Override protected void add (OrganizationComponent organizationComponent) {organizationComponentList.add (organizationComponent) } / / rewrite remove @ Override protected void remove (OrganizationComponent organizationComponent) {organizationComponent.remove (organizationComponent);} @ Override protected void print () {System.out.println ("=" + getName () + "="); for (OrganizationComponent organizationComponent: organizationComponentList) {organizationComponent.print ();}} @ Override public String getName () {return super.getName () @ Override public String getDes () {return super.getDes ();}}
Leaf leaf node
Package com.zte;public class Department extends OrganizationComponent {public Department (String name, String des) {super (name, des);} / / add and remove methods no longer need to write @ Override protected void print () {System.out.println ("=" + getName () + "=");} @ Override public String getName () {return super.getName () } @ Override public String getDes () {return super.getDes ();}} package com.zte;public class Client {public static void main (String [] args) {/ / create a university OrganizationComponent university = new University ("Tsinghua University", "the best university in China"); / / create a college OrganizationComponent college1 = new College ("computer school", "computer school") OrganizationComponent college2 = new College ("School of Information Engineering", "School of Information Engineering"); / / create departments under each college college1.add (new Department ("Software Engineering", "Software Engineering"); college1.add ("Network Engineering", "Network Engineering")) College1.add (new Department ("computer Science and Technology", "established Specialty"); college2.add (new Department ("Communication Engineering", "Communication Engineering"); college2.add (new Department ("Information Engineering", "Information Engineering")); / / add the college to the school university.add (college1); university.add (college2) / / print all the departments under the university university.print (); / / print all the departments under the college college1.print ();}} 5. Considerations and details of the combination pattern
1) simplify the operation of the client. The client only needs to face a consistent object without considering the whole part or the leaf of the node.
2) it has strong expansibility. When we need to modify the composite object, we only need to adjust the internal hierarchical relationship, and the client does not have to make any changes.
3) it is convenient to create a complex hierarchical structure. The client does not care about the composition details in the combination, and it is easy to add nodes or leaves to create a complex tree structure.
4) when you need to traverse the organization, or when the object you are dealing with has a tree structure, it is very suitable to use the combination pattern.
5) High abstraction is required, if there are many differences between nodes and leaves, for example, many methods and attributes are different, so it is not suitable to use combination mode.
At this point, the study on "combinatorial pattern case analysis of Java design patterns" is over. I hope to be able to solve everyone's 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.