CSDN博客

img 2002pine

Design Pattern 16-Facade

发表于2004/9/29 15:15:00  705人阅读

using System;

namespace Pattern
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 /*

 假设你有三个播音设备.分别使CD 磁带机,Mp3 它们分别有自己独立的操作放案.这让你很头痛

 因为你必须掌握三套类似却有相对独立的方案.那么我们考虑如何解决*/

 public class CDPlay
 {
  private  string SongName;
  public CDPlay(string sn)
  {
   SongName=sn;
  }

  public void PlayCd()
  {
   Console.WriteLine("Play Cd" + SongName);
  }
 
  public void StopCd()
  {
   Console.WriteLine("Stop Cd");
  }

 }

 public class RecordPlay
 {
  private  string SongName;
  public RecordPlay(string sn)
  {
   SongName=sn;
  }

  public void PlayRecord()
  {
   Console.WriteLine("Play Record" + SongName);
  }
 
  public void StopRecord()
  {
   Console.WriteLine("Stop Record");
  }
 }

 public class Mp3Play
 {
  private  string SongName;
  public Mp3Play(string sn)
  {
   SongName=sn;
  }

  public void PlayMp3()
  {
   Console.WriteLine("Play Mp3" + SongName);
  }
 
  public void StopMp3()
  {
   Console.WriteLine("Stop Mp3");
  }
 }

 

 //下面我们建立统一的界面

 public class Facade
 {
  private Mp3Play mp3;
  private RecordPlay rd;
  private CDPlay cd;
 
  private string playName;
  private  string songName;

  public string PlayName
  {
   get{return playName;}
   set{playName=value;}
  }
 
  public string SongName
  {
   get{return SongName;}
   set{SongName=value;}
  }

  public Facade(string pn,string sn)
  {
   playName=pn;
   songName=sn;
  }
 
  public void play()
  {
   switch(playName)
   {
    case "CD":
     cd=new CDPlay(songName);
     cd.PlayCd();
     break;
    case "mp3":
     mp3=new Mp3Play(songName);
     mp3.PlayMp3();
     break;
    case "Record":
     rd=new RecordPlay(songName);
     rd.PlayRecord();
     break;
   }
  }
 
  public void stop()
  {
   switch(playName)
   {
    case "CD":
     cd=new CDPlay(songName);
     cd.StopCd();
     break;
    case "mp3":
     mp3=new Mp3Play(songName);
     mp3.StopMp3();
     break;
    case "Record":
     rd=new RecordPlay(songName);
     rd.StopRecord();
     break;
   }
  
  }
 }

}
   //门面模式
   Facade facade = new Facade("CD","aaaa");
   facade.play();
   facade.stop ();

   facade.PlayName="mp3";
   facade.play();
   facade.stop();

   facade.PlayName="Record";
   facade.play();
   facade.stop();

阅读全文
0 0

相关文章推荐

img
取 消
img