Unified Source (FACADE)

zhaozj2021-02-16  50

I feel a bit political taste. Of course. We don't discuss politics. Just illustrate a measures that must be carried out in many cases

In fact. When you don't want to establish an independent operation for each subsystem. You may have a single one.

The operational scheme is optional. It is necessary to seek its unique operational solution for each subsystem. Make the action change. FACDE

You can do this.

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

Class CDPlay {Private String Songname; CDPLAY (STRING SN) {SongName = Sn;}

Public void playcd () {Console.WriteLine ("Play CD" Songname);} public void stopcd () {console.writeLine ("STOP CD");}

}

Class Recordplay {Private String Songname; RecordPlay (String Sn) {SongName = Sn;

Public void playrecord () {Console.writeline ("Play Record" Songname; PUBLIC VOID StopRecordd () {Console.WriteLine ("Stop Record");}}

Class Mp3play {Private String Songname; CDPLAY (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

Class facade {private mp3play mp3; private recordplay rp; private string playname; private string songname

Public string playname {get {return playname;}; set {playname = value;};};}; set {songname = value;

Public facade (string pn, string sn) {playname = pn; songname = sn;} public void play () {copy () {case "cd": cd = new cdplay (songname); cd.playcd (); case "MP3": MP3 = New MP3PLAY (SongName); mp3.playmp3 (); Case "Record": rd = new recordplay (songname); rd.playRecord ();}} public void stop () {CASE "CD": CD = New CDPLAY (); cd.stopcd (); case "mp3": mp3 = new mp3play (SongName); mp3.stopmp3 (); case "record": rd = new recordplay; rd.stopRecord ); } }//transfer

Public Class Client {public static int main (string [] args) {facade facade = new facade ("cd"); facade.play (); facade.stop ();

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

Facade.PlayName = "Record"; Facade.Play (); facade.stop (); return 0;}}

This only requires a unified interface. Don't know how each subsystem is particularly customized.

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

New Post(0)