设计模式笔记:适配器、迭代器、外观、组合模式

来源:互联网 发布:大数据架构师 认证 编辑:程序博客网 时间:2024/05/16 17:41

一、适配器模式

将一个接口转换成客户希望的另一个接口,适配器模式使接口不兼容的那些类可以一起工作,其别名为包装器(Wrapper)。适配器模式既可以作为类结构型模式,也可以作为对象结构型模式。

通常情况下,客户端可以通过目标类的接口访问它所提供的服务。有时,现有的类可以满足客户类的功能需要,但是它所提供的接口不一定是客户类所期望的,这可能是因为现有类中方法名与目标类中定义的方法名不一致等原因所导致的。

在这种情况下,现有的接口需要转化为客户类期望的接口,这样保证了对现有类的重用

如果不进行这样的转化,客户类就不能利用现有类所提供的功能,适配器模式可以完成这样的转化。

适配器提供客户类需要的接口,适配器的实现就是把客户类的请求转化为对适配者的相应接口的调用

适配器可以使由于接口不兼容而不能交互的类可以一起工作

适配器模式分为类适配器和对象适配器两种,类适配器需要支持多重继承的语言来实现,对于不支持多重继承的语言就不能这样做,但是视乎情况可以尝试使用内部类去解决多重继承的问题,看看类图:

(1)、类适配器:


(2)、对象适配器:


设有鸭子类和火鸡类,现在把火鸡和鸭子行为进行适配

下面是他们各自的接口以及实现

public interface Duck {public void quack();public void fly();}public interface Turkey {public void gobble();public void fly();}public class MallardDuck implements Duck {@Overridepublic void quack() {System.out.println("Duck Quack");}@Overridepublic void fly() {System.out.println("Duck flying");}}public class WildTurkey implements Turkey {@Overridepublic void gobble() {System.out.println("Turkey gooble");}@Overridepublic void fly() {System.out.println("Turkey flying");}}


到了这里,我们要把他们进行适配,把火鸡的行为放到鸭子中来:

