/** Decorator Mode * Decorator mode is the use of an instance of a subclass of a decorated class, where the client gives an instance of the subclass to the adornment class.
is an integrated alternative * advantage: Using the adornment mode, you can provide more flexible extension objects than inheritance, it can dynamically add the function of the object *, and can be arbitrarily combined with these features.
* Disadvantage: Because can be arbitrarily combined, so there may be some unreasonable logic *///First create mobile phone class interface package july.star.decorate; /** * Phone * Create mobile interface * @author Moxingjian * @email 939697374@qq.com * @date December 11, 2016 PM 8:59:36 * @version 1.0 *
/public interface Phone {//Each phone has a function of calling public void Call ();}
Implement its interface package july.star.decorate; /** * IPhone * @author Moxingjian * @email 939697374@qq.com * @date December 11, 2016 PM 9:01:17 * @version 1.0 * * PU Blic class IPhone implements phone{@Override public void Call () {System.out.println ("phone can be called.
");
}}//create abstract Phone abstract class package july.star.decorate; /** * phonedecotate * Create abstract phone decoration class * @author Moxingjian * @email 939697374@qq.com * @date December 11, 2016 afternoon 9:03:29 * @ver
Sion 1.0 */Public abstract class Phonedecorate implements phone{private Phone p; Public phonedecorate (Phone p) {thiS.P = p;
} @Override public void Call () {P.call ();
}//To the phone to decorate the ringtone function package july.star.decorate;
/** * Ringphonedecorate * to the phone to decorate the ringtone function * @author Moxingjian * @email 939697374@qq.com * @date December 11, 2016 9:26:02
* @version 1.0 */public class Ringphonedecorate extends phonedecorate{public ringphonedecorate (Phone p) {
Super (P);
} @Override public void Call () {System.out.println ("phone can listen to ringtones");
Super.call ();
}//Add play video function to phone package july.star.decorate; /** * musicphonedecorate * Add playback video function to mobile phone * @author Moxingjian * @email 939697374@qq.com * @date December 11, 2016 9:29
: * @version 1.0 */public class Playphonedecorate extends phonedecorate{public playphonedecorate (Phone p) {
Super (P);
} @Override public void Call () {Super.call ();
System.out.println ("Mobile phone can play video");
}}//test package july.star.decorate; Import jaVa.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStreamWriter;
Import Java.util.Scanner; /** * Phonedemo * Decorator mode * Decorator mode is the use of an instance of a subclass of a decorated class, where the client gives an instance of the subclass to the adornment class.
is an integrated alternative * advantage: Using the adornment mode, you can provide more flexible extension objects than inheritance, it can dynamically add the function of the object *, and can be arbitrarily combined with these features. * Disadvantage: Because it can be arbitrarily combined, so there may be some unreasonable logic * @author Moxingjian * @email 939697374@qq.com * @date December 11, 2016 PM 9:06:30 * @versi
On 1.0 */public class Phonedemo {public static void main (string[] args) {Phone p = new IPhone ();
P.call ();
System.out.println ("-----------------------");
Listen to the ring before the phone//To add the ringtone function ringphonedecorate RPD = new Ringphonedecorate (p);
Rpd.call ();
System.out.println ("-----------------------");
Watch the video after the call//Add playback video to the phone playphonedecorate PD = new Playphonedecorate (p);
Pd.call (); System.out.println ("-----------------------");
Listen to the ringtone before you call, play the video after the call ringphonedecorate R = new Ringphonedecorate (new Playphonedecorate (p));
R.call ();
System.out.println ("-----------------------"); Similar IO flow is also the decorator mode design inputstream is = system.in; BYTE stream//wrapper InputStreamReader ISR = new InputStreamReader (IS);
Character stream, single read//re-packaged BufferedReader bis = new BufferedReader (ISR);
Can be represented by a line//BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System.out));
or the console input//scanner sc = new Scanner (system.in);
}
}