组合(部分整体)模式_容器与内容一致性(把一组相似对象当初整体来使用)
/**
* 组合模式的抽象父类,定义一致性
* @author maikec
* @date 2019/5/13
*/
public abstract class AbstractComponent implements Cloneable {
/**
* 自我介绍
*/
public abstract void sayHello();
public AbstractComponent add(AbstractComponent component)throws NoSupportedAddException{
throw new NoSupportedAddException();
}
public Boolean remove(AbstractComponent component) throws NoSupportedRemoveException{
throw new NoSupportedRemoveException( );
}
public List<AbstractComponent> getChild() throws NoSupportedSuchMethodException{
throw new NoSupportedSuchMethodException( "未提供getChild实现" );
}
@Override
protected AbstractComponent clone() throws CloneNotSupportedException {
return (AbstractComponent) super.clone();
}
}
/**
* @author maikec
* @date 2019/5/13
*/
public class Composite extends AbstractComponent {
private final List<AbstractComponent> components;
public Composite(){
components = new LinkedList<>( );
}
@Override
public void sayHello() {
System.out.println( "This is Composite" );
}
@Override
public AbstractComponent add(AbstractComponent component) throws NoSupportedAddException {
components.add( component );
return component;
}
@Override
public Boolean remove(AbstractComponent component) throws NoSupportedRemoveException {
Assert.notNull( component );
if (components.size() > 0){
if (components.contains( component )){
components.remove( component );
return true;
}
throw new NullPointerException( "未包含:"+component );
}
return false;
}
@Override
public List<AbstractComponent> getChild() throws NoSupportedSuchMethodException {
if (components.size() > 0){
return components;
}
return null;
}
/**
* 深拷贝
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Composite clone() throws CloneNotSupportedException {
Composite composite = new Composite();
this.components.forEach( composite::add );
return composite;
}
}
/**
* @author maikec
* @date 2019/5/13
*/
public class Leaf extends AbstractComponent {
@Override
public void sayHello() {
System.out.println( "This is Leaf" );
}
/**
* 浅拷贝
* @return
* @throws CloneNotSupportedException
*/
@Override
protected Leaf clone() throws CloneNotSupportedException {
return (Leaf) super.clone();
}
}
/**
* @author maikec
* @date 2019/5/13
*/
public class NoSupportedAddException extends NoSupportedSuchMethodException {
public NoSupportedAddException(){
super("未提供add实现");
}
public NoSupportedAddException(String msg){
super(msg);
}
}
/**
* @author maikec
* @date 2019/5/13
*/
public class NoSupportedRemoveException extends NoSupportedSuchMethodException {
public NoSupportedRemoveException(){
super("未提供remove实现");
}
public NoSupportedRemoveException(String msg){
super(msg);
}
}
/**
* @author maikec
* @date 2019/5/13
*/
public class NoSupportedSuchMethodException extends RuntimeException{
public NoSupportedSuchMethodException(){
super("未提供实现");
}
public NoSupportedSuchMethodException(String msg){
super(msg);
}
}
/**
* @author maikec
* @date 2019/5/13
*/
public class CompositeDemo {
public static void main(String[] args) throws CloneNotSupportedException {
AbstractComponent leaf = new Leaf();
leaf.sayHello();
AbstractComponent cloneLeaf = leaf.clone();
System.out.println( cloneLeaf.hashCode() + "||" + leaf.hashCode() );
AbstractComponent composite = new Composite();
composite.add( leaf );
composite.add( cloneLeaf );
composite.sayHello();
System.out.println( "====" );
composite.getChild().forEach( AbstractComponent::sayHello );
System.out.println( "====" );
System.out.println( composite.remove( leaf ).booleanValue() );
}
}
复制代码
附录
github.com/maikec/patt… 个人GitHub设计模式案例
声明
引用该文档请注明出处