//人类(ConcreteComponent)
public class Person {
private String name;
public Person() {
}
public Person(String name) {
this.name = name;
}
public void show() {
System.out.println("装扮的" + name);
}
}
//服饰类(Decorator)
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();
}
}
}
//具体服装类(concreteDecorator)
class LeatherShoes extends Finery {
@Override
public void show() {
System.out.print("皮鞋");
super.show();
}
}
//具体服装类(concreteDecorator)
class TShirts extends Finery{
@Override
public void show() {
System.out.print("T恤");
super.show();
}
}
//具体服装类(concreteDecorator)
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();
}
//请求
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;
}
}
//Handler类
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);
}
//ConcreteHandler类,经理
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);
}
}
}
/**
* ConcreteHandler类,总监
*/
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);
}
}
}
/**
* ConcreteHandler类,总经理
*/
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);
}
}