It feels a bit political. Of course, we don't discuss politics. It's just a measure that must be done in many cases.
In fact, when you do not want to establish independent operations for each subsystem, you may want to have a single
The operation scheme is available for you to choose from. Otherwise, you must seek the unique operation scheme for each subsystem to change the operation to a single. facde.
This can be done.
Assume that you have three broadcasting devices, which enable the CD drive and MP3 devices respectively. They have their own independent operation cases, which makes you a headache.
Because you have to master three similar but relatively independent solutions, we will consider how to solve them.
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 ");
}
}
// The following figure shows a unified interface.
Class facade {
Private mp3play MP3;
Private recordplay RP;
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 ();
Case "MP3 ":
MP3 = new mp3play (songname );
Mp3.playmp3 ();
Case "record ":
RD = new recordplay (songname );
Rd. playrecord ();
}
}
Public void stop (){
Case "cd ":
Cd = new cdplay (songname );
CD. stopcd ();
Case "MP3 ":
MP3 = new mp3play (songname );
Mp3.stopmp3 ();
Case "record ":
RD = new recordplay (songname );
Rd. stoprecord ();
}
}
// Call
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;
}
}
In this way, we only need a unified interface without knowing how each of its subsystems is customized.