Design Pattern 16-Facade

xiaoxiao2021-03-06  57

Using system;

Namespace pattern {///

/// summary description for class1. /// / *

Suppose you have three broadcasting equipment. Make CD tape drives, MP3, which have their own independent operation. This makes you a headache.

Because you have to master the three sets of relatively independent programs. So we consider how to solve * /

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

// We have established a unified interface

Public class facade {private mp3play mp3; private recordplay rd; private string playname; private string songname

Public string playname {get {returnome = value;}} public string songname {get {return songname;}}}

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; casse Record ": rd = new recordplay (SongName); rd.stopRecord (); break;}}}} // facade pattern facade facade = new facade (" CD "," AAAA "); Facade.Play (); Facade. STOP (); facade.playname = "mp3"; facade.play (); facade.stop ();

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

转载请注明原文地址:https://www.9cbs.com/read-111806.html

New Post(0)