外观模式

外观模式:http://www.javaeye.com/topic/327449

定义:它为子系统中的一组接口提供一个统一的高层接口。是的子系统更容易使用。

有一天女朋友想要去一个地方旅游。。旅游需要考虑很多很多的问题。如旅游的地点,航班,宾馆,饭店。我把所有的这些问题都扔给她了。。。假设简化旅游的行程。需要完成的步骤有

  • 计划旅游景点
  • 预定宾馆
  • 预定机票
  • 飞往目的地
  • 入住宾馆
  • 吃饭
  • 游玩

首先把旅游作为一个子系统,子系统包含4个类 ,分别如下。

航班Java代码
  1. package facade.demo;  
  2. /**
  3. * 航班
  4. * @author Nicholas
  5. *
  6. */  
  7. public class Flight {  
  8.     public void scheduled(){  
  9.          System.out.println("预定机票");  
  10.      }  
  11.       
  12.     public void go(){  
  13.          System.out.println("飞往目的地");  
  14.      }  
  15. }  
package facade.demo; /** * 航班 * @author Nicholas * */ public class Flight { public void scheduled(){ System.out.println("预定机票"); } public void go(){ System.out.println("飞往目的地"); } }
package facade.demo; /** * 饭店 * @author Nicholas * */ public class Restaurant { public void eating(){ System.out.println("吃饭"); } }

好了。。旅游真是个麻烦事情,我还是把这些麻烦扔给女朋友吧。。

女朋友Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20facade.demo%3B%0A%2F**%0A%20*%20%E6%88%91%E7%9A%84%E5%A5%B3%E6%9C%8B%E5%8F%8B%0A%20*%20%40author%20Nicholas%0A%20*%0A%20*%2F%0Apublic%20class%20Mygirl1%20%7B%0A%09private%20Flight%20flight%3B%0A%09private%20Hotel%20hotel%3B%0A%09private%20Place%20place%3B%0A%09private%20Restaurant%20restaurant%3B%0A%0A%09%0A%09public%20Mygirl1(Flight%20flight%2C%20Hotel%20hotel%2C%20Place%20place%2C%20Restaurant%20restaurant)%20%7B%0A%09%09this.flight%20%3D%20flight%3B%0A%09%09this.hotel%20%3D%20hotel%3B%0A%09%09this.place%20%3D%20place%3B%0A%09%09this.restaurant%20%3D%20restaurant%3B%0A%09%7D%0A%09%0A%09%0A%09public%20void%20travel()%7B%0A%09%09place.plan()%3B%0A%09%09hotel.booking()%3B%0A%09%09flight.scheduled()%3B%0A%09%09flight.go()%3B%0A%09%09hotel.check()%3B%0A%09%09restaurant.eating()%3B%0A%09%09place.play()%3B%0A%09%7D%0A%09%0A%09public%20static%20void%20main(String%5B%5D%20args)%7B%0A%09%09Flight%20flight%20%3D%20new%20Flight()%3B%0A%09%09Hotel%20hotel%20%3D%20new%20Hotel()%3B%0A%09%09Place%20place%20%3D%20new%20Place()%3B%0A%09%09Restaurant%20restaurant%20%3D%20new%20Restaurant()%3B%0A%09%09Mygirl1%20mygirl%20%3D%20new%20Mygirl1(flight%2Chotel%2Cplace%2Crestaurant)%3B%0A%09%09mygirl.travel()%3B%0A%09%09%0A%09%7D%0A%09%0A%7D%0A%0A" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1235113984027" lk_media="yes">
  1. package facade.demo;  
  2. /**
  3. * 我的女朋友
  4. * @author Nicholas
  5. *
  6. */  
  7. public class Mygirl1 {  
  8.     private Flight flight;  
  9.     private Hotel hotel;  
  10.     private Place place;  
  11.     private Restaurant restaurant;  
  12.   
  13.       
  14.     public Mygirl1(Flight flight, Hotel hotel, Place place, Restaurant restaurant) {  
  15.         this.flight = flight;  
  16.         this.hotel = hotel;  
  17.         this.place = place;  
  18.         this.restaurant = restaurant;  
  19.      }  
  20.       
  21.       
  22.     public void travel(){  
  23.          place.plan();  
  24.          hotel.booking();  
  25.          flight.scheduled();  
  26.          flight.go();  
  27.          hotel.check();  
  28.          restaurant.eating();  
  29.          place.play();  
  30.      }  
  31.       
  32.     public static void main(String[] args){  
  33.          Flight flight = new Flight();  
  34.          Hotel hotel = new Hotel();  
  35.          Place place = new Place();  
  36.          Restaurant restaurant = new Restaurant();  
  37.          Mygirl1 mygirl = new Mygirl1(flight,hotel,place,restaurant);  
  38.          mygirl.travel();  
  39.           
  40.      }  
  41.       
  42. }  
package facade.demo; /** * 我的女朋友 * @author Nicholas * */ public class Mygirl1 { private Flight flight; private Hotel hotel; private Place place; private Restaurant restaurant; public Mygirl1(Flight flight, Hotel hotel, Place place, Restaurant restaurant) { this.flight = flight; this.hotel = hotel; this.place = place; this.restaurant = restaurant; } public void travel(){ place.plan(); hotel.booking(); flight.scheduled(); flight.go(); hotel.check(); restaurant.eating(); place.play(); } public static void main(String[] args){ Flight flight = new Flight(); Hotel hotel = new Hotel(); Place place = new Place(); Restaurant restaurant = new Restaurant(); Mygirl1 mygirl = new Mygirl1(flight,hotel,place,restaurant); mygirl.travel(); } }

结果如下:

