mpj
mpj

Reputation: 357

Design Patterns: Builder + Decorator + Composite

I have to do a project that uses at least three design patterns. At first time I tried to use builder pattern with decorator pattern but my teacher told me that I should connect both of them with composite pattern. I tried to imagine a structure of uml but it is too hard for me. Could anybody help me? How to connect them?

Upvotes: 0

Views: 1757

Answers (2)

Ray Tayek
Ray Tayek

Reputation: 10003

gof says:

A decorator can be viewed as a degenerate composite with only one component. However, a decorator adds additional responsibilities—it isn't intended for object aggregation.

and:

A Composite (163) is what the builder often builds.

here is a composite with a decorator. try to add a builder.

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();
    }
}

Upvotes: 1

Nikhil Sharma
Nikhil Sharma

Reputation: 21

Design Pattern Combination Image

Let say we have a Complex Object which has many properties (Component) now this component can contain other components . So we can create this Complex object using a Builder Pattern and the resulting object will be a composite And now this component can be modified further by applying additional responsibilities using the Decorator Design Pattern

Let take an example of a command and a group of command Component -- Command extends Component - say a bat command echo GroupOfCommand extend Component - will contain list of commands --say echo , echo , echo

Now This component can have multiple parameter to be passed like ComponentBuilder().setCommand("echo").setArgument("hello").setArgument("world").build();

Decorator will extend Component and contain component so Component.execute() method will be the part of Decorator WrapWithBrackerDecorator execute method will be like

public String execute(){
 return "(" +component.execute() +")";
 }

Upvotes: 0

Related Questions