public class TurkeyAdapter implements Duck {Turkey turkey;public TurkeyAdapter(Turkey turkey) {this.turkey = turkey;}@Overridepublic void quack() {turkey.gobble();}@Overridepublic void fly() {for(int i = 0;i<5;i++){turkey.fly();}}//为了方便我在这里写测试public static void main(String[] args) {//实例化两个实体类MallardDuck duck = new MallardDuck();WildTurkey turkey = new WildTurkey();//实例化适配器Duck tuckeyAdapter = new TurkeyAdapter(turkey);//正常状态下调用火鸡行为System.out.println("The Turkey says...");turkey.gobble();turkey.fly();//正常状态下调用鸭子行为System.out.println("\nThe Duck says...");testDuck(duck);//利用适配器调用火鸡行为System.out.println("\nThe adapter says...");testDuck(tuckeyAdapter);}//我们把火鸡当作鸭子使用了static void testDuck(Duck duck){duck.quack();duck.fly();}}//The Turkey says...//Turkey gooble//Turkey flying////The Duck says...//Duck Quack//Duck flying////The adapter says...//Turkey gooble//Turkey flying//Turkey flying//Turkey flying//Turkey flying//Turkey flying
像这样子的使用方法就是对象适配器,而平常在java中的应用:iterator(后面也可以用作迭代器)

旧版枚举接口:Enumeration,它要求实现两个方法

hasMoreElement()是否有下一个元素

nextElement()下一个元素

但是在后面推出了Iterator之后,我们希望访问Itertor的方式去访问旧的枚举接口,那么我们可以写一个适配器去实现:

public class EnumerationIterator implements Iterator{Enumeration e;public EnumerationIterator(Enumeration e) {this.e = e;}@Overridepublic boolean hasNext() {return e.hasMoreElements();}@Overridepublic Object next() {return e.nextElement();}@Overridepublic void remove() {throw new UnsupportedOperationException();}}


适配器模式除了在java中iterator的应用,在android中也有应用:ListView与adapter

我们知道,作为最重要的View,ListView需要能够显示各式各样的视图,每个人需要的显示效果各不相同,显示的数据类型、数量等也千变万化。那么如何隔离这种变化尤为重要

android中适配器隔离了用户需要的变化,他不算是很严谨的适配器模式,从功能上来说,他把各种各样用户的需求适配到了listview上

参考文章:http://www.codeceo.com/article/android-listview-adapter-2.html


优点:

1、将目标类和适配者类解耦,通过引入一个适配器类来重用现有的适配者类,而无须修改原有代码
2、增加了类的透明性和复用性,将具体的实现封装在适配者类中,对于客户端类来说是透明的,而且提高了适配者的复用性。
3、灵活性和扩展性都非常好,通过使用配置文件,可以很方便地更换适配器,也可以在不修改原有代码的基础上增加新的适配器类,完全符合“开闭原则”
4、对于对象适配器:一个对象适配器可以把多个不同的适配者适配到同一个目标,也就是说,同一个适配器可以把适配者类和它的子类都适配到目标接口


缺点:
与类适配器模式相比,要想置换适配者类的方法就不容易。如果一定要置换掉适配者类的一个或多个方法,就只好先做一个适配者类的子类,将适配者类的方法置换掉,然后再把适配者类的子类当做真正的适配者进行适配,实现过程较为复杂

二、迭代器模式

在上面已经提到过适配器模式中Iterator,在java中已经给我们提供了这个接口,所以在这里顺势总结迭代器,假如现在要求遍历两种不同的菜单,那么为了方便,给他们实现迭代器模式是最好的选择

迭代器模式:提供一种方法顺序访问一个聚合对象中的各个元素,而又不暴露该对象的内部表示(别名:游标  Cursor)

1、不使用java的Iterator,自定义迭代器:有两家菜单,他们提供不同的遍历方法,这令服务员很困扰,这时候需要提供一个迭代器的接口减少代码量,降低耦合:


首先准备好需要的实体类:MenuItem,上图没有给出,他描述的是菜单的基本属性,菜单项,当然菜单是由菜单项组成的

public class MenuItem {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 String toString() {return (name + ", $" + price + "\n   " + description);}}

接着,我们定义迭代器,同时实现两份菜单的迭代器:我们现在不需要了解数据源从哪里来,他可以是数组,也可以是list,因为主菜单对遍历数据有不同的实现形式,我们只是提供一个统一的接口,这就是把服务员跟菜单解耦了,他不用管菜单的详细实现,因为他在迭代器中能统一访问,变化的代码也交由迭代器托管

下面就是一个是数组,一个是list

public interface Iterator {boolean hasNext();Object next();}public class DinerMenuIterator implements Iterator {MenuItem[] items;int position = 0; public DinerMenuIterator(MenuItem[] items) {this.items = items;} public Object next() {MenuItem menuItem = items[position];position = position + 1;return menuItem;} public boolean hasNext() {if (position >= items.length || items[position] == null) {return false;} else {return true;}}}public class PancakeHouseMenuIterator implements Iterator {ArrayList items;int position = 0; public PancakeHouseMenuIterator(ArrayList items) {this.items = items;} public Object next() {Object object = items.get(position);position = position + 1;return object;} public boolean hasNext() {if (position >= items.size()) {return false;} else {return true;}}}

看看菜单代码:可以看到他们是不一样的实现方法,但是他们都提供了一个迭代器方法:public Iterator createIterator(),服务员就是通过它访问菜单

public class DinerMenu{static final int MAX_ITEMS = 6;int numberOfItems = 0;MenuItem[] menuItems;  public DinerMenu() {menuItems = new MenuItem[MAX_ITEMS]; addItem("Vegetarian BLT","(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);addItem("BLT","Bacon with lettuce & tomato on whole wheat", false, 2.99);addItem("Soup of the day","Soup of the day, with a side of potato salad", false, 3.29);addItem("Hotdog","A hot dog, with saurkraut, relish, onions, topped with cheese",false, 3.05);addItem("Steamed Veggies and Brown Rice","Steamed vegetables over brown rice", true, 3.99);addItem("Pasta","Spaghetti with Marinara Sauce, and a slice of sourdough bread",true, 3.89);}  public void addItem(String name, String description,                      boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);if (numberOfItems >= MAX_ITEMS) {System.err.println("Sorry, menu is full!  Can't add item to menu");} else {menuItems[numberOfItems] = menuItem;numberOfItems = numberOfItems + 1;}} public MenuItem[] getMenuItems() {return menuItems;}  public Iterator createIterator() {return new DinerMenuIterator(menuItems);} // other menu methods here}public class PancakeHouseMenu{ArrayList menuItems; public PancakeHouseMenu() {menuItems = new ArrayList();    addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true,2.99); addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false,2.99); addItem("Blueberry Pancakes","Pancakes made with fresh blueberries",true,3.49); addItem("Waffles","Waffles, with your choice of blueberries or strawberries",true,3.59);}public void addItem(String name, String description,                    boolean vegetarian, double price){MenuItem menuItem = new MenuItem(name, description, vegetarian, price);menuItems.add(menuItem);} public ArrayList getMenuItems() {return menuItems;}  public Iterator createIterator() {return new PancakeHouseMenuIterator(menuItems);}  public String toString() {return "Objectville Pancake House Menu";}// other menu methods here}
这时候,迭代器模式完成了,下面服务员的代码只是访问菜单:

public class Waitress {PancakeHouseMenu pancakeHouseMenu;DinerMenu dinerMenu; public Waitress(PancakeHouseMenu pancakeHouseMenu, DinerMenu dinerMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;} public void printMenu() {//打印每一项Iterator pancakeIterator = pancakeHouseMenu.createIterator();Iterator dinerIterator = dinerMenu.createIterator();System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);} private void printMenu(Iterator iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();System.out.print(menuItem.getName() + ", ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}} public void printVegetarianMenu() {//打印素食菜单项printVegetarianMenu(pancakeHouseMenu.createIterator());printVegetarianMenu(dinerMenu.createIterator());} public boolean isItemVegetarian(String name) {//判断是否为素食Iterator breakfastIterator = pancakeHouseMenu.createIterator();if (isVegetarian(name, breakfastIterator)) {return true;}Iterator dinnerIterator = dinerMenu.createIterator();if (isVegetarian(name, dinnerIterator)) {return true;}return false;}private void printVegetarianMenu(Iterator iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();if (menuItem.isVegetarian()) {System.out.print(menuItem.getName());System.out.println("\t\t" + menuItem.getPrice());System.out.println("\t" + menuItem.getDescription());}}}private boolean isVegetarian(String name, Iterator iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();if (menuItem.getName().equals(name)) {if (menuItem.isVegetarian()) {return true;}}}return false;}}

测试方法:

public class MenuTestDrive {public static void main(String args[]) {        PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();        DinerMenu dinerMenu = new DinerMenu(); Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu); waitress.printMenu();}public static void printMenu() {PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();DinerMenu dinerMenu = new DinerMenu();ArrayList breakfastItems = pancakeHouseMenu.getMenuItems(); for (int i = 0; i < breakfastItems.size(); i++) {MenuItem menuItem = (MenuItem)breakfastItems.get(i);System.out.print(menuItem.getName());System.out.println("\t\t" + menuItem.getPrice());System.out.println("\t" + menuItem.getDescription());}MenuItem[] lunchItems = dinerMenu.getMenuItems(); for (int i = 0; i < lunchItems.length; i++) {MenuItem menuItem = lunchItems[i];System.out.print(menuItem.getName());System.out.println("\t\t" + menuItem.getPrice());System.out.println("\t" + menuItem.getDescription());}}}


2、利用java的迭代器,依然使用上面的例子,不一样的是,java的iterator有remove方法(如果看过源码,发现iterator接口里面就只有三个方法hasNext、next、remove),数组没有自带的iterator方法,所以要给他实现迭代器,而arraylist犹自带的iterator方法,就不需要在写一个迭代器:menuitem上面有

public interface Menu {public Iterator createIterator();}public class DinerMenu implements Menu {static final int MAX_ITEMS = 6;int numberOfItems = 0;MenuItem[] menuItems;  public DinerMenu() {menuItems = new MenuItem[MAX_ITEMS]; addItem("Vegetarian BLT","(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99);addItem("BLT","Bacon with lettuce & tomato on whole wheat", false, 2.99);addItem("Soup of the day","Soup of the day, with a side of potato salad", false, 3.29);addItem("Hotdog","A hot dog, with saurkraut, relish, onions, topped with cheese",false, 3.05);addItem("Steamed Veggies and Brown Rice","Steamed vegetables over brown rice", true, 3.99);addItem("Pasta","Spaghetti with Marinara Sauce, and a slice of sourdough bread",true, 3.89);}  public void addItem(String name, String description,                      boolean vegetarian, double price) {MenuItem menuItem = new MenuItem(name, description, vegetarian, price);if (numberOfItems >= MAX_ITEMS) {System.err.println("Sorry, menu is full!  Can't add item to menu");} else {menuItems[numberOfItems] = menuItem;numberOfItems = numberOfItems + 1;}} public MenuItem[] getMenuItems() {return menuItems;}  public Iterator createIterator() {return new DinerMenuIterator(menuItems);//return new AlternatingDinerMenuIterator(menuItems);} // other menu methods here}public class PancakeHouseMenu implements Menu {ArrayList menuItems; public PancakeHouseMenu() {menuItems = new ArrayList();    addItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true,2.99); addItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false,2.99); addItem("Blueberry Pancakes","Pancakes made with fresh blueberries, and blueberry syrup",true,3.49); addItem("Waffles","Waffles, with your choice of blueberries or strawberries",true,3.59);}public void addItem(String name, String description,                    boolean vegetarian, double price){MenuItem menuItem = new MenuItem(name, description, vegetarian, price);menuItems.add(menuItem);} public ArrayList getMenuItems() {return menuItems;}  public Iterator createIterator() {return menuItems.iterator();}  // other menu methods here}

数组需要迭代器

简单提及一下:ArrayList继承AbstractList,之上的容器实现了Collection,而Collection通过继承Iterable有iterator的方法,在内部已经实现了iterator方法,不用自己去写

public class DinerMenuIterator implements Iterator {MenuItem[] list;int position = 0; public DinerMenuIterator(MenuItem[] list) {this.list = list;} public Object next() {MenuItem menuItem = list[position];position = position + 1;return menuItem;} public boolean hasNext() {if (position >= list.length || list[position] == null) {return false;} else {return true;}}  public void remove() {if (position <= 0) {throw new IllegalStateException("You can't remove an item until you've done at least one next()");}if (list[position-1] != null) {for (int i = position-1; i < (list.length-1); i++) {list[i] = list[i+1];}list[list.length-1] = null;}}}
public class Waitress {Menu pancakeHouseMenu;Menu dinerMenu; public Waitress(Menu pancakeHouseMenu, Menu dinerMenu) {this.pancakeHouseMenu = pancakeHouseMenu;this.dinerMenu = dinerMenu;} public void printMenu() {Iterator pancakeIterator = pancakeHouseMenu.createIterator();Iterator dinerIterator = dinerMenu.createIterator();System.out.println("MENU\n----\nBREAKFAST");printMenu(pancakeIterator);System.out.println("\nLUNCH");printMenu(dinerIterator);} private void printMenu(Iterator iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();System.out.print(menuItem.getName() + ", ");System.out.print(menuItem.getPrice() + " -- ");System.out.println(menuItem.getDescription());}} public void printVegetarianMenu() {System.out.println("\nVEGETARIAN MENU\n----\nBREAKFAST");printVegetarianMenu(pancakeHouseMenu.createIterator());System.out.println("\nLUNCH");printVegetarianMenu(dinerMenu.createIterator());} public boolean isItemVegetarian(String name) {Iterator pancakeIterator = pancakeHouseMenu.createIterator();if (isVegetarian(name, pancakeIterator)) {return true;}Iterator dinerIterator = dinerMenu.createIterator();if (isVegetarian(name, dinerIterator)) {return true;}return false;}private void printVegetarianMenu(Iterator iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();if (menuItem.isVegetarian()) {System.out.print(menuItem.getName());System.out.println("\t\t" + menuItem.getPrice());System.out.println("\t" + menuItem.getDescription());}}}private boolean isVegetarian(String name, Iterator iterator) {while (iterator.hasNext()) {MenuItem menuItem = (MenuItem)iterator.next();if (menuItem.getName().equals(name)) {if (menuItem.isVegetarian()) {return true;}}}return false;}}public class MenuTestDrive {public static void main(String args[]) {PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu();DinerMenu dinerMenu = new DinerMenu();Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu);waitress.printMenu();waitress.printVegetarianMenu();System.out.println("\nCustomer asks, is the Hotdog vegetarian?");System.out.print("Waitress says: ");if (waitress.isItemVegetarian("Hotdog")) {System.out.println("Yes");} else {System.out.println("No");}System.out.println("\nCustomer asks, are the Waffles vegetarian?");System.out.print("Waitress says: ");if (waitress.isItemVegetarian("Waffles")) {System.out.println("Yes");} else {System.out.println("No");}}}


适用性:

访问一个聚合对象的内容而无需暴露它的内部表示
支持对聚合对象的多种遍历
为遍历不同的聚合结构提供一个统一的接口


这是迭代器模式的类图:书上的好看一点哈哈



三、组合模式

定义:将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。



上面是设计模式里面的图

现在在迭代器上扩展,如果菜单上不只是菜单项,而且有另外一个菜单,而菜单里面有菜单项

这就复杂很多了,包括使用递归,这里将会组合迭代器:



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

空迭代器关联菜单项

public class NullIterator implements Iterator {   public Object next() {return null;}  public boolean hasNext() {return false;}   public void remove() {throw new UnsupportedOperationException();}}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 component = (MenuComponent) iterator.next();if (component instanceof Menu) {stack.push(component.createIterator());} return component;} else {return null;}}  public boolean hasNext() {if (stack.empty()) {return false;} else {Iterator iterator = (Iterator) stack.peek();if (!iterator.hasNext()) {stack.pop();return hasNext();} else {return true;}}}   public void remove() {throw new UnsupportedOperationException();}}


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

执行思路:

allMenu先添加了三个子菜单,然后在next的过程中逐个进入子菜单,打印子菜单的菜单项,然后再到下一个子菜单,以此类推

适用性:

1.你想表示对象的部分-整体层次结构
2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

四、外观模式

定义:为子系统中的一组接口提供一个一致的界面,定义一个高层接口,这个接口使得这一子系统更加容易使用。其实也就是客户通过一个接口来访问多个子系统里面的资源



在以下情况下可以考虑使用外观模式:
(1)设计初期阶段,应该有意识的将不同层分离,层与层之间建立外观模式。
(2) 开发阶段,子系统越来越复杂,增加外观模式提供一个简单的调用接口。
(3) 维护一个大型遗留系统的时候,可能这个系统已经非常难以维护和扩展,但又包含非常重要的功能,为其开发一个外观类,以便新系统与其交互


比如我希望按下一个按钮,家里面的电视、音响、风扇、灯光全都打开,那么这个按钮就是提供的一个简单接口,而其他灯光、电视等就是各个子系统的接口

所以外观模式存在这样的特点:

外观模式中,一个子系统的外部与其内部的通信通过一个统一的外观类进行,外观类将客户类与子系统的内部复杂性分隔开,使得客户类只需要与外观角色打交道,而不需要与子系统内部的很多对象打交道。

优点
它对客户端屏蔽了子系统组件,减少了客户端所需处理的对象数目,并使得子系统使用起来更加容易。通过引入外观模式,客户端代码将变得很简单,与之关联的对象也很少。
它实现了子系统与客户端之间的松耦合关系,这使得子系统的变化不会影响到调用它的客户端,只需要调整外观类即可。
一个子系统的修改对其他子系统没有任何影响,而且子系统内部变化也不会影响到外观对象。
 
缺点
不能很好地限制客户端直接使用子系统类,如果对客户端访问子系统类做太多的限制则减少了可变性和灵活性。
如果设计不当,增加新的子系统可能需要修改外观类的源代码,违背了开闭原则。


适用性:
当要为访问一系列复杂的子系统提供一个简单入口时可以使用外观模式。
客户端程序与多个子系统之间存在很大的依赖性。引入外观类可以将子系统与客户端解耦,从而提高子系统的独立性和可移植性。
 在层次化结构中,可以使用外观模式定义系统中每一层的入口,层与层之间不直接产生联系,而通过外观类建立联系,降低层之间的耦合度


参考:http://blog.csdn.net/lovelion/article/details/8259789



0 0
原创粉丝点击