Composite mode and others (1)-Do you want to add leaves to a leaf?

Source: Internet
Author: User
The composite mode is easy to understand and implement in the design mode. It is very easy to implement a composite mode. We can use very few Code The composite mode is implemented, but the composite mode brings many problems or concerns.
The composite mode has two important ideas. merging should include either a single item or a single set or a set of individual items ). the second important idea is to define the same behavior for individual items and synthesis. in this way, the same interface or abstraction can be used when we access the composite object (whether it is synthesis or leaf.
When using the composite mode, we will first define three basic classes, the top-level abstract class component, composite synthesis, and leaf.
If we only consider the two ideas, we can use the interface when designing the top-level abstraction of the composite mode.
Public interface icomponent
{
}
(The name conflicts with system. componentmodel. icomponent of. net. We are not afraid of namespaces ^-^)
To implement the first idea, we can add two methods to the interface.
Public interface icomponent
{
Void addcomponent (icomponent component );
Void removecomponent (icomponent component );
}
Then we define two classes composite and leaf to implement this interface.
Now we have implemented the most simple composite mode. Now we can't do either of these interfaces or other functions.
To add a compositing object or leaf object to a compositing object, we need to define an ilist object in the composite class to store the compositing object or leaf (RAO password) that is added to the compositing object ), then implement the two methods in the interface.
Does the leaf class need this variable? Of course you don't need it. You cannot add anything in the leaf. But since the leaf cannot add other objects, that is
Void addcomponent (icomponent component );
Void removecomponent (icomponent component); these two methods have no function in the Ye leaf class. Why should we implement them,
Why can't we define these two methods in the composite class? It seems a bit different from the second idea of the composite mode. It is depressing.
In practical applications, interfaces are rarely used to implement top-layer abstraction, because we use the iterator or vistor mode when accessing objects in composite (the most controversial mode ), at this time, if the object in composite mode is not a tree structure, it will enter an endless loop. we generally use abstract classes to implement top-level abstraction, and add methods in the class to determine whether the synthesis is a tree structure .... (I wrote two articles today. Article More than the total number of my articles last year. ^-^) Let's continue writing tomorrow.

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.