Software design patterns learning (XII) combined mode

Combined mode

Combination patterns of interest that the structure present in the leaf member and a container member and a container member objects they form tissue, leaves member can not contain member objects, the container means may comprise a member of the object, these members of the object may be a leaf member objects, it may be . These objects may constitute a tree structure, the tree structure is a processing pattern combination with object-oriented approach.


Mode motivation

In the Windows operating system's file directory structure of files and folders containing two types of objects, which in a folder can contain subfolders, you can also include files. Folders are containers, and a variety of different types of files is a member of the class, also called a leaf class, a folder can be larger as a member of another folder. Combination pattern described how the container objects and leaf objects recursive composition, so that the user need not distinguish between them when in use, can do the same treatment vessel and leaves the object the object.


Schema Definition

Combining a plurality of objects form a tree structure to represent - hierarchy of "integral part" of.


Mode structure

Here Insert Picture Description

  1. The Component (abstract member)

    Abstract component can be an interface or an abstract class, and the container member is a leaf member object declaration interface in that role can contain all subclasses Total declare and implement actions. In the abstract component in the definition of the access and management of its sub-component methods, such as increased sub-components, delete sub-components, sub-components and other acquisition.

  2. Leaf (leaf member)

    Leaf member leaf node represents an object in the composite structure, the leaf nodes have no children, to achieve behavior defined in the abstract member. And a method of managing access to the sub-member, may be processed by the abnormality or the like.

  3. Composite (container means)

    Represents a container member in the composite structure node object in the container, the container comprising a child node of the node, which may be a child node of a leaf node, a node may be a container, which provides for storing a set of child nodes, implement the behavior defined in the abstract member, including those accessing and managing sub-component approach in its business method can call the business methods of its child nodes recursively.

  4. Client (client category)

    Client class is the interface to access and control the object combining member through abstract member.


Pattern Analysis

The key is to define a combination pattern of abstract component class, both may represent the leaves, can also represent a container, and the client program against the abstract component class, without knowing it in the end is a leaf or container that can be centrally.

Abstract classes member generally designed to interface or abstract class, subclass all declarations and implementations sharing method in the abstract class member. For client-side programming, the abstract component for programming, without regard to its sub-class is a container component or leaf member.

public abstract class Component {

    public abstract void add(Component c);
    public abstract void remove(Component c);
    public abstract Component getChild(int i);
    public abstract void operation();
}

Inherited abstract member is a leaf member is exemplary code below, the leaf member need to implement the abstract construct all methods declared in the class, including business methods and management and a method to access sub-member, but leaves member does not include the sub-member, so the client must provide exception handling or error message when sub-component method calls and access management components of the leaf.

public class Leaf extends Component {

    @Override
    public void add(Component c) {
        //异常处理或错误提示
    }

    @Override
    public void remove(Component c) {
        //异常处理或错误提示
    }

    @Override
    public Component getChild(int i) {
        //异常处理或错误提示
        return null;
    }

    @Override
    public void operation() {
        //实现代码
    }
}

Construction container member need to implement all the abstract methods declared in the class, including business methods and methods for accessing and managing the sub-component implementation. Since the container is a container component acts as a role include members of the member, the member will call the members of its business methods, using a recursive algorithm, that is, in operation container member () method recursive calls its operation () method of the component members.

public class Composite extends Component {

    private ArrayList list = new ArrayList();

    @Override
    public void add(Component c) {
        list.add(c);
    }

    @Override
    public void remove(Component c) {
        list.remove(c);
    }

    @Override
    public Component getChild(int i) {
        return (Component) list.get(i);
    }

    @Override
    public void operation() {
        for (Object o : list) {
            ((Component) o).operation();
        }
    }
}


Transparent mode with a combination safe combination mode

The form of combination patterns define an abstract class member, can be divided into a transparent mode and a combination mode safety combination

  1. Transparent mode combination

    Transparent combination mode, abstract component Component declare all methods for managing members object, the benefits of doing so is to ensure that all members have the same kind of interface. In the client view, the method leaves object and a container object provided is consistent, clients can treat all of the same objects. The disadvantage is not secure enough, because the leaves objects and container objects in nature there is a difference.
    [Pictures of foreign chains dump fails, the source station may have a security chain mechanism, it is recommended to save the picture down directly upload (img-uVYJfFOW-1584340554438) (C: \ Users \ LENOVO \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200316142721342.png)]
  2. Safety combination mode

    Safety combination mode abstract component does not declare any Component method for managing object members, but these members method for managing objects declared in the class Composite. For leaf object, the client can not call to a method of managing members object. The disadvantage is lack of transparency, the client can not fully abstract for the program.[Pictures of foreign chains dump fails, the source station may have a security chain mechanism, it is recommended to save the picture down directly upload (img-mrTDQGOi-1584340554439) (C: \ Users \ LENOVO \ AppData \ Roaming \ Typora \ typora-user-images \ image-20200316143422742.png)]

Guess you like

Origin www.cnblogs.com/Yee-Q/p/12503803.html