双维度扩展
代码结构
源码
package com.myspringboot.shejimoshi.qiaojie;
public class Book extends GiftImpl {
public void out(){
System.out.println("Book!");
}
}
package com.myspringboot.shejimoshi.qiaojie;
public class Flower extends GiftImpl {
public void out(){
System.out.println("Flower!");
}
}
package com.myspringboot.shejimoshi.qiaojie;
public abstract class Gift {
GiftImpl impl;
void out(){
impl.out();
};
}
package com.myspringboot.shejimoshi.qiaojie;
public class GiftImpl {
public void out(){
}
}
package com.myspringboot.shejimoshi.qiaojie;
public class Main {
public static void main(String[] args) {
new WarmGift(new Flower()).out();
new WildGift(new Book()).out();
}
}
package com.myspringboot.shejimoshi.qiaojie;
public class WarmGift extends Gift {
public WarmGift(GiftImpl impl) {
this.impl = impl;
}
@Override
void out() {
System.out.print("WarmGift ");
super.out();
}
}
package com.myspringboot.shejimoshi.qiaojie;
public class WildGift extends Gift {
public WildGift(GiftImpl impl) {
this.impl = impl;
}
@Override
void out() {
System.out.print("WildGift ");
super.out();
}
}