a- package decorate.demo;
- public class RingPhoneDecorate extends PhoneDecorate {
- public RingPhoneDecorate(Phone p) {
- super(p);
- }
- @Override
- public void phone() {
- System.out.println("手机有彩铃啦~~");
- super.phone();
- }
-
- }
- package decorate.demo;
- public abstract class PhoneDecorate implements Phone {
- private Phone p;
- public PhoneDecorate(Phone p){
- this.p = p;
- }
- @Override
- public void phone() {
- this.p.phone();
- }
- }
- package decorate.demo;
- public class MusicPhoneDecorate extends PhoneDecorate {
- public MusicPhoneDecorate(Phone p) {
- super(p);
- }
- @Override
- public void phone() {
- System.out.println("手机可以听音乐啦~~");
- super.phone();
- }
- }
- package decorate.demo;
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- Phone p = new PhoneImpl();
- p.phone();
- System.out.println("------------------------------");
- new MusicPhoneDecorate(p).phone();
- System.out.println("------------------------------");
- new MusicPhoneDecorate(new RingPhoneDecorate(p)).phone();
- }
- }
复制代码
|
|