深入Java设计模式之组合模式

主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

何时使用: 1、您想表示对象的部分-整体层次结构(树形结构)。 2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

如何解决:树枝和叶子实现统一接口,树枝内部组合该接口。

主要角色

(1)组合部件(Component):它是一个抽象接口。这里表示树根School

(2)叶子(Leaf):在组合中表示子节点对象,这里表示的是网络部InternetDepartment,其没有孩子了。

(3)合成部件(Composite):表示自己还有孩子,这里表示的是分校BranchSchool。

模式代码模板

package com.lzhsite.technology.designPattern.composite.GeneralImplement;

public interface Component
{
    public Component getComposite();

    public void sampleOperation();
}
package com.lzhsite.technology.designPattern.composite.GeneralImplement;

import java.util.Enumeration;
import java.util.Vector;

public class Composite implements Component
{
    private Vector componentVector = new Vector();

    public Component getComposite()
    {
        return this;
    }

    public void sampleOperation()
    {
        Enumeration enumeration = getChildren();
        while (enumeration.hasMoreElements())
        {
            ((Component) enumeration.nextElement()).sampleOperation();
        }
    }

    public void add(Component component)
    {
        componentVector.addElement(component);
    }

    public void remove(Component component)
    {
        componentVector.removeElement(component);
    }

    public Enumeration getChildren()
    {
        return componentVector.elements();
    }
}
package com.lzhsite.technology.designPattern.composite.GeneralImplement;

public class Leaf implements Component
{
    public Component getComposite()
    {
        return null;
    }

    public void sampleOperation()
    {

    }
}

应用举例

深入Java设计模式之组合模式_第1张图片

输入结果

深入Java设计模式之组合模式_第2张图片

package com.lzhsite.technology.designPattern.composite.FolderFileImplement;

public class ClientForFolderFileImplement
{
    public static void main(String[] args)
    {
        Root root1 = new Folder("C://");
        Root root2 = new Folder("D://");
        Root winDir = new Folder("Windows/");
        Root sysDir = new Folder("System/");
        Root userDir = new Folder("User/");
        Root defaultDir = new Folder("Default/");
        
        Root helloWorld = new File("HelloWorld.java");
        winDir.addfile(helloWorld);
        
        
        Root helloWorld1 = new File("HelloWorld1.java");
        Root helloWorld2 = new File("HelloWorld2.java");
        sysDir.addfile(helloWorld1);
        sysDir.addfile(helloWorld2);
        root1.addfile(winDir);
        root1.addfile(sysDir);
 

        
        Root HelloWorld3 = new File("HelloWorld3.java");
        Root HelloWorld4 = new File("HelloWorld4.java");
        Root HelloWorld5 = new File("HelloWorld5.java");
        userDir.addfile(defaultDir);
        userDir.addfile(HelloWorld4);
        userDir.addfile(HelloWorld5);
        defaultDir.addfile(HelloWorld3);
        root2.addfile(userDir);
        
        
        root1.display();
        root2.display();
    }
}
package com.lzhsite.technology.designPattern.composite.FolderFileImplement;

import java.util.List;

public interface Root
{
    public boolean addfile(Root file);

    public boolean removeFile(Root file);

    public List getFiles();

    public void display();
}
package com.lzhsite.technology.designPattern.composite.FolderFileImplement;

import java.util.ArrayList;
import java.util.List;

public class Folder implements Root {
	private String name;
	private List folders;

	public Folder(String name) {
		this.name = name;
		this.folders = new ArrayList();
	}

	public boolean addfile(Root file) {
		return folders.add(file);
	}

	public boolean removeFile(Root file) {
		return folders.remove(file);
	}

	public List getFiles() {
		return folders;
	}

	
	
	public String getName() {
		return name;
	}

	private static String getLevelStr(int level) {
		StringBuffer levelStr = new StringBuffer();
		for (int levelI = 0; levelI < level; levelI++) {
			levelStr.append("\t");
		}
		return levelStr.toString();
	}

    private int level = 0;
    
	public void printFileName(Root root) {

		if (root instanceof Folder) {
			System.out.print(getLevelStr(level));
			System.out.print("   |   ");
			System.out.println(((Folder) root).getName());
			for (Root sonRoot : root.getFiles()) {
				level++;
				printFileName(sonRoot);
				level--;
			}
			
		} else if (root instanceof File) {
			System.out.print(getLevelStr(level));
			root.display();
			return;
		}
	}

	public void display() {
		System.out.println(name);
		for (Root root : folders) {
			printFileName(root);
		}
	}
}
package com.lzhsite.technology.designPattern.composite.FolderFileImplement;

import java.util.List;

public class File implements Root
{
    private String name;

    public File(String name)
    {
        this.name = name;
    }

    public boolean addfile(Root file)
    {
        return false;
    }

    public boolean removeFile(Root file)
    {
        return false;
    }

    public List getFiles()
    {
        return null;
    }

    public void display()
    {
        System.out.println("   |   " + name);
    }

}

示例代码:https://gitee.com/lzhcode/maven-parent/tree/master/lzh-technology/src/main/java/com/lzhsite/technology/designPattern/composite

你可能感兴趣的:(设计模式)