デザインパターン(11) - の組み合わせモード

コンビネーション(合成)モードでは、「全体/部分」階層を表すためにツリー構造へのオブジェクトにことができます。組み合わせは、一貫性のある方法で、顧客は個々のオブジェクトとそれらの組み合わせで対処することができます。

これは、ビット抽象的に聞こえるかもしれ、私たちは前の記事のイテレータパターンの詳細な基本的な例を続け、我々は秘密の組み合わせモードを見てみましょう。

前回の記事では、我々はレストランのメニュー、メニューパンケーキハウスやカフェメニューを実現し、そして今、私たちはレストランのメニューの「サブメニュー」のデザートを追加したいと考えています。

その結果、私たちは私たちのデザインを変更する必要があり、私たちは今、メニュー、サブメニューとメニュー項目を収容することができ、ツリー構造のいくつかの種類を必要とします。私たちは、各メニューアイテムに決定するために、個々の間で歩くことができるようにする必要がありますが、少なくともそれが今と同じように簡単にイテレータを持ちます。また、メニュー項目間を歩くことができるように、より多くの柔軟性を必要としています。たとえば、あなただけのデザートメニューを横断する必要があるかもしれませんか、(付属のデザートメニューを含む)全体のレストランのメニューをたどることができます。

メニューの組み合わせモードを適用する方法で見てみましょう。最初に、我々は、作成する必要があるコンポーネントのインタフェースを、我々は統一されたアプローチとメニューとメニュー項目に対処することができるようにすることを、メニューとメニュー項目に共通のインターフェースとして。言い換えれば、我々は、メニューまたはメニュー項目の同じメソッドを呼び出すことができます。ロールメニューコンポーネントは、リーフノードと組み合わせノードに対して共通のインタフェースを提供することです。

次のカテゴリの図の組み合わせパターン:

コンポーネントインタフェースとして

import java.util.*;

public abstract class MenuComponent {
   
	public void add(MenuComponent menuComponent) {
		throw new UnsupportedOperationException();
	}
	public void remove(MenuComponent menuComponent) {
		throw new UnsupportedOperationException();
	}
	public MenuComponent getChild(int i) {
		throw new UnsupportedOperationException();
	}
  
	public String getName() {
		throw new UnsupportedOperationException();
	}
	public String getDescription() {
		throw new UnsupportedOperationException();
	}
	public double getPrice() {
		throw new UnsupportedOperationException();
	}
	public boolean isVegetarian() {
		throw new UnsupportedOperationException();
	}

	public abstract Iterator createIterator();
 
	public void print() {
		throw new UnsupportedOperationException();
	}
}

今、私たちは、要素の組み合わせの中に動作を実装し、葉のクラス図の組み合わせで、メニュー項目のクラス、見てください。

import java.util.Iterator;

public class MenuItem extends MenuComponent {
 
	String name;
	String description;
	boolean vegetarian;
	double price;
    
	public MenuItem(String name, 
	                String description, 
	                boolean vegetarian, 
	                double price) 
	{ 
		this.name = name;
		this.description = description;
		this.vegetarian = vegetarian;
		this.price = price;
	}
  
	public String getName() {
		return name;
	}
  
	public String getDescription() {
		return description;
	}
  
	public double getPrice() {
		return price;
	}
  
	public boolean isVegetarian() {
		return vegetarian;
	}

	public Iterator createIterator() {
		return new NullIterator();
	}
 
	public void print() {
		System.out.print(" " + getName());
		if (isVegetarian()) {
			System.out.print("(v)");
		}
		System.out.println(", " + getPrice());
		System.out.println(" -- " + getDescription());
	}
}

我々はすでに、あなたはまた、カテゴリー、すなわちメニューの組み合わせを必要とする、メニュー項目があります。クラスの組み合わせは、他のメニューまたはメニュー項目を保持することができます。

import java.util.Iterator;
import java.util.ArrayList;

