Design pattern combination pattern

Posted by slurpee on Sat, 15 Jan 2022 01:15:21 +0100

practical problem

In the company, there are many levels, such as chairman, Department Manager, technician, ordinary employees, etc. you are a tree structure. Each tree contains members and departments, and departments contain departments and members, and so on. Members are equivalent to leaves and departments are equivalent to branches.
Each branch can have multiple leaves and branches. By analogy, I really can't understand it. It doesn't matter. I'll give a vivid example to illustrate this relationship.

Combination mode

Intent: combine objects into a tree structure to represent a "part whole" hierarchy. C o m p o s i t e enables users to use single objects and composite objects consistently.
Applicability:

  • You want to represent the part of the object - the overall hierarchy.
  • You want users to ignore the difference between composite objects and individual objects. Users will use all objects in the composite structure uniformly.

structure

Let me explain this structure. The Component abstract class is used to manage the leaves and branches in the concrete class
Leaves are Leaf classes and branches are Composite classes. Our specific files are equivalent to leaves and folders are equivalent to branches.
We operate the addition, deletion and modification of leaves and branches through specific methods..
In this way, the caller can shield the difference between leaves and branches, and the caller can operate conveniently without the need to relationship whether it is a leaf or a branch.

example

Now there is a kingdom.
The Kingdom has many members and departments, and we have strict hierarchy
Leaves are members of the Kingdom, and branches are an important part of the kingdom

Component abstract class (KingDomComponent class)

    public abstract class KingDomComponent
    {
        //name
        public string name;
        //wages
        public int wages;

        public KingDomComponent(string name, int wages)
        {
            this.name = name;
            this.wages = wages;
        }
		
        public virtual void Add(params KingDomComponent[] c) { }

        public virtual void Remove(params KingDomComponent[] c) { }

        public virtual void DisPlay(int dept) 
        {
            Console.WriteLine(new string('-', dept) + name + " Salary:" + wages);
        }
    }

Branch class (Department class)

    public class Department : KingDomComponent
    {
        IList<KingDomComponent> kingDoms = new List<KingDomComponent>();

        public Department(string name, int wages) : base(name, wages) { }

        public override void Add(params KingDomComponent[] c)
        {
            foreach (var item in c)
            {
                kingDoms.Add(item);
            }
        }

        public override void Remove(params KingDomComponent[] c)
        {
            foreach (var item in c)
            {
                kingDoms.Remove(item);
            }
        }

        public override void DisPlay(int dept)
        {
            Console.Write("department:");
            base.DisPlay(dept);

            foreach(var item in kingDoms)
            {
                item.DisPlay(dept + 4);
            }
        }
    }

Leaf class (Member class)

    public class Member : KingDomComponent
    {
        public Member(string name, int wages) : base(name, wages) { }

        public override void DisPlay(int dept)
        {
            Console.Write("staff:");
            base.DisPlay(dept); 
        }
    }

Look at the class diagram

Calling end

    class Client
    {
        static void Main()
        {
            KingDomComponent component1 = new Department("king", 100000);
            KingDomComponent component2 = new Member("queen", 50000);
            KingDomComponent component3 = new Department("Convoy", 80000);
            KingDomComponent component4 = new Member("Cavalry armor", 10000);
            KingDomComponent component5 = new Member("Jockey B", 10000);
            KingDomComponent component6 = new Department("Management guild", 60000);
            KingDomComponent component7 = new Member("Civilian 1", 500);
            KingDomComponent component8 = new Member("Civilian 2", 700);
            KingDomComponent component9 = new Member("Civilian 3", 900);

            component1.Add(component2, component3, component6);
            component3.Add(component4, component5);
            component6.Add(component7, component8, component9);

            component1.DisPlay(0);

            Console.WriteLine("\n The war struck, killing two civilians and a knight\n");

            component6.Remove(component7, component8);
            component3.Remove(component4);

            component1.DisPlay(0);
        }
    }

result

summary

Composite pattern is a very common design pattern. It can help us deal with many relationships between whole and part, as well as tree structure.

Thank you for watching 0.0

Topics: C# Design Pattern