设计模式之组合模式

组合模式是一种用于构建树形结构的软件设计模式,它允许用户以一致的方式处理单个对象和组合对象。文章通过一个示例展示了如何在Java中实现组合模式,包括抽象组件接口、叶节点(如圆形和长方形)以及容器(复合形状)。代码中,`CompoundShape`作为容器,可以添加和移除`Shape`对象,并通过递归计算包含的形状的总面积。这种模式的优势在于其可扩展性和对开闭原则的遵循。
摘要由CSDN通过智能技术生成

定义:将对象组合成树形结构以表示“部分 - 整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

组合模式适合于树形数据结构,使用时可以应用多态或者递归处理树形数据结构。

该模式中包含 3 类角色:

  1. 抽象组件:叶节点和容器共同的接口。
  2. 叶节点(简单组件):树的基本结构,完成实际的业务逻辑。
  3. 容器(复合组件):由叶节点或容器组成的复杂结构。

代码实现

// 形状类   
public interface Shape {  
    int getX();  
    int getY();  
    double getArea();  
}
//基础形状  
public abstract class BaseShape implements Shape{  
    private int x;  
    private int y;  
  
    public BaseShape(int x, int y) {  
        this.x = x;  
        this.y = y;  
    }  
  
    @Override  
    public int getX() {  
        return x;  
    }  
  
    @Override  
    public int getY() {  
        return y;  
    }  
  
    @Override  
    public double getArea() {  
        return 0;  
    }  
}

叶节点

//圆形  
public class Circle extends BaseShape {  
    private int radius;  
  
    public Circle(int x, int y, int radius) {  
        super(x, y);  
        this.radius = radius;  
    }  
  
    @Override  
    public double getArea() {  
        return Math.PI * (Math.pow(radius, 1));  
    }  
}

//长方形  
public class Rectangle extends BaseShape{  
    private int width;  
    private int height;  
  
    public Rectangle(int x, int y, int width, int height) {  
        super(x, y);  
        this.width = width;  
        this.height = height;  
    }  
  
    @Override  
    public double getArea() {  
        return width+height;  
    }  
}

容器

// 复合形状  
public class CompoundShape extends BaseShape {  
    private List<Shape> shapes = new ArrayList<>();  
  
    public CompoundShape(Shape... shapes) {  
        super(0, 0);  
        this.shapes.addAll(List.of(shapes));  
    }  
  
    public void addShape(Shape shape) {  
        shapes.add(shape);  
    }  
  
    public void removeShape(Shape shape) {  
        shapes.remove(shape);  
    }  
  
    @Override  
    public int getX() {  
        int x = 0;  
        for (Shape child : shapes) {  
            x = Math.min(x, child.getX());  
        }  
        return x;  
    }  
  
    @Override  
    public int getY() {  
        int y = 0;  
        for (Shape child : shapes) {  
            y = Math.min(y, child.getY());  
        }  
        return y;  
    }  
  
    @Override  
    public double getArea() {  
        double area = 0;  
        for (Shape child : shapes) {  
            area = Math.max(area, child.getArea());  
        }  
        return area;  
    }  
}

使用

Shape circle=new Circle(1,1,1);  
Shape rectangle=new Rectangle(2,2,2,2);  
// 容器中包含圆形和正方形
Shape compoundShape=new CompoundShape(circle,rectangle);  
System.out.println(compoundShape.getArea());

组合模式的优点是符合开闭原则,容易扩展节点,包括叶节点和容器。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值