装饰模式:我们可以通过装饰模式进行功能的扩展,需要什么功能只需要具体继承我们的装饰组建,就可以,如下图结构





/**
*
*/
package com.cai.decorator;
/**
*@title:
*@author:allencai
*@date:2018-4-27下午9:25:59
*/
public interface Car {
//每辆车都具有跑的功能
public void run();
public void show();
}
*@title:
*@author:allencai
*@date:2018-4-27下午9:48:10
*/
public class Runcar implements Car{
/* (non-Javadoc)
* @see com.cai.decorator.Car#run()
*/
@Override
public void run() {
System.err.println("可以跑");
}
/* (non-Javadoc)
* @see com.cai.decorator.Car#show()
*/
@Override
public void show() {
this.run();
}
}
/**
*
*/
package com.cai.decorator;
/**
*@title:
*@author:allencai
*@date:2018-4-27下午9:27:05
*/
public abstract class DecoratorCar implements Car{
private Car car;
public DecoratorCar(Car car) {
this.car = car;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public abstract void show();
}
/**
*
*/
package com.cai.decorator;
/**
*@title:
*@author:allencai
*@date:2018-4-27下午9:30:18
*/
public class FlyDecorator extends DecoratorCar{
/**
* @param car
*/
public FlyDecorator(Car car) {
super(car);
}
public void show(){
this.getCar().show();
this.flyRun();
}
//添加飞车
public void flyRun(){
System.err.println("我会飞");
}
public void run(){
}
}