本文所有案例代码
码云:https://gitee.com/helloworld6379/designPattern
Github:Github地址
设计模式概述
1 设计模式是程序员在面对同类软件工程设计问题所总结出来的有用的经验,模式不是代码,而是某类问题的通
用解决方案,设计模式(Design pattern)代表了最佳的实践。这些解决方案是众多软件开发人员经过相当长的
一段时间的试验和错误总结出来的。
2 设计模式的本质提高 软件的维护性,通用性和扩展性,并降低软件的复杂度。
3 设计模式并不局限于某种语言,java,php,c++ 都有设计模式.
设计模式类型
设计模式分为三种类型,共 23 种
1 创建型模式:单例模式、抽象工厂模式、原型模式、建造者模式、工厂模式。
2 结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。
3 行为型模式:模版方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter 模式)、状态模式、策略模式、职责链模式(责任链模式)。
简单介绍
意图:避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。
主要解决:职责链上的处理者负责处理请求,客户只需要将请求发送到职责链上即可,无须关心请求的处理细节和请求的传递,所以职责链将请求的发送者和请求的处理者解耦了。
何时使用:在处理消息的时候以过滤很多道。
实现
通过不同的日志级别打印不同级别日志。
UML图:
代码:
package com.struggle.pattern.responsibilitychain;
/**
* @Description 日志抽象类
* @Author: LiuXing
* @Date: 2020/6/15 22:33
*/
public abstract class AbstractLogger {
public static int INFO = 1;
public static int DEBUG = 2;
public static int ERROR = 3;
protected int level;
//责任链中的下一个元素
protected AbstractLogger nextLogger;
public void setNextLogger(AbstractLogger nextLogger){
this.nextLogger = nextLogger;
}
public void logMessage(int level, String message){
if(this.level <= level){
write(message);
}
if(nextLogger !=null){
nextLogger.logMessage(level, message);
}
}
abstract protected void write(String message);
}
package com.struggle.pattern.responsibilitychain;
/**
* @Description Debug级别日志
* @Author: LiuXing
* @Date: 2020/6/15 22:31
*/
public class DebugLogger extends AbstractLogger {
public DebugLogger(int level){
this.level = level;
}
@Override
protected void write(String message) {
System.out.println("File::Logger: " + message);
}
}
package com.struggle.pattern.responsibilitychain;
/**
* @Description Error级别日志
* @Author: LiuXing
* @Date: 2020/6/15 22:31
*/
public class ErrorLogger extends AbstractLogger {
public ErrorLogger(int level){
this.level = level;
}
@Override
protected void write(String message) {
System.out.println("Error Console::Logger: " + message);
}
}
package com.struggle.pattern.responsibilitychain;
/**
* @Description Info级别日志
* @Author: LiuXing
* @Date: 2020/6/15 2InfoLogger2:31
*/
public class InfoLogger extends AbstractLogger {
public InfoLogger(int level){
this.level = level;
}
@Override
protected void write(String message) {
System.out.println("InfoLogger Console::Logger: " + message);
}
}
package com.struggle.pattern.responsibilitychain;
/**
* @Description 职责链模式测试
* @Author: LiuXing
* @Date: 2020/6/15 23:01
*/
public class ChainPatternDemo {
private static AbstractLogger getChainOfLoggers(){
AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);
AbstractLogger fileLogger = new DebugLogger(AbstractLogger.DEBUG);
AbstractLogger consoleLogger = new InfoLogger(AbstractLogger.INFO);
//设置下一个日志记录器
errorLogger.setNextLogger(fileLogger);
fileLogger.setNextLogger(consoleLogger);
return errorLogger;
}
public static void main(String[] args) {
AbstractLogger loggerChain = getChainOfLoggers();
loggerChain.logMessage(AbstractLogger.INFO, "This is an information.");
loggerChain.logMessage(AbstractLogger.DEBUG,
"This is a debug level information.");
loggerChain.logMessage(AbstractLogger.ERROR,
"This is an error information.");
}
}