現在還處於實習生招生季,簡歷是一切的基石,畢竟簡歷不過,哪怕你是大牛,連面試的機會都沒有,豈不是開心的不行,往往我們的簡歷是有針對性的,對於不同的公司和不同的崗位,應聘騰訊後台,鼓吹阿裡和Java就是你的不對了……可是給每個公司都寫一份簡歷,那就不開心了,所以我們可以采用裝飾者模式
基本角色的實現
abstract class Component {
public abstract void print();
}
Decorator:
abstract class Decorator extends Component{
private Component component;
Decorator( Component component ){
this.component = component;
}
@Override
public void print(){
component.print();
}
}
Concrete Component:
class Resume extends Component{
@Override
public void print() {
System.out.println("My Name: llin\n"
+"My Major: Software Engineering"
+"My University: Tianjin University"
);
}
}
Concrete Decorator:
class AliDecrator extends Decorator{
AliDecrator(Component component) {
super(component);
}
@Override
public void print() {
super.print();
System.out.println("I want a position at Ali major in Java");
}
}
class TencentDecorator extends Decorator{
TencentDecorator(Component component) {
super(component);
}
@Override
public void print(){
super.print();
System.out.println("I want a position at QQ major in C++");
}
}
測試用類:
public class decoratorEg {
public static void main ( String [] args ){
Component resume = new Resume();
Component aliResume = new AliDecrator( resume );
Component QQResume = new TencentDecorator( resume );
aliResume.print();
System.out.println("");
QQResume.print();
}
}
測試結果如下:(其實在大量拓展,大量排列組合時才能更好地凸顯出Decorator模式的優勢)
在InputStream和OutputStream上添加各種功能,類似緩存、文件讀入等等一系列功能拓充都是Decorator模式下的產物