一、链设计模式设计理念:
使用java面向对象的三大特征(继承、多态、封装)对原有对象实现功能扩展,通过处理机制对于不同层级之间实现继承关系生成多个不同子类继承共同的“类型”,通过对每个子类传入父类,重写并调用父类方法以实现对原有的代码不修改增加并实现新的功能模块。简单通俗,装饰模式就是动态的对一个原有对象添加一些额外的职责;职责链模式就是对一个事务对象根据链设计关系对该事务的动态处理。
二、装饰模式:
1.作用:动态的给一个对象添加新的职责。
2.实现方式:将一个类的对象嵌入到另外一个新的对象之中,由另外一个对象(原有对象)来决定是否调用嵌入对象的行为对自己的扩展功能,这个新的对象为装饰器(装饰机制)。为了使得装饰器与它所装饰的对象客户端来说是透明的,装饰器类和被装饰必须实现相同的接口,客户使用时不需要知道一个类的对象是否被装饰过,可以一致性地使用装饰的对象以及被装饰的对象
3.小菜穿衣代码实践:
(1)首先需要创建一个角色展示的接口 Component;在接口中有一个展示的方法(行为)
- package com.example;
-
- /**
- * @BelongsProject: 装饰模式
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 19:22
- */
- public interface Component {
- /**
- * @description: 角色展示
- * @author: mr.zhang
- * @date: 2022/11/10 19:26
- * @param: []
- * @return: void
- **/
- abstract void show();
- }
(2)具体被装扮者人类
- package com.example;
-
- /**
- * @BelongsProject: 具体的被装扮者类
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 19:30
- */
- public class Person implements Component{
-
- private String name;//被装扮的人
-
- public Person(String name) {
- this.name = name;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- @Override
- public void show() {
- System.out.println("装扮的"+name);
- }
- }
(3)构建一个装饰器(服饰类)实现角色接口;
- package com.example;
-
- /**
- * @BelongsProject: 服饰类 装饰器
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 19:36
- */
- public class Finery implements Component {
-
- protected Component component;//引入被装饰者
-
- public void decorate(Component component){
- this.component=component;
- }
-
- /**
- * @description: 当存在一个被装饰者时就调用他的展示方法
- * @author: mr.zhang
- * @date: 2022/11/10 19:39
- * @param: []
- * @return: void
- **/
- @Override
- public void show() {
- if (component!=null)
- component.show();
- }
- }
-
(4)具体需要的服饰(装饰配件)
- package com.example;
-
- /**
- * @BelongsProject: 具体服饰类 跨库
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 19:42
- */
- public class Kuaku extends Finery{
-
- public void show(){
- System.out.println("垮裤");
- super.show();
- }
-
- }
- package com.example;
-
- /**
- * @BelongsProject: 破球鞋
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 19:53
- */
- public class Pqx extends Finery{
- public void show(){
- System.out.println("破球鞋");
- super.show();
- }
- }
- package com.example;
-
- /**
- * @BelongsProject: T恤
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 19:56
- */
- public class T_shirt extends Finery{
- public void show(){
- System.out.println("T恤");
- super.show();
- }
- }
- package com.example;
-
- /**
- * @BelongsProject: 西装
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 19:58
- */
- public class Suit extends Finery{
- public void show(){
- System.out.println("西装");
- super.show();
- }
- }
- package com.example;
-
- /**
- * @BelongsProject: 皮鞋
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 19:59
- */
- public class PX extends Finery{
- public void show(){
- System.out.println("皮鞋");
- super.show();
- }
- }
(5)客户端测试代码 创建被装饰者对象(小菜),选择装饰服饰,确定装饰关系
- package com.example;
-
- import org.junit.jupiter.api.Test;
- import org.springframework.boot.test.context.SpringBootTest;
-
- @SpringBootTest
- class Tests {
-
- @Test
- void contextLoads() {
- Person person = new Person("小菜");//被装饰者小菜
- System.out.println("第一次被装扮:");
- Pqx pqx = new Pqx();
- Kuaku kuaku = new Kuaku();
- T_shirt shirt = new T_shirt();
-
- //装饰过程
- pqx.decorate(person);//选择破球鞋
- kuaku.decorate(pqx);//继续选择了垮裤
- shirt.decorate(kuaku);//最后选择了T恤
- shirt.show();/* 第一次被装扮:
- T恤
- 垮裤
- 破球鞋
- 装扮的小菜 逆序输出是因为super在打印语句之后
- **/
-
- System.out.println("第二次被装扮:");
- PX px = new PX();
- Suit suit = new Suit();
- px.decorate(person);
- suit.decorate(px);
- suit.show();/*
- 第二次被装扮:
- 西装
- 皮鞋
- 装扮的小菜
- **/
- }
-
- }

三、职责链模式:
1.作用:主要构造事件层级处理机制、客户请求者,客户发出请求通过在职责链发送至处理层,请求在整个链路传递过程中都有可能被该层级处理对象处理,让事件处理者与发送者实现解耦关系。
2.职责链模式与装饰模式:满足了设计模式的开闭原则,以及单一原则;在任意位置可以增添新事件和新对象,提高了代码的维护性和可拓展性;责任链简化了对象之间的连接。每个对象只需保持一个指向其后继者的引用,不需保持其他所有处理者的引用,这避免了使用众多的 if 或者 if···else 语句;责任分担。每个类只需要处理自己该处理的工作,不该处理的传递给下一个对象完成,明确各类的责任范围,符合类的单一职责原则。
3.小菜涨薪代码实践
(1)创建一个代办事件实体bean封装客户请求
- package com.example;
-
- /**
- * @BelongsProject: 代办事件请求实体bean
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 20:55
- */
- public class Request {
- private String requestType;//类别
- private String requestContent;//内容
- private int number;//数量
-
- public Request() {
- }
-
- 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;
- }
- }
(2)请求者封装信息类
- package com.example;
-
- /**
- * @BelongsProject: 发出请求者封装bean
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 20:53
- */
- public abstract class Solicitant {
-
- protected String name;
- protected Solicitant solicitant;
-
- public Solicitant(String name) {
- this.name = name;
- }
-
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Solicitant getSolicitant() {
- return solicitant;
- }
-
- public void setSolicitant(Solicitant solicitant) {
- this.solicitant = solicitant;
- }
- public abstract void processRequest(Request request);
-
-
- }
(3)处理事务者一部门总监,并对其权限设置,如果不能处理上交到上级领导,如果能处理则处理不上报
- package com.example;
-
- /**
- * @BelongsProject: 总监
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 21:07
- */
- public class Majordomo extends Solicitant{
- public Majordomo(String name) {
- super(name);
- }
-
- @Override
- public void processRequest(Request request) {
- if (request.getRequestType().equals("请假") && request.getNumber() <= 5){
- System.out.println(name +":"+ request.getRequestContent()+"数量"+request.getNumber()+",被批准");
- }else {
- if (request != null){
- solicitant.processRequest(request);
- }
- }
- }
- }
(4)部门经理,权限大于部门总监实现功能一致
- package com.example;
-
- /**
- * @BelongsProject: 部门经理
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 20:56
- */
- public class TechnicalManager extends Solicitant{
- public TechnicalManager(String name) {
- super(name);
- }
-
- @Override
- public void processRequest(Request request) {
- if (request.getRequestType().equals("请假") && request.getNumber() <= 2) {
- System.out.println(name +":"+ request.getRequestContent()+"数量"+request.getNumber()+"被批准");
- }else {
- if (request != null){
- solicitant.processRequest(request);
- }
- }
- }
- }
(5)最大权限者(最高职位者)总经理无论事件大小下级管理者无法处理的事件都有该管理者处理
- package com.example;
-
- /**
- * @BelongsProject: 总经理
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 21:06
- */
- public class GeneralManager extends Solicitant{
- public GeneralManager(String name) {
- super(name);
- }
-
- @Override
- public void processRequest(Request request) {
- if (request.getRequestType().equals("请假")){
- System.out.println(name +":"+ request.getRequestContent()+"数量"+request.getNumber()+"被批准");
- }else if (request.getRequestType().equals("涨工资") && request.getNumber() <=500){
- System.out.println(name +":"+ request.getRequestContent()+"数量"+request.getNumber()+"被批准");
- }else if (request.getRequestType().equals("涨工资") && request.getNumber() >500){
- System.out.println(name +":"+ request.getRequestContent()+"数量"+request.getNumber()+"再说吧");
- }
- }
- }
(6)最后客户端测试代码
- package com.example;
-
- /**
- * @BelongsProject: 客户端测试类
- * @BelongsPackage: com.example
- * @Author: Mr.zhang
- * @CreateTime: 2022-11-10 21:08
- */
- public class Test1 {
- public static void main(String[] args) {
- TechnicalManager tm = new TechnicalManager("部门经理");
- Majordomo md = new Majordomo("总监");
- GeneralManager gm = new GeneralManager("总经理");
-
- tm.setSolicitant(md);
- md.setSolicitant(gm);
-
- Request request = new Request();
- request.setRequestType("请假");
- request.setRequestContent("小菜因为 个人事务处理 想要请假");
- request.setNumber(1);
-
- tm.processRequest(request);
-
-
- Request request1 = new Request();
- request1.setRequestType("涨工资");
- request1.setRequestContent("小菜因为工资太低,想要涨点工资");
- request1.setNumber(1500);
-
- tm.processRequest(request1);
- }
- }
实现结果:

每一个层级权限明确,要么直接处理,要么不做任何处理上报到上级领导处理,不能出现一个事件由两级不同领导同时处理。