2017-06-01 22 views
0

少なくとも3つのデザインパターンを使用するプロジェクトを行う必要があります。私は最初にデコレータパターンでビルダーパターンを使用しようとしましたが、私の教師は両方のパターンを複合パターンで接続する必要があると私に教えてくれました。私はumlの構造を想像しようとしましたが、それは私にとっては難しすぎます。誰も私を助けることができますか?それらを接続する方法?デザインパターン:Builder + Decorator + Composite

答えて

1

gofは言う:

デコレータは一成分と縮重合成と見なすことができます。しかし、デコレータは、オブジェクトの集約を目的としたものではありません。

と:

Aコンポジット(163)は、ビルダーは、しばしば構築するものです。

ここではデコレータとのコンポジットです。ビルダーを追加しようとします。

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

Design Pattern Combination Image

レッツは、我々は多くのプロパティ(コンポーネント)今、このコンポーネントは、他の成分を含有することができますがあり複雑なオブジェクトを持っていると言います。したがって、Builderパターンを使用してこのComplexオブジェクトを作成すると、結果のオブジェクトはコンポジットになります そして、このコンポーネントは、デコレータデザインパターンを使用して追加の責任を適用することによってさらに変更できます。

コマンドとコマンド コンポーネントのグループ - コマンドは、コンポーネントを拡張 - 今すぐこのコンポーネントは以下のように渡される複数のパラメータを持つことができ、コマンド--sayエコーのリストが含まれているエコー、

をエコーし​​ます - バットコマンドエコー GroupOfCommandは、コンポーネントを拡張言います(); setArgument( "hello")。setArgument( "world")。build();ComponentBuilder()。setCommand( "echo")。

デコレータので Component.execute()メソッドは、デコレータ の一部であるWrapWithBrackerDecorator方法は

public String execute(){ 
return "(" +component.execute() +")"; 
} 
ようになり実行するコンポーネントを拡張し、コンポーネント を含有します
関連する問題