First of all assume a scene, such as we are selling mobile phones, if customers come to pick a mobile phone, need to take a different type of mobile phone to demonstrate the function
For simplicity, we can make an interface that connects to the phone at one end and controls the various functions of the demo phone on the other end.
Mobile phone has text messaging, phone two basic functions, we can set the interface to these two features
Public interface Cellphone
{public
void phone (long num);
public void SMS (long num, String content);
The following general mobile phone can use this interface
public class Nokia implements Cellphone
{
@Override public
void Phone (long number)
{
Util.print (" Nokia phone ");
}
@Override public
void SMS (long, String content)
{
util.print ("Nokia SMS");
}
public class BlackBerry implements Cellphone
{
@Override public
void Phone (long number)
{
Util.print ("BlackBerry phone");
@Override public
void SMS (long, String content)
{
util.print ("BlackBerry sms");
}
You can also play games on the iphone.
Public class The Iphone implements Cellphone
{
@Override public
void phone (long num)
{
Util.print (" Iphone phone ");
}
@Override public
void SMS (long num, String content)
{
util.print ("Iphone sms");
}
public void Play ()
{
util.print (' Iphone play ');
}
So, we can use the interface to demonstrate the function of the phone when we operate.
Cellphone cell = new Nokia ();
Cell.phone (250);
At this time, customers also want us to demonstrate the cottage machine, but the interface does not match, we need to make an adapter to convert
public class Shanzhaiji
{public
void Dadianhua ()
{
util.print ("Shanzhaiji Dadianhua Jiushiniu") ;
}
public void Faduanxin ()
{
util.print ("Shanzhaiji faduanxin jiushiniu");
}
public class Shanzhaiadapter implements Cellphone
{
private Shanzhaiji Shanzhai;
Public Shanzhaiadapter (Shanzhaiji Shanzhai)
{
This.shanzhai = Shanzhai;
}
@Override public
void phone (long num)
{
Shanzhai.dadianhua ();
}
@Override public
void SMS (long num, String content)
{
shanzhai.faduanxin ();
}
}
Through the interface conversion, we can also demonstrate the cottage machine
Shanzhaiadapter adapter = new Shanzhaiadapter (new Shanzhaiji ());
Adapter.phone (0);