如何使用Java模型类实现图数据结构及层级产品属性处理
产品属性层级结构Java实现
设计思路
产品属性同时存在「单个属性」和「可嵌套的属性组」两种形态,直接用组合模式实现最适配需求:
- 抽离公共的属性节点抽象类,统一约定打印、添加子节点的通用行为
- 单个产品属性作为叶子节点,只存储自身名称和属性值,不支持挂载子节点
- 属性组作为容器节点,存储分组名称,可挂载任意数量的单属性、子属性组
- 产品类作为顶层入口,存储产品名称和挂载的顶层属性/属性组,提供统一打印入口
核心代码实现
1. 属性节点抽象基类
/** * 属性节点公共抽象(组合模式基类) */ public abstract class AttributeComponent { protected String name; public String getName() { return name; } /** * 添加子节点,单属性(叶子节点)不支持该操作,默认抛出异常 */ public void add(AttributeComponent component) { throw new UnsupportedOperationException("单属性节点不支持添加子元素"); } /** * 打印方法 * @param productName 当前所属产品名称 * @param groupPath 当前节点所属的分组路径 * @param indent 层级缩进前缀 */ public abstract void print(String productName, String groupPath, String indent); }
2. 单个产品属性(叶子节点)
/** * 单个产品属性,无下级节点 */ public class ProductAttribute extends AttributeComponent { private final Object value; public ProductAttribute(String name, Object value) { this.name = name; this.value = value; } @Override public void print(String productName, String groupPath, String indent) { String groupDesc = groupPath.isBlank() ? "无(顶层属性)" : groupPath; System.out.printf("%s├─ 属性:%s = %s | 所属产品:%s | 归属分组:%s%n", indent, name, value, productName, groupDesc); } }
3. 属性组(容器节点,支持嵌套)
import java.util.ArrayList; import java.util.List; /** * 属性分组,可包含单属性、子属性组 */ public class AttributeGroup extends AttributeComponent { private final List<AttributeComponent> children = new ArrayList<>(); public AttributeGroup(String name) { this.name = name; } @Override public void add(AttributeComponent component) { children.add(component); } @Override public void print(String productName, String groupPath, String indent) { // 拼接当前分组的全路径 String currentGroupPath = groupPath.isBlank() ? name : groupPath + " > " + name; System.out.printf("%s├─ 【属性组:%s】%n", indent, name); // 递归打印所有子节点,缩进加两个空格区分层级 String childIndent = indent + "│ "; for (AttributeComponent child : children) { child.print(productName, currentGroupPath, childIndent); } } }
4. 产品实体类
import java.util.ArrayList; import java.util.List; /** * 产品顶层实体 */ public class Product { private final String productName; private final List<AttributeComponent> attributeComponents = new ArrayList<>(); public Product(String productName) { this.productName = productName; } public void addAttributeComponent(AttributeComponent component) { attributeComponents.add(component); } /** * 顶层属性打印入口 */ public void printAttributes() { System.out.println("============ 产品:" + productName + " 属性清单 ============"); for (AttributeComponent component : attributeComponents) { component.print(productName, "", ""); } System.out.println(); } }
测试验证
构造汽车、椅子两个产品的属性,覆盖嵌套分组场景:
public class AttributeTest { public static void main(String[] args) { // 构造汽车属性:cost、colour、price Product car = new Product("家用燃油车"); AttributeGroup priceGroup = new AttributeGroup("价格成本组"); priceGroup.add(new ProductAttribute("cost", 78000)); priceGroup.add(new ProductAttribute("price", 95900)); AttributeGroup appearanceGroup = new AttributeGroup("外观组"); appearanceGroup.add(new ProductAttribute("colour", "深空灰")); car.addAttributeComponent(priceGroup); car.addAttributeComponent(appearanceGroup); // 构造椅子属性:cost、width、profundity、stature,演示三层嵌套 Product chair = new Product("网布办公椅"); AttributeGroup sizeGroup = new AttributeGroup("尺寸参数组"); AttributeGroup seatSizeSubGroup = new AttributeGroup("座面尺寸子组"); seatSizeSubGroup.add(new ProductAttribute("width", 51)); seatSizeSubGroup.add(new ProductAttribute("profundity", 49)); sizeGroup.add(seatSizeSubGroup); sizeGroup.add(new ProductAttribute("stature", 118)); chair.addAttributeComponent(sizeGroup); chair.addAttributeComponent(new ProductAttribute("cost", 320)); // 执行打印 car.printAttributes(); chair.printAttributes(); } }
打印效果
运行后输出清晰展示层级、所属产品、归属分组路径:
============ 产品:家用燃油车 属性清单 ============ ├─ 【属性组:价格成本组】 │ ├─ 属性:cost = 78000 | 所属产品:家用燃油车 | 归属分组:价格成本组 │ ├─ 属性:price = 95900 | 所属产品:家用燃油车 | 归属分组:价格成本组 ├─ 【属性组:外观组】 │ ├─ 属性:colour = 深空灰 | 所属产品:家用燃油车 | 归属分组:外观组 ============ 产品:网布办公椅 属性清单 ============ ├─ 【属性组:尺寸参数组】 │ ├─ 【属性组:座面尺寸子组】 │ │ ├─ 属性:width = 51 | 所属产品:网布办公椅 | 归属分组:尺寸参数组 > 座面尺寸子组 │ │ ├─ 属性:profundity = 49 | 所属产品:网布办公椅 | 归属分组:尺寸参数组 > 座面尺寸子组 │ ├─ 属性:stature = 118 | 所属产品:网布办公椅 | 归属分组:尺寸参数组 ├─ 属性:cost = 320 | 所属产品:网布办公椅 | 归属分组:无(顶层属性)
内容的提问来源于stack exchange,提问作者Srinivas
相关产品推荐
相关产品推荐

