Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of Java combination Model

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of Java combination pattern case analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Java combination pattern case analysis article. Let's take a look at it.

I. Preface

The combination pattern, also known as the partial whole pattern, is used to treat a group of similar objects as a single object. The combination pattern combines objects according to the tree structure, which is used to represent the partial and overall hierarchy. This type of design pattern is a structural pattern, which provides a way to modify the same group of objects.

II. Introduction

Intention:

Objects are combined into a tree structure to represent a "part-whole" hierarchy. The combination mode enables users to use individual objects and composite objects consistently.

The main solution is:

In the problem of our attribute structure, it blurs the concepts of simple elements and complex elements, and the client program can deal with complex elements like simple elements, thus decoupling the internal structure of the complex elements of the client program.

Application example:

S arithmetic expressions include operands, operators, and another Operand, where another operator can also be operands, operators, and another Operand.

In java AWT and SWING, it is a leaf for Button and CheckBox, and Container is a branch. Android View source code can be referred to.

When to use:

When you want to represent the part of the object-the overall hierarchy (tree structure)

You want the user to ignore the difference between the composite object and a single object, and the user will use all the objects in the composite structure uniformly.

Advantages

1 the high-level module is easy to call.

Free increase of 2 nodes

Shortcoming

When using combinatorial mode, the declaration of leaves and branches are straight lines, not interfaces, which violates the principle of dependency inversion. I don't understand the principle of relying on inversion. Please refer to another article. I lose if I can't explain it.

Use the scene part, the overall scene, such as tree menu, file, folder management

Note: it is defined as a concrete class

Implement class Employee {private String name; private String dept; private int salary; private List subordinates; public Employee (String name, String dept, int salary) {this.name = name; this.dept = dept; this.salary = salary; this.subordinates = new ArrayList ();} public void add (Employee employee) {subordinates.add (employee) } public void remove (Employee employee) {subordinates.remove (employee);} public List getSubordinates () {return subordinates } @ Override public String toString () {return "Employee {" + "name='" + name +''+ ", dept='" + dept +''+ ", salary=" + salary + ", subordinates=" + subordinates +'}' }} class CompositePatternDemo {public static void main (String [] args) {Employee CEO = new Employee ("John", "CEO", 30000); Employee headSales = new Employee ("Robert", "Head Sales", 20000); Employee headMarketing = new Employee ("Michel", "Head Marketing", 20000); Employee clerk1 = new Employee ("Laura", "Marketing", 10000) Employee clerk2 = new Employee ("Bob", "Marketing", 10000); Employee salesExecutive1 = new Employee ("Richard", "Sales", 10000); Employee salesExecutive2 = new Employee ("Rob", "Sales", 10000); CEO.add (headSales); CEO.add (headMarketing); headSales.add (salesExecutive1); headSales.add (salesExecutive2); headMarketing.add (clerk1); headMarketing.add (clerk2) System.out.println (CEO);}} this is the end of the article on "case Analysis of Java composition pattern". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Java combination pattern case Analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report