Use the simplest example to understand Composite Pattern and compositepattern.

Source: Internet
Author: User

Use the simplest example to understand Composite Pattern and compositepattern.

The composite mode is very good when the tree structure is displayed. This document shows the following Department structure:

In the preceding example, some nodes include subnodes, and some nodes include subnodes. No matter what node, each node represents a department.

 

First, design an abstract base class for the Department.

    public abstract class DepartmentComponent
    {
        public string Name { private set; get; }
        public DepartmentComponent(string name)
        {
            this.Name = name;
        }
        public virtual void PrintSupervisorOf(int spacing)
        {
            for (int counter = 0; counter < spacing; counter++)
            {
                Console.Write(" ");
            }
            Console.WriteLine(Name);
        }
    }

In the preceding example, the constructor assigns a value to the attribute and prints the node name using the PrintSupervisorOf (int spacing) method.

 

Next, design the nodes that contain the sub-nodes. This class maintains a set of the DepartmentComponent type and provides methods to add or delete nodes from the set.

    public class DepartmentComposite : DepartmentComponent
    {
        private IList<DepartmentComponent> employees;
        public DepartmentComposite(string name) : base(name)
        {
            employees = new List<DepartmentComponent>();
        }
        public void AddDepartment(DepartmentComponent e)
        {
            employees.Add(e);
        }
        public void RemoveDepartment(DepartmentComponent e)
        {
            employees.Remove(e);
        }
        public override void PrintSupervisorOf(int spacing)
        {
            base.PrintSupervisorOf(spacing);
            foreach (DepartmentComponent e in employees)
            {
                e.PrintSupervisorOf(spacing + 1);
            }
        }
    }

The preceding PrintSupervisorOf (int spacing) method overwrites the abstract base class method. It not only prints the name of the current node, but also uses recursion to display the names of all subnodes.

 

Finally, the design department contains the child nodes.

    public class Department : DepartmentComponent
    {
        public Department(string name) : base(name)
        {
            
        }
        public override void PrintSupervisorOf(int spacing)
        {
            base.PrintSupervisorOf(spacing);
        }
    }

 

Client.

    class Program
    {
        static void Main(string[] args)
        {
DepartmentComposite salesDepartment1 = new DepartmentComposite ("sales department 1 ");
Department zhangsan = new Department ("James ");
Department lisi = new Department ("Li Si ");
DepartmentComposite salesDepartment2 = new DepartmentComposite ("Sales Department 2 ");
DepartmentComposite specialDepartment = new DepartmentComposite ("Sales Department 2 Special Action Group ");
Department wangwu = new Department ("Wang Wu ");
Department zhaoliu = new Department ("Zhao liu ");
DepartmentComposite salesDepartment = new DepartmentComposite ("Sales Department ");
            salesDepartment.AddDepartment(salesDepartment1);
            salesDepartment.AddDepartment(salesDepartment2);
            salesDepartment1.AddDepartment(zhangsan);
            salesDepartment1.AddDepartment(lisi);
            salesDepartment2.AddDepartment(specialDepartment);
            specialDepartment.AddDepartment(wangwu);
            specialDepartment.AddDepartment(zhaoliu);
            salesDepartment.PrintSupervisorOf(0);
            Console.ReadKey();
        }
    }


In Java, what are the 23 design patterns that must be understood?

1. Simple Factory Pattern)
2. Builder Pattern)
3. Strategy Mode
4. Factory Method Pattern)
5. Abstract Factory)
6. Command Pattern)
7. Template Method)
8. Single Pattern)
9. Prototype Pattern)

Which design patterns are commonly used in java?

There are 23 design modes in total!

According to the purpose, the design patterns can be divided into creation patterns, structural patterns, and behavior patterns.
The creation mode is used to process the object creation process, the structure mode is used to process the combination of classes or objects, and the behavior mode is used to describe how classes or objects interact and how responsibilities are assigned.

The creation mode is used to process the object creation process. It mainly includes the following five design modes:
Factory Method Pattern)
Abstract Factory Pattern)
Builder Pattern)
Prototype Pattern)
Singleton Pattern)

The structural mode is used to process the combination of classes or objects. It mainly includes the following seven design modes:
Adapter Pattern)
Bridge Pattern)
Composite Pattern)
Decorator Pattern)
Facade Pattern)
Flyweight Pattern)
Proxy Pattern)

The behavior pattern is used to describe how classes or objects interact and how responsibilities are assigned. It mainly includes the following 11 design patterns:
Chain of Responsibility Pattern)
Command Pattern)
Interpreter mode (Interpreter Pattern)
Iterator Pattern)
Mediator Pattern)
Memory memorandum mode (Memento Pattern)
Observer Pattern)
State Pattern)
Strategy Pattern)
Template Method Pattern)
Visitor Pattern)

We recommend that you have a good book: "software tips: the point of design patterns", in which 23 examples of design patterns are very vivid and easy to understand, as well as the application of design patterns in JDK, I saw a big harvest! Baidu searches for "design patterns", and the first design patterns in Baidu encyclopedia are the first to push the book. If the number of views is more than, it will not be wrong. Let's share some good things!
I wish you an early learning of the design model!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.