装饰模式(Decorator)
- 概念
动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。 - UML类图:
- 代码
给人打扮
public class Person {
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public void show() {
System.out.println("装扮的" + name);
}
}
public class Finery extends Person{
protected Person component;
public void Decorate(Person component){
this.component = component;
}
@Override
public void show(){
if (component != null){
component.show();
}
}
}
class LeatherShoes extends Finery {
@Override
public void show() {
System.out.print("皮鞋");
super.show();
}
}
class TShirts extends Finery{
@Override
public void show() {
System.out.print("T恤");
super.show();
}
}
class Suit extends Finery{
@Override
public void show() {
System.out.print("西装");
super.show();
}
}
class Demo{
public static void main(String[] args) {
Person person = new Person("查尔");
LeatherShoes ls = new LeatherShoes();
Suit suit = new Suit();
TShirts ts = new TShirts();
ls.Decorate(person);
suit.Decorate(ls);
ts.Decorate(suit);
ts.show();
}
- 总结:
- 对原始功能的扩展(增强)。
- 每个具体装饰类只关心自己的功能,不关心如何被添加到对象链当中。让户用在客户端根据需要有选择、按顺序地使用,实现不同效果。
- 把类中的装饰功能从类中去除,简化原有的类,让其符合单一职责。
- 有效地把类的核心职责和装饰功能分开,还可去除相关类中重复的装饰逻辑
职责链模式
- 概念
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。 - UML类图:
- 代码
public class Request {
private String requestType;
private String requestContent;
private int number;
public String getRequestType() {
return requestType;
}
public void setRequestType(String requestType) {
this.requestType = requestType;
}
public String getRequestContent() {
return requestContent;
}
public void setRequestContent(String requestContent) {
this.requestContent = requestContent;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
abstract class Manager {
protected String name;
protected Manager superior;
public Manager(String name){
this.name = name;
}
public void setSuperior(Manager superior){
this.superior=superior;
}
abstract public void RequestApplications(Request request);
}
class CommonManager extends Manager {
public CommonManager(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if ("请假".equals(request.getRequestType()) && request.getNumber() <= 2) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "被比准");
} else if (superior != null) {
superior.RequestApplications(request);
}
}
}
class Majordomo extends Manager {
public Majordomo(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if ( "请假".equals(request.getRequestType()) && request.getNumber() <= 5) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "被比准");
} else if (superior != null) {
superior.RequestApplications(request);
}
}
}
class GeneralManger extends Manager {
public GeneralManger(String name) {
super(name);
}
@Override
public void RequestApplications(Request request) {
if ( "请假".equals(request.getRequestType())) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "被比准");
} else if ( "加薪".equals(request.getRequestType()) && request.getNumber() == 500) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "被比准");
} else if ("加薪".equals(request.getRequestType()) && request.getNumber() > 500) {
System.out.println(name + ":" + request.getRequestContent() + "数量" + request.getNumber() + "再说吧");
}
}
}
public class Demo {
public static void main(String[] args) {
CommonManager difan = new CommonManager("笛梵");
Majordomo huashuo = new Majordomo("华硕");
GeneralManger jinhuan = new GeneralManger("金换");
difan.setSuperior(huashuo);
huashuo.setSuperior(jinhuan);
Request request = new Request();
request.setRequestType("请假");
request.setRequestContent("小白请假");
request.setNumber(3);
difan.RequestApplications(request);
Request request1 = new Request();
request1.setRequestType("加薪");
request1.setRequestContent("小白请求加薪");
request1.setNumber(500);
difan.RequestApplications(request1);
}
}
- 总结:
- 降低接受者与发送者耦合度; 一个对象只需保持一个指向其后继者的引用,不需保持其他所有处理者的引用,客户端只需要发起一次调用即可。
- 核心思想就是分而治之。每个类只需要处理自己该处理的工作,不能处理的传递给下一个对象完成,明确各类的责任范围,把它们组织起来,形成一个链式的结构。
- 增强给对象指派职责的灵活性。通过改变链内的成员或者调动它们的次序,允许动态地新增或者删除责任。
- 使用职责链要保证每个请求一定被处理。如果一个请求一直传到链的末端都得不到处理,这就槽糕了。