好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#设计模式之Facade外观模式解决天河城购物问题示例

本文实例讲述了c#设计模式之facade外观模式解决天河城购物问题。分享给大家供大家参考,具体如下:

一、理论定义

外观模式   把  分散的子系统,集合成一个系统,提供一站式服务。

二、应用举例

需求描述: 聂小倩 和 宁采臣是一对小富则安 的聊斋夫妻。住在比较偏远的小乡村。
今天,两人初次来到大城市广州,听说天河城提供一站式服务,不像小城市那样,买个东西  得  东奔西跑。
在一个地方,就可以买到 自己想要的衣服,电脑,鞋子,iphone,还可以看大片,
吃冰淇淋,吃真功夫,买化妆品,珠宝首饰。天河城,果然是一宝地啊。
ok,边走边看。

三、具体编码

1.阿迪达斯

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 阿迪达斯

   /// </summary>

   public class adidas

   {

     public void serivce( string something) {

       console.writeline( "在阿迪达斯购买了: " +something);

     }

   }

}

2.飞扬影城

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 飞扬影城

   /// </summary>

   public class feiyangmovie

   {

     public void serivce( string something)

     {

       console.writeline( "在飞扬影城看了一部电影: " + something);

     }

   }

}

3.国美电器

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 国美电器

   /// </summary>

   public class gome

   {

     public void serivce( string something)

     {

       console.writeline( "在国美电器 买了: " + something);

     }

   }

}

4.哈根达斯

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 哈根达斯

   /// </summary>

   public class haagendaz

   {

     public void serivce( string something)

     {

       console.writeline( "在哈根达斯 买了: " + something);

     }

   }

}

5.真功夫

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 真功夫

   /// </summary>

   public class kungfu

   {

     public void serivce( string something)

     {

       console.writeline( "在真功夫 吃了: " + something);

     }

   }

}

6.六福珠宝

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 六福珠宝

   /// </summary>

   public class lukfook

   {

     public void serivce( string something)

     {

       console.writeline( "在六福珠宝 买了: " + something);

     }

   }

}

7.耐克

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 耐克

   /// </summary>

   public class nike

   {

     public void serivce( string something)

     {

       console.writeline( "在耐克店 买了: " + something);

     }

   }

}

8.only

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// only时装

   /// </summary>

   public class only

   {

     public void serivce( string something)

     {

       console.writeline( "在only时装 买了: " + something);

     }

   }

}

9.苏宁电器

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 苏宁电器

   /// </summary>

   public class suning

   {

     public void serivce( string something)

     {

       console.writeline( "在苏宁电器 买了: " + something);

     }

   }

}

10.veromoda国际时装品牌

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// veromoda国际时装品牌

   /// </summary>

   public class veromoda

   {

     public void serivce( string something)

     {

       console.writeline(something);

     }

   }

}

11.消费者

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

namespace com.design.gof.facade

{

   /// <summary>

   /// 消费店子

   /// </summary>

   public enum shopoption

   {

     adidas = 1, dkny = 2, gome = 3,

     nike = 4, suning = 5, veromoda = 6,

     feiyangmovie = 7, haagendaz = 8, lukfook = 9, kungfu = 10

   }

   /// <summary>

   /// 消费单

   /// </summary>

   public class bill {

     /// <summary>

     /// 要去的消费店

     /// </summary>

     public shopoption item { get ; set ; }

     /// <summary>

     /// 去这个店要买啥

     /// </summary>

     public string something { get ; set ; }

   }

   public class consumer

   {

     /// <summary>

     /// 消费单

     /// </summary>

     public ilist<bill> items { get ; set ; }

     /// <summary>

     /// 姓名

     /// </summary>

     public string name { get ; set ; }

   }

}

12.天河城---一站式服务

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.reflection;

namespace com.design.gof.facade

{

   /// <summary>

   /// 天河城

   /// </summary>

   public class teemall

   {

     private static readonly assembly assembly = assembly.loadfile(appdomain.currentdomain.basedirectory + @"\com.design.gof.dll" );

     /// <summary>

     /// 一站式服务

     /// </summary>

     /// <param name="consumer"></param>

     public void offerservice(consumer consumer) {

       console.writeline( "我是: " + consumer.name+ ",不差钱,今天来天河城玩: " );

       console.writeline( "----------------------------------------------" );

       foreach (bill item in consumer.items)

       {

         object obj= assembly.createinstance( "com.design.gof.facade." + item.item);

         methodinfo info = obj.gettype().getmethod( "serivce" );

         info.invoke(obj, new object [] { item.something });

       }

       console.writeline();

     }

   }

}

13.主函数调用

?

using system;

using system.collections.generic;

using system.linq;

using system.text;

using com.design.gof.facade;

namespace com.design.gof.test

{

   class program

   {

     static void main( string [] args)

     {

       //天河城购物中心

       teemall teemall = new teemall();

       //消费者 1

       consumer consumer = new consumer

       {

         name= "聂小倩" ,

         //消费单

         items = new list<bill> {

          new bill{ item=shopoption.adidas, something= "运动服" },

          new bill{ item=shopoption.gome, something= "苹果iphone智能手机" },

          new bill{ item=shopoption.feiyangmovie, something= "<冰河世纪 4>" },

          new bill{ item=shopoption.kungfu, something= "香菇炖鸡" },

           new bill{ item=shopoption.lukfook, something= "金项链" },

         }

       };

       teemall.offerservice(consumer);

       //消费者 2

       consumer = new consumer

       {

         name = "宁采臣" ,

         //消费单

         items = new list<bill> {

          new bill{ item=shopoption.feiyangmovie, something= "《太空一号》" },

          new bill{ item=shopoption.veromoda, something= "然后去了veromoda时装,买了一套服装" },

          new bill{ item=shopoption.haagendaz, something= "买了一雪糕" },

          new bill{ item=shopoption.suning, something= "在苏宁看买平板电脑" },

         }

       };

       teemall.offerservice(consumer);

       console.readkey();

     }

   }

}

14.运行结果

15.总结

天河城 teemall 理论上应该包括 所有 商场的引用,

这里用反射 避免了这一动作。

附: 完整实例代码点击此处 本站下载 。

希望本文所述对大家c#程序设计有所帮助。

原文链接:http://HdhCmsTestcnblogs测试数据/HCCZX/archive/2012/08/13/2636459.html

dy("nrwz");

查看更多关于C#设计模式之Facade外观模式解决天河城购物问题示例的详细内容...

  阅读:49次