设计模式/行为型设计模式
简述
为了避免请求的发送者和接收者之间的耦合关系,使多个接收对象都有机会处理请求。
将这些对象练成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止
UML图
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public interface Drawable { void draw(); }
@Slf4j public class DrawHead implements Drawable { private Drawable drawable;
public DrawHead(Drawable drawable) { this.drawable = drawable; }
@Override public void draw() { log.debug(“DrawHead 画头部--"); drawable.draw(); } }
@Slf4j public class DrawBody implements Drawable { private Drawable drawable;
public DrawBody(Drawable drawable) { this.drawable = drawable; }
@Override public void draw() { log.debug(“DrawBody 画身体--"); drawable.draw(); } }
@Slf4j public class DrawFoot implements Drawable { @Override public void draw() { log.debug(“DrawFoot 画脚 — “); } }
public class Client { public static void main(String[] args) { Drawable drawFoot = new DrawFoot(); Drawable drawBody = new DrawBody(drawFoot); Drawable drawHead = new DrawHead(drawBody); drawHead.draw(); } }
|