设计模式——组合模式

组合(部分整体)模式_容器与内容一致性(把一组相似对象当初整体来使用)

/**
 * 组合模式的抽象父类,定义一致性
 * @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设计模式案例

声明

引用该文档请注明出处

转载于:https://juejin.im/post/5ce8eaac6fb9a07ef7104f70

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值