public class Menu extends MenuComponent {
 
	ArrayList menuComponents = new ArrayList();
	String name;
	String description;
  
	public Menu(String name, String description) {
		this.name = name;
		this.description = description;
	}
 
	public void add(MenuComponent menuComponent) {
		menuComponents.add(menuComponent);
	}
 
	public void remove(MenuComponent menuComponent) {
		menuComponents.remove(menuComponent);
	}
 
	public MenuComponent getChild(int i) {
		return (MenuComponent)menuComponents.get(i);
	}
 
	public String getName() {
		return name;
	}
 
	public String getDescription() {
		return description;
	}

  
	public Iterator createIterator() {
		return new CompositeIterator(menuComponents.iterator());
	}
 
 
	public void print() {
		System.out.print("\n" + getName());
		System.out.println(", " + getDescription());
		System.out.println("---------------------");
  
		Iterator iterator = menuComponents.iterator();
		while (iterator.hasNext()) {
			MenuComponent menuComponent = 
				(MenuComponent)iterator.next();
			menuComponent.print();
		}
	}
}

我々はまた、アセンブリ内のメニュー項目を横断するための組合せイテレータを実装し、すべてのサブメニュー(およびサブサブメニューが......)が含まれていることを確認しました。

import java.util.*;
  
public class CompositeIterator implements Iterator {
	Stack stack = new Stack();
   
	public CompositeIterator(Iterator iterator) {//将要遍历的顶层组合的迭代器传入,把它抛进一个堆栈数据结构中
		stack.push(iterator);
	}
   
	public Object next() {
		if(hasNext()) {
			Iterator iterator = (Iterator) stack.peek();
			MenuComponent menuComponent = (MenuComponent) iterator.next();
			if(menuComponent instanceof Menu) {//如果元素是一个菜单,则有了另一个需要被包含进遍历中的组合,把它也压入栈中
				stack.push(menuComponent.createIterator());
			}
			return menuComponent;
		}
		else {
			return null;
		}
	}
  
	public boolean hasNext() {
		if(stack.isEmpty()) {
			return false;
		}
		else {
			if(((Iterator)stack.peek()).hasNext()) {
				return true;
			}
			else {
				stack.pop();
				return hasNext();
			}
		}
	}
   
	public void remove() {
		throw new UnsupportedOperationException();
	}
}

また、時間がない事内のメニュー項目が横断できたときに、空のイテレータを実装して、空のイテレータそれのhasNext()は常にfalseを返し返す必要があります

import java.util.Iterator;
  
public class NullIterator implements Iterator {
   
	public Object next() {
		return null;
	}
  
	public boolean hasNext() {
		return false;
	}
   
	public void remove() {
		throw new UnsupportedOperationException();
	}
}

今はウェイトレスを達成し、そして彼女はベジタリアン方法であるアイテム達を正確に伝えるためにのために缶を追加することができます。

import java.util.Iterator;
  
public class Waitress {
	MenuComponent allMenus;
 
	public Waitress(MenuComponent allMenus) {
		this.allMenus = allMenus;
	}
 
	public void printMenu() {
		allMenus.print();
	}
  
	public void printVegetarianMenu() {
		Iterator iterator = allMenus.createIterator();

		System.out.println("\nVEGETARIAN MENU\n----");
		while (iterator.hasNext()) {
			MenuComponent menuComponent = 
					(MenuComponent)iterator.next();
			try {
				if (menuComponent.isVegetarian()) {
					menuComponent.print();
				}
			} catch (UnsupportedOperationException e) {}
		}
	}
}

最後に、テストへ

