组合模式
2019-07-26 本文已影响0人
码上述Andy
1.概述
组合模式是描述部分和整体的关系,可以形象的表示一个树形结构。
2.UML结构图
image.png3.代码实现:
Component抽象接口
**
* Created by zhouwen on 2019/7/27
*/
public abstract class Component {
protected String mName;
public Component(String name) {
mName = name;
}
public abstract void show();
public void add(Component component) {
}
public void remove(Component component) {
}
}
Composite树枝节点
/**
* Created by zhouwen on 2019/7/27 14:19
*/
//树枝节点
public class Composite extends Component {
private List<Component> components = new ArrayList<>();
public Composite(String name) {
super(name);
}
@Override
public void show() {
Logger.getLogger("Composite").info("show#name=" + mName);
for (Component component :
components) {
component.show();
}
}
@Override
public void add(Component component) {
if (components.contains(component))
components.add(component);
}
@Override
public void remove(Component component) {
if (components.contains(component))
components.remove(component);
}
}
Leaf树叶节点
/**
* Created by zhouwen on 2019/7/27
*
*/
//树叶节点
public class Leaf extends Component{
public Leaf(String name) {
super(name);
}
@Override
public void show() {
Logger.getLogger("Leaf").info("show#name=" + mName);
}
}
Client实现类
/**
* Created by zhouwen on 2019/7/27
*/
public class Client {
public void test() {
Component root = new Composite("root");
Component branch = new Composite("branch");
Component branch2 = new Composite("branch2");
Component branch3 = new Composite("branch3");
root.add(branch);
root.add(branch2);
branch2.add(branch3);
Component leaf = new Leaf("leaf");
branch.add(leaf);
Component leaf2 = new Leaf("leaf2");
Component leaf3 = new Leaf("leaf3");
branch2.add(leaf2);
branch2.add(leaf3);
root.show();
}
}
下面以我们公司层级为例:
image.png/**
* Created by zhouwen on 2019/7/27 14:58
*/
public abstract class Company {
protected String mName;
public Company(String name) {
mName = name;
}
public abstract void showHierarchy();
public void add(Company component) {
}
public void remove(Company component) {
}
}
/**
* Created by zhouwen on 2019/7/27 15:18
*/
public class BJ总公司 extends Company{
private List<Company> companies = new ArrayList<>();
@Override
public void showHierarchy() {
Logger.getLogger("BJ总公司").info("BJ总公司#name=" + mName);
for (Company company:
companies) {
company.showHierarchy();
}
}
public BJ总公司(String name) {
super(name);
}
@Override
public void add(Company company) {
if (companies.contains(company)){
companies.add(company);
}
}
@Override
public void remove(Company company) {
if (companies.contains(company))
companies.remove(company);
}
}
/**
* Created by zhouwen on 2019/7/27 15:02
*/
public class BjSubCompany extends Company {
private List<Company> companies = new ArrayList<>();
@Override
public void showHierarchy() {
Logger.getLogger("BjSubCompany").info("BjSubCompany#name=" + mName);
for (Company company:
companies) {
company.showHierarchy();
}
}
public BjSubCompany(String name) {
super(name);
}
@Override
public void add(Company company) {
if (companies.contains(company)){
companies.add(company);
}
}
@Override
public void remove(Company company) {
if (companies.contains(company))
companies.remove(company);
}
}
/**
* Created by zhouwen on 2019/7/27 15:03
*/
public class ForeignBranchOffice extends Company{
public ForeignBranchOffice(String name) {
super(name);
}
@Override
public void showHierarchy() {
}
}
/**
* Created by zhouwen on 2019/7/27 15:02
*/
public class GzSubCompany extends Company {
private List<Company> companies = new ArrayList<>();
@Override
public void showHierarchy() {
Logger.getLogger("GzSubCompany").info("GzSubCompany#name=" + mName);
for (Company company :
companies) {
company.showHierarchy();
}
}
public GzSubCompany(String name) {
super(name);
}
@Override
public void add(Company company) {
if (companies.contains(company)) {
companies.add(company);
}
}
@Override
public void remove(Company company) {
if (companies.contains(company))
companies.remove(company);
}
}
/**
* Created by zhouwen on 2019/7/27 15:04
*/
public class HR人力 extends Company{
public HR人力(String name) {
super(name);
}
@Override
public void showHierarchy() {
Logger.getLogger("HR人力").info("HR人力#name=" + mName);
}
}
/**
* Created by zhouwen on 2019/7/27 15:05
*/
public class RD研发 extends Company{
public RD研发(String name) {
super(name);
}
@Override
public void showHierarchy() {
Logger.getLogger("RD研发").info("RD研发#name=" + mName);
}
}
/**
* Created by zhouwen on 2019/7/27 15:05
*/
public class Finance财务 extends Company{
public Finance财务(String name) {
super(name);
}
@Override
public void showHierarchy() {
Logger.getLogger("Finance财务").info("Finance财务#name=" + mName);
}
}
/**
* Created by zhouwen on 2019/7/27 15:05
*/
public class Admin行政 extends Company{
@Override
public void showHierarchy() {
Logger.getLogger("Admin行政").info("Admin行政#name=" + mName);
}
public Admin行政(String name) {
super(name);
}
}
/**
* Created by zhouwen on 2019/7/27 15:16
*/
public class Client {
public void test(){
Company root = new BJ总公司("BJ总公司");
Company company = new BjSubCompany("北京分公司");
Company company2 = new BjSubCompany("广州分公司");
Company company3 = new BjSubCompany("ForeignBranchOffice");
root.add(company);
root.add(company2);
root.add(company3);
company.add(new Admin行政("北京行政"));
company.add(new HR人力("北京HR人力"));
company.add(new Finance财务("北京财务"));
company2.add(new HR人力("广州HR人力"));
company2.add(new RD研发("广州研发"));
Logger.getLogger("Client").info("公司层级:");
root.showHierarchy();
}
}