Design patterns 设计模式:Builder+Decorator+Composite
我必须做一个至少使用三种设计模式的项目。第一次,我尝试使用builder模式和decorator模式,但我的老师告诉我,我应该用复合模式将两者连接起来。我试图想象uml的结构,但这对我来说太难了。有人能帮我吗?如何连接它们?说: 装饰器可以看作是只有一个组件的退化复合物。然而,装饰者增加了额外的职责,而不是用于对象聚合 以及: 复合结构是构建者经常构建的 这是一个带有装饰器的合成图。尝试添加一个生成器Design patterns 设计模式:Builder+Decorator+Composite,design-patterns,decorator,builder,composite,Design Patterns,Decorator,Builder,Composite,我必须做一个至少使用三种设计模式的项目。第一次,我尝试使用builder模式和decorator模式,但我的老师告诉我,我应该用复合模式将两者连接起来。我试图想象uml的结构,但这对我来说太难了。有人能帮我吗?如何连接它们?说: 装饰器可以看作是只有一个组件的退化复合物。然而,装饰者增加了额外的职责,而不是用于对象聚合 以及: 复合结构是构建者经常构建的 这是一个带有装饰器的合成图。尝试添加一个生成器 package p; import java.util.ArrayList; import j
package p;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class So44300051 {
static abstract class Component {
Component(String name) {
this.name=name;
}
abstract boolean add(Component component);
abstract boolean remove(Component component);
abstract List<Component> children();
void visit() {
System.out.println(this);
if(this instanceof Decorator1)
((Decorator1)this).additionalBehaviour1();
for(Component child:children())
child.visit();
}
final String name;
}
static class Composite extends Component {
Composite(String name) {
super(name);
}
@Override boolean add(Component component) {
return children.add(component);
}
@Override boolean remove(Component component) {
return children.remove(component);
}
@Override List<Component> children() {
return Collections.unmodifiableList(children);
}
@Override public String toString() {
return "Composite [name="+name+"]";
}
final List<Component> children=new ArrayList<>();
}
static class Leaf extends Component {
Leaf(String name) {
super(name);
}
@Override boolean add(Component component) {
return false;
}
@Override boolean remove(Component component) {
return false;
}
@Override List<Component> children() {
return Collections.emptyList();
}
@Override public String toString() {
return "Leaf [name="+name+"]";
}
}
static abstract class Decorator extends Component {
Decorator(Component component) {
super(component.name);
this.component=component;
}
@Override boolean add(Component component) {
return this.component.add(component);
}
@Override boolean remove(Component component) {
return this.component.remove(component);
}
@Override List<Component> children() {
return this.component.children();
}
@Override public String toString() {
return getClass().getSimpleName()+" "+component;
}
final Component component;
}
static class Decorator1 extends Decorator {
Decorator1(Component component) {
super(component);
}
void additionalBehaviour1() {
System.out.println("additional behaviour 1.");
}
}
public static void main(String[] args) {
Component root=new Composite("root");
root.add(new Leaf("leaf 1"));
root.add(new Decorator1(new Leaf("leaf2")));
root.visit();
}
}
假设我们有一个复杂的对象,它有许多属性组件,现在这个组件可以包含其他组件。因此,我们可以使用构建器模式创建这个复杂的对象,得到的对象将是一个复合对象
现在,通过使用Decorator设计模式应用额外的职责,可以进一步修改这个组件
让我们以一个命令和一组命令为例
成分-
命令扩展组件-例如bat命令回显
GroupOfCommand扩展组件-将包含命令列表-例如echo、echo、echo
现在,这个组件可以像下面这样传递多个参数
ComponentBuilder.setCommandecho.setArgumenthello.setArgumentworld.build
装饰器将扩展组件并包含组件
所以
Component.execute方法将是Decorator的一部分
WrapWithBrackerDecorator执行方法如下
public String execute(){
return "(" +component.execute() +")";
}