Unified Storage (facade)

Source: Internet
Author: User

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.