设计模式实战:广告管理系统的设计与实现
// 广告接口
interface AdContent {
void display();
}
// 基础广告实现类
class BasicAd implements AdContent {
@Override
public void display() {
System.out.println("Displaying basic ad.");
}
}
// 广告装饰者基类
abstract class AdDecorator implements AdContent {
protected AdContent decoratedAd;
public AdDecorator(AdContent decoratedAd) {
this.decoratedAd = decoratedAd;
}
@Override
public void display() {
decoratedAd.display();
}
}
// 视频广告装饰者
class VideoAdDecorator extends AdDecorator {
public VideoAdDecorator(AdContent decoratedAd) {
super(decoratedAd);
}
@Override
public void display() {
super.display();
System.out.println("Displaying video ad.");
}
}
// 图片广告装饰者
class ImageAdDecorator extends AdDecorator {
public ImageAdDecorator(AdContent decoratedAd) {
super(decoratedAd);
}
@Override
public void display() {
super.display();
System.out.println("Displaying image ad.");
}
}