public class MenuTestDrive {
	public static void main(String args[]) {

		MenuComponent pancakeHouseMenu = 
			new Menu("PANCAKE HOUSE MENU", "Breakfast");
		MenuComponent dinerMenu = 
			new Menu("DINER MENU", "Lunch");
		MenuComponent cafeMenu = 
			new Menu("CAFE MENU", "Dinner");
		MenuComponent dessertMenu = 
			new Menu("DESSERT MENU", "Dessert of course!");
  
		MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined");
  
		allMenus.add(pancakeHouseMenu);
		allMenus.add(dinerMenu);
		allMenus.add(cafeMenu);
  
		pancakeHouseMenu.add(new MenuItem(
			"K&B's Pancake Breakfast", 
			"Pancakes with scrambled eggs, and toast", 
			true,
			2.99));
		pancakeHouseMenu.add(new MenuItem(
			"Regular Pancake Breakfast", 
			"Pancakes with fried eggs, sausage", 
			false,
			2.99));
		pancakeHouseMenu.add(new MenuItem(
			"Blueberry Pancakes",
			"Pancakes made with fresh blueberries, and blueberry syrup",
			true,
			3.49));
		pancakeHouseMenu.add(new MenuItem(
			"Waffles",
			"Waffles, with your choice of blueberries or strawberries",
			true,
			3.59));

		dinerMenu.add(new MenuItem(
			"Vegetarian BLT",
			"(Fakin') Bacon with lettuce & tomato on whole wheat", 
			true, 
			2.99));
		dinerMenu.add(new MenuItem(
			"BLT",
			"Bacon with lettuce & tomato on whole wheat", 
			false, 
			2.99));
		dinerMenu.add(new MenuItem(
			"Soup of the day",
			"A bowl of the soup of the day, with a side of potato salad", 
			false, 
			3.29));
		dinerMenu.add(new MenuItem(
			"Hotdog",
			"A hot dog, with saurkraut, relish, onions, topped with cheese",
			false, 
			3.05));
		dinerMenu.add(new MenuItem(
			"Steamed Veggies and Brown Rice",
			"A medly of steamed vegetables over brown rice", 
			true, 
			3.99));
 
		dinerMenu.add(new MenuItem(
			"Pasta",
			"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
			true, 
			3.89));
   
		dinerMenu.add(dessertMenu);
  
		dessertMenu.add(new MenuItem(
			"Apple Pie",
			"Apple pie with a flakey crust, topped with vanilla icecream",
			true,
			1.59));
		dessertMenu.add(new MenuItem(
			"Cheesecake",
			"Creamy New York cheesecake, with a chocolate graham crust",
			true,
			1.99));
		dessertMenu.add(new MenuItem(
			"Sorbet",
			"A scoop of raspberry and a scoop of lime",
			true,
			1.89));

		cafeMenu.add(new MenuItem(
			"Veggie Burger and Air Fries",
			"Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
			true, 
			3.99));
		cafeMenu.add(new MenuItem(
			"Soup of the day",
			"A cup of the soup of the day, with a side salad",
			false, 
			3.69));
		cafeMenu.add(new MenuItem(
			"Burrito",
			"A large burrito, with whole pinto beans, salsa, guacamole",
			true, 
			4.29));
 
		Waitress waitress = new Waitress(allMenus);
   
		waitress.printVegetarianMenu();
 
	}
}

ここでの結果は、

設計解析

これは、前回の記事で紹介して組み合わせパターンが合わないことは注目に値する一つの責任体制をこのクラスは、2つの責任を持つようにモードを組み合わせることで、階層を管理する必要がなく、[アクション]メニューの実装だけではなく。だから、と引き換えに、責任の単一モード設計原理の組み合わせを話すこと、透明性(透明性)コンポーネントインタフェースを可能にすることによって、いわゆる透明性、運用管理サブノードとリーフノードの数は、顧客が均等に合わせて、リーフノード処理することができる含んでいます。換言すれば、要素またはそれが葉ノードであるか否かの組み合わせは、顧客に透明です

公開された295元の記事 ウォン称賛37 ビュー30000 +

おすすめ

転載: blog.csdn.net/tianshan2010/article/details/104714063