计划旅游景点
预定宾馆
预定机票
飞往目的地
入住宾馆
吃饭
游玩

以上都是我的想象了。。。女朋友就是上帝啊。。。。。哎。如果真的那样做。。估计女朋友非弄死我。。。

到这里我们发现了个问题。女朋友要旅游需要关心那么多问题。。这些对她来说太复杂了。。她并不关心细节。只要她答到旅游的目的就好了。。。这时候,解决麻烦的人出现了。。。对,那就好我,我就是那个所谓的"外观"。我来把这些问题解决掉,让她享受旅游的过程就好了。

我Java代码 <embed height="15" width="14" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20facade.demo%3B%0A%0A%2F**%0A%20*%20%E6%88%91(%E6%88%91%E5%B0%B1%E6%98%AF%E5%A4%96%E8%A7%82%E4%BA%86)%0A%20*%20%40author%20Nicholas%0A%20*%0A%20*%2F%0Apublic%20class%20ManFacade%20%7B%0A%09private%20Flight%20flight%3B%0A%09private%20Hotel%20hotel%3B%0A%09private%20Place%20place%3B%0A%09private%20Restaurant%20restaurant%3B%0A%0A%20%20%20%20%20%20%20public%20ManFacade()%7B%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20this.flight%20%3D%20new%20Flight()%3B%0A%09%09this.hotel%20%3D%20new%20Hotel()%3B%0A%09%09this.place%20%3D%20new%20Place()%3B%0A%09%09this.restaurant%20%3D%20new%20Restaurant()%3B%0A%20%20%20%20%20%20%26nbsp%3B%7D%0A%0A%09public%20ManFacade(Flight%20flight%2C%20Hotel%20hotel%2C%20Place%20place%2C%20Restaurant%20restaurant)%20%7B%0A%09%09this.flight%20%3D%20flight%3B%0A%09%09this.hotel%20%3D%20hotel%3B%0A%09%09this.place%20%3D%20place%3B%0A%09%09this.restaurant%20%3D%20restaurant%3B%0A%09%7D%0A%09%0A%09%0A%09public%20void%20travel()%7B%0A%09%09place.plan()%3B%0A%09%09hotel.booking()%3B%0A%09%09flight.scheduled()%3B%0A%09%09flight.go()%3B%0A%09%09hotel.check()%3B%0A%09%09restaurant.eating()%3B%0A%09%09place.play()%3B%0A%09%7D%0A%7D%0A" src="http://www.javaeye.com/javascripts/syntaxhighlighter/clipboard_new.swf" lk_mediaid="lk_juiceapp_mediaPopup_1235113984029" lk_media="yes">
  1. package facade.demo;  
  2.   
  3. /**
  4. * 我(我就是外观了)
  5. * @author Nicholas
  6. *
  7. */  
  8. public class ManFacade {  
  9.     private Flight flight;  
  10.     private Hotel hotel;  
  11.     private Place place;  
  12.     private Restaurant restaurant;  
  13.   
  14.        public ManFacade(){       
  15.               this.flight = new Flight();  
  16.         this.hotel = new Hotel();  
  17.         this.place = new Place();  
  18.         this.restaurant = new Restaurant();  
  19.         }  
  20.   
  21.     public ManFacade(Flight flight, Hotel hotel, Place place, Restaurant restaurant) {  
  22.         this.flight = flight;  
  23.         this.hotel = hotel;  
  24.         this.place = place;  
  25.         this.restaurant = restaurant;  
  26.      }  
  27.       
  28.       
  29.     public void travel(){  
  30.          place.plan();  
  31.          hotel.booking();  
  32.          flight.scheduled();  
  33.          flight.go();  
  34.          hotel.check();  
  35.          restaurant.eating();  
  36.          place.play();  
  37.      }  
  38. }  
package facade.demo; /** * 我(我就是外观了) * @author Nicholas * */ public class ManFacade { private Flight flight; private Hotel hotel; private Place place; private Restaurant restaurant; public ManFacade(){ this.flight = new Flight(); this.hotel = new Hotel(); this.place = new Place(); this.restaurant = new Restaurant(); } public ManFacade(Flight flight, Hotel hotel, Place place, Restaurant restaurant) { this.flight = flight; this.hotel = hotel; this.place = place; this.restaurant = restaurant; } public void travel(){ place.plan(); hotel.booking(); flight.scheduled(); flight.go(); hotel.check(); restaurant.eating(); place.play(); } }

看来麻烦解决掉了。女朋友很轻松的享受旅游过程了。。。。

Java代码
  1. package facade.demo;  
  2.   
  3. /**
  4. * 女朋友
  5. * @author Nicholas
  6. *
  7. */  
  8. public class Mygirl {  
  9.   
  10.     public static void main(String[] args){  
  11.          Restaurant restaurant = new Restaurant();  
  12.          ManFacade manFacade = new ManFacade();//我来解决掉这些麻烦。。。  
  13.          manFacade.travel();//旅游成功  
  14.      }  
  15.       
  16. }  
package facade.demo; /** * 女朋友 * @author Nicholas * */ public class Mygirl { public static void main(String[] args){ Restaurant restaurant = new Restaurant(); ManFacade manFacade = new ManFacade();//我来解决掉这些麻烦。。。 manFacade.travel();//旅游成功 } }

结果如下:

计划旅游景点
预定宾馆
预定机票
飞往目的地
入住宾馆
吃饭
游玩

本身外观模式的作用就是让接口更容易使用。真实的场景要复杂的多,这个例子只能提供个表面的意思。

总的来说,外观模式就是为了使接口更简单。。。

posted on 2009-02-20 15:12  dhj  阅读(136)  评论(0编辑  收藏  举报

导航