组合模式
程序员文章站
2022-05-04 17:46:24
...
考勤系统有个子系统,叫加班子考勤系统。
我们不仅要支持多个系统,甚至还要支持子系统。
我们不能把加班考勤子系统加入到系统数组,因为类型不一样。
使用组合模式就可以解决这个问题
组合模式:允许你将对象组合成树形结构来表现“整体/部分”层次结构。
组合能让客户以一致的方式处理个别对象以及对象组合。
提供一个接口,让个别对象和对象组合共同使用,能够用统一的做法来处理个别对象和对象组合。
这样程序员就可以使用统一的做法来遍历人员和子系统。
package com.ez.biz; import java.util.ArrayList; import java.util.List; import com.ez.SystemComponent; /** * @author 窗外赏雪(EZ编程网) */ public class Test { public static void main(String[] args) { SystemComponent ps = new PerformanceSystem(); SystemComponent ss = new SalarySystem(); SystemComponent as = new AttendanceSystem(); List<SystemComponent> applicationSystem=new ArrayList<SystemComponent>(); applicationSystem.add(ps); applicationSystem.add(ss); applicationSystem.add(as); Programmer programmer = new Programmer(applicationSystem); System.out.println("======列出所有系统的人员列表,包括子系统====="); programmer.comparePeople(); } }
package com.ez.biz; import java.util.Iterator; import java.util.List; import com.ez.SystemComponent; /** * 程序员需要列出所有系统的人员信息 * @author 窗外赏雪(EZ编程网) */ public class Programmer { private List<SystemComponent> systemComponent; public Programmer(List<SystemComponent> systemComponent) { this.systemComponent = systemComponent; } public void comparePeople() { printPeople(systemComponent.iterator()); } private void printPeople(Iterator<SystemComponent> iterator) { while (iterator.hasNext()) { SystemComponent systemComponent = (SystemComponent) iterator.next(); systemComponent.show(); } } }
package com.ez.biz; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.ez.ApplicationSystem; import com.ez.SystemComponent; /** * 加班考勤子系统 * @author 窗外赏雪(EZ编程网) */ public class AttendanceChildSystem extends SystemComponent implements ApplicationSystem { private List<SystemComponent> peoples; private String name; public AttendanceChildSystem() { name="加班子考勤系统"; peoples=new ArrayList<SystemComponent>(); peoples.add(new People("李四",19,false)); peoples.add(new People("王五",24,true)); peoples.add(new People("赵六",13,false)); peoples.add(new People("孙七",42,false)); } public Iterator<SystemComponent> createIterator(){ return peoples.iterator(); } public String getName() { return name; } @Override public void show() { System.out.println("======系统信息====="); System.out.println("名称:"+getName()); System.out.println("===================="); //把系统下面的所有人员信息show出来,如果还有子系统,会递归把所有子系统的人员信息show出来。 Iterator<SystemComponent> iterator=createIterator(); while(iterator.hasNext()){ SystemComponent systemComponent=iterator.next(); systemComponent.show(); } } }
package com.ez.biz; import java.util.Hashtable; import java.util.Iterator; import com.ez.ApplicationSystem; import com.ez.SystemComponent; /** * 考勤系统包含员工列表和加班考勤子系统。 * @author 窗外赏雪(EZ编程网) */ public class AttendanceSystem extends SystemComponent implements ApplicationSystem{ private Hashtable<String,SystemComponent> peoples; private String name; public AttendanceSystem() { name="考勤系统"; peoples=new Hashtable<String,SystemComponent>(); peoples.put("李四",new People("李四",19,false)); peoples.put("王五",new People("王五",24,true)); peoples.put("赵六",new People("赵六",13,false)); peoples.put("孙七",new People("孙七",42,false)); //考勤系统有个子系统 peoples.put("加班子考勤系统", new AttendanceChildSystem()); } public Iterator<SystemComponent> createIterator(){ return peoples.values().iterator(); } public String getName() { return name; } @Override public void show() { System.out.println("======系统信息====="); System.out.println("名称:"+getName()); System.out.println("===================="); //把系统下面的所有人员信息show出来,如果还有子系统,会递归把所有子系统的人员信息show出来。 Iterator<SystemComponent> iterator=createIterator(); while(iterator.hasNext()){ SystemComponent systemComponent=iterator.next(); systemComponent.show(); } } }
package com.ez.biz; import java.util.Iterator; import com.ez.ApplicationSystem; import com.ez.SystemComponent; import com.ez.impl.ArrayIterator; /** * 绩效系统 * 实现创建迭代器,返回数组迭代器。 * SystemComponent可以是人员,也可以是系统。 * @author 窗外赏雪(EZ编程网) */ public class PerformanceSystem extends SystemComponent implements ApplicationSystem{ static final int MAX_COUNT=10; private SystemComponent[] peoples; private String name; public PerformanceSystem() { name="绩效系统"; peoples=new SystemComponent[MAX_COUNT]; peoples[0]=new People("李四",19,false); peoples[1]=new People("王五",24,true); peoples[2]=new People("赵六",13,false); peoples[3]=new People("孙七",42,false); } public Iterator<SystemComponent> createIterator(){ return new ArrayIterator<SystemComponent>(peoples); } public String getName() { return name; } @Override public void show() { System.out.println("======系统信息======="); System.out.println("名称:"+getName()); System.out.println("===================="); //把系统下面的所有人员信息show出来,如果还有子系统,会递归把所有子系统的人员信息show出来。 Iterator<SystemComponent> iterator=createIterator(); while(iterator.hasNext()){ SystemComponent systemComponent=iterator.next(); systemComponent.show(); } } }
package com.ez.biz; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import com.ez.ApplicationSystem; import com.ez.SystemComponent; /** * 工资系统 * SystemComponent可以是人员,也可以是系统。 * @author 窗外赏雪(EZ编程网) */ public class SalarySystem extends SystemComponent implements ApplicationSystem{ private List<SystemComponent> peoples; private String name; public SalarySystem() { name="工资系统"; peoples=new ArrayList<SystemComponent>(); peoples.add(new People("李四",19,false)); peoples.add(new People("王五",24,true)); peoples.add(new People("赵六",13,false)); peoples.add(new People("孙七",42,false)); } public Iterator<SystemComponent> createIterator(){ return peoples.iterator(); } public String getName() { return name; } @Override public void show() { System.out.println("======系统信息====="); System.out.println("名称:"+getName()); System.out.println("===================="); //把系统下面的所有人员信息show出来,如果还有子系统,会递归把所有子系统的人员信息show出来。 Iterator<SystemComponent> iterator=createIterator(); while(iterator.hasNext()){ SystemComponent systemComponent=iterator.next(); systemComponent.show(); } } }
package com.ez.biz; import com.ez.SystemComponent; /** * @author 窗外赏雪(EZ编程网) */ public class People extends SystemComponent{ private String name; private int age; private boolean partyMember; public String getName() { return name; } public int getAge() { return age; } public boolean isPartyMember() { return partyMember; } public People(String name, int age, boolean partyMember) { this.name = name; this.age = age; this.partyMember=partyMember; } public void show(){ System.out.println("**********人员信息**********"); System.out.println("姓名:"+getName()+" ,年龄:"+getAge()); if(isPartyMember()){ System.out.println("是否党员:是"); }else{ System.out.println("是否党员:否"); } } }
package com.ez; /** * 让个别对象和对象组合共同使用,能够用统一的做法来处理个别对象和对象组合。 * SystemComponent可以是人员,也可以是系统。 * @author 窗外赏雪(EZ编程网) */ public abstract class SystemComponent { public void show(){ throw new UnsupportedOperationException(); } public String getName() { throw new UnsupportedOperationException(); } public int getAge() { throw new UnsupportedOperationException(); } public boolean isPartyMember() { throw new UnsupportedOperationException(); } }
package com.ez; import java.util.Iterator; /** * 使用迭代器系统接口 * @author 窗外赏雪(EZ编程网) */ public interface ApplicationSystem { Iterator<SystemComponent> createIterator(); }
package com.ez.impl; import java.util.Iterator; /** * 专门用于迭代数组类型,实现java.util的迭代器接口。 * @author 窗外赏雪(EZ编程网) * @param <T> */ public class ArrayIterator<T> implements Iterator<T> { private T[] peoples; private int position = 0; public ArrayIterator(T[] peoples) { this.peoples = peoples; } @Override public boolean hasNext() { if (position >= peoples.length || peoples[position] == null) { return false; } else { return true; } } @Override public T next() { T people = peoples[position]; position++; return people; } @Override public void remove() { } }
上一篇: 组合模式——公司组织结构