抽象类
/**
* @Author: chuan.bai
* @Description 制作面条抽象类
* @Date: Created on 16:55 2021/4/1
* @Modified By:
*/
public abstract class NoodlesAbstract {
/**
* 制作面条的方法
*/
abstract void process();
}
基础类
/**
* @Author: chuan.bai
* @Description 制作普通面条
* @Date: Created on 16:58 2021/4/1
* @Modified By:
*/
public class NormalNoodles extends NoodlesAbstract{
@Override
void process() {
System.out.println("制作普通面条");
}
}
装饰器
/**
* @Author: chuan.bai
* @Description 面条装饰器
* @Date: Created on 17:01 2021/4/1
* @Modified By:
*/
public abstract class Decorator extends NoodlesAbstract{
private NoodlesAbstract noodlesAbstract;
public Decorator(NoodlesAbstract noodlesAbstract) {
this.noodlesAbstract = noodlesAbstract;
}
@Override
void process() {
noodlesAbstract.process();
}
}
/**
* @Author: chuan.bai
* @Description 加西红柿的面
* @Date: Created on 17:05 2021/4/1
* @Modified By:
*/
public class EggNoodles extends Decorator {
public EggNoodles(NoodlesAbstract noodlesAbstract) {
super(noodlesAbstract);
}
@Override
public void process() {
super.process();
System.out.println("加鸡蛋");
}
}
/**
* @Author: chuan.bai
* @Description 加西红柿
* @Date: Created on 17:05 2021/4/1
* @Modified By:
*/
public class TomatoNoodles extends Decorator {
public TomatoNoodles(NoodlesAbstract noodlesAbstract) {
super(noodlesAbstract);
}
@Override
public void process() {
super.process();
System.out.println("加西红柿");
}
}
客户端
/**
* @Author: chuan.bai
* @Description
* @Date: Created on 17:06 2021/4/1
* @Modified By:
*/
public class Client {
public static void main(String[] args) {
NormalNoodles normalNoodles = new NormalNoodles();
TomatoNoodles tomatoNoodles = new TomatoNoodles(normalNoodles);
tomatoNoodles.process();
EggNoodles eggNoodles = new EggNoodles(tomatoNoodles);
eggNoodles.process();
}
}
输出结果
制作普通面条
加西红柿
制作普通面条
加西红柿
加鸡蛋
Process finished with exit code 0