/*
* decorator in linked list 's point of view ??????
*/
class TestClass {
String desc;
TestClass tc;
public TestClass(TestClass tc, String desc) {
this.tc = tc;
this.desc = tc == null ? "" : tc.desc + desc;
}
public String getDesc() {
return desc;
}
}
public class Hello {
public static void main(String[] args) {
TestClass t = new TestClass(new TestClass(new TestClass(new TestClass(null, ""), "A"), "B"), "C");
System.out.println(t.getDesc());
TestClass t2 = new TestClass(null, "");
char a = 'A';
while (a <= 'Z') {
t2 = new TestClass(t2, new Character(a).toString());
a++;
}
System.out.println(t2.getDesc());
}
}
用linked list 来理解Decorator 模式
最新推荐文章于 2026-08-22 07:00:00 发布
本文通过一个具体的Java代码示例介绍了装饰者模式的链式调用过程。通过对类TestClass的设计与实例化,展示了如何利用装饰者模式来增强对象的功能,同时保持代码的灵活性和扩展性。

1304

被折叠的 条评论
为什么被折叠?



