设计模式-行为型-策略模式(Strategy)
参考文献
https://www.oodesign.com/strategy-pattern
https://java-design-patterns.com/patterns/strategy/
https://refactoringguru.cn/design-patterns/strategy
策略模式
定义一系列算法,封装每个算法,并使它们可以互换.策略使算法能够独立于使用它的客户端而变化.
组件
Strategy(策略): 定义所有支持的算法通用的接口. Context 使用此接口来调用 ConcreteStrategy 定义的算法.
ConcreteStrategy(具体策略): 每个具体策略都实现一个算法.它们实现了 Strategy 接口定义的方法,提供了不同的算法实现
Context(上下文)
包含对策略对象的引用.
可以定义一个接口,让策略访问其数据.
Context 对象包含对应使用的ConcreteStrategy 的引用.当需要执行操作时,算法将从策略对象运行.上下文不知道策略的实施.如有必要,可以定义附加对象以将数据从上下文对象传递到策略.
...
设计模式-行为型-备忘录模式(Memento)
参考文献
https://www.oodesign.com/memento-pattern
https://java-design-patterns.com/patterns/memento/
https://refactoringguru.cn/design-patterns/memento
备忘录模式
允许在不暴露对象实现细节的情况下保存和恢复对象之前的状态.
组件
Memento 备忘录
存储 Originator对象的内部状态.状态可以包括任意数量的状态变量.
备忘录必须具有两个接口,一个接口用于负责人(Caretaker).这个接口不能允许任何操作或者访问备忘录存储的内部状态,从而遵循封装原则.另一个接口用于原发器(Originator),允许原发器访问任何必要的状态变量以恢复先前的状态
Originator 原发器
创建一个备忘录对象,用于捕获原发器的内部状态.
使用备忘录对象来恢复先前的状态.
Caretaker 负责人
负责保持备忘录.
备忘录对于负责人来说是不透明的,而负责人不能对其进行操作.
实现方式
确定担任原发器角色的类取决于你的 ...
设计模式-行为型-状态模式(State)
参考文献
https://refactoringguru.cn/design-patterns/state
https://java-design-patterns.com/patterns/state/
状态模式
允许对象在其内部状态发生变化时改变其行为
组件
状态接口(State Interface):定义了表示各种状态的方法,具体状态类必须实现该接口.
具体状态类(Concrete State Class):实现了状态接口的具体状态类,每个具体状态类负责处理特定的状态下的行为.
上下文类(Context Class):维护一个对当前状态对象的引用,提供了用于切换状态和执行相应操作的方法.
环境类状态切换方法(Transition Methods in Context Class):在上下文类中提供了用于切换状态的方法,允许客户端自行决定何时切换状态.
共享状态(Shared State):多个上下文对象共享同一个状态对象的情况.这种情况下,可以将状态对象设计成可共享的,以减少对象的创建和内存占用.
实现方式
定义状态接口(State Interface):创 ...
设计模式-创建型-命令模式(Command)
参考文献
https://www.oodesign.com/command-pattern
https://refactoringguru.cn/design-patterns/command
https://java-design-patterns.com/patterns/command/
命令模式
它可将请求转换为一个包含与请求相关的所有信息的独立对象. 该转换让你能根据不同的请求将方法参数化、 延迟请求执行或将其放入队列中, 且能实现可撤销操作.
组件
Command - 接口通常仅声明一个执行命令的方法.
ConcreteCommand - 会实现各种类型的请求. 具体命令自身并不完成工作, 而是会将调用委派给一个业务逻辑对象. 但为了简化代码, 这些类可以进行合并.
接收对象执行方法所需的参数可以声明为具体命令的成员变量. 你可以将命令对象设为不可变, 仅允许通过构造函数对这些成员变量进行初始化.
Client - 会创建并配置具体命令对象. 客户端必须将包括接收者实体在内的所有请求参数传递给命令的构造函数. 此后, 生成的命令就可以与一个或多个发送者 ...
设计模式-结构型-组合模式(Composite)
参考文献
https://www.oodesign.com/composite-pattern
https://refactoringguru.cn/design-patterns/composite
组合模式
允许将对象组织成树形结构,以表示“整体-部分”层次关系,并且能够以统一的方式处理单个对象和组合对象.
组件
组件(Component):抽象类或接口,定义了组合中所有对象共有的操作,可以是抽象类或接口.它可以包含对子组件进行管理的方法,比如添加、删除、获取子组件等.
叶子节点(Leaf):代表组合中的叶子对象,叶子节点没有子节点,实现了组件的操作方法.
容器节点(Composite):代表组合中的容器对象,可以包含子节点,实现了组件的操作方法.容器节点可以存储叶子节点和其他容器节点,形成树形结构.
客户端(Client):使用组合模式的外部代码,通过组件接口操作组合对象.客户端通常无法区分处理的是单个对象还是组合对象,因为它们都遵循同样的接口
实现方式
使用抽象类或接口定义组件(Component),其中包含对子组件的管理方法,如添加、删除、获取子组件等.
创建叶 ...
设计模式-行为型-责任链模式(Chain of Responsibility)
参考文献
https://www.oodesign.com/chain-of-responsibility-pattern
https://java-design-patterns.com/patterns/chain-of-responsibility/
https://refactoringguru.cn/design-patterns/chain-of-responsibility
责任链模式
允许你将请求沿着处理者链进行发送. 收到请求后, 每个处理者均可对请求进行处理, 或将其传递给链上的下个处理者.
组件
Request: 责任链中的请求对象
1public class Request{}
Handler: 定义处理请求的接口
1234public interface Handler{ boolean shouldSkip(Request request); boolean handle(Request request);}
RequestHandler: 实际处理请求的类
如果它可以处理请求,则进行处理,否则将 ...
设计模式-行为型-模版方法模式(Template Method)
参考文献
https://refactoringguru.cn/design-patterns/template-method
https://www.oodesign.com/template-method-pattern
模版方法模式
模板方法使用子类重写的抽象操作在基类中定义算法以提供具体行为.
定义操作中算法的骨架,将某些步骤推迟到子类中.
模板方法允许子类重新定义算法的某些步骤,而不让它们改变算法的结构.
模板方法模式建议将算法分解为一系列步骤, 然后将这些步骤改写为方法, 最后在 “模板方法” 中依次调用这些方法. 步骤可以是 抽象的, 也可以有一些默认的实现. 为了能够使用算法, 客户端需要自行提供子类并实现所有的抽象步骤. 如有必要还需重写一些步骤 (但这一步中不包括模板方法自身).
组件
AbstractClass(抽象类): 定义具体子类定义的抽象基元操作以实现算法的步骤.
实现定义算法骨架的模板方法.模板方法调用原始操作以及 AbstractClass 或其他对象中定义的操作
ConcreteClass(具体类): 实现原始操作以执行 ...
设计模式-行为型-空对象模式(Null Object)
参考文献
https://www.oodesign.com/null-object-pattern
https://java-design-patterns.com/patterns/null-object/
空对象模式
它用于处理对象引用为空时的情况.
该模式的目标是消除对空引用的显式检查,从而提高代码可读性和可维护性.
组件
AbstractClass: 定义具体实现必须定义的抽象基元操作.
RealClass: AbstractClass的实际实现,执行一些实际操作
NullClass: 不执行任何抽象类操作的实现,以便向客户端提供非空对象
Client: 客户端获取抽象类的实现并使用它.它并不真正关心实现是空对象还是真实对象,因为它们都以相同的方式使用
实现方式
定义一个共同的接口或抽象类,其中包含实际对象和空对象的共同方法.
创建一个实际对象类,实现共同接口,并提供具体的实现逻辑.
创建一个空对象类,也实现共同接口,但在方法中提供空实现.
在需要使用对象的地方,使用空对象作为默认值,而不是使用 null.
使用场景
避免在代码中频繁进行 null 引用的检查 ...
设计模式-行为型-访问者模式(Visitor)
参考文献
https://java-design-patterns.com/patterns/visitor/
https://www.oodesign.com/visitor-pattern
https://refactoringguru.cn/design-patterns/visitor
访问者模式
表示要对对象结构的元素执行的操作
Visitor允许您定义新操作,而无需更改其操作的元素的类
组件
Visitor: 这是一个接口或抽象类,用于声明所有类型的可访问类的访问操作.通常操作的名称是相同的,并且操作通过方法签名来区分:输入对象类型决定调用哪个方法.
ConcreteVisitor: 对于每种类型的访问者,必须实现在抽象访问者中声明的所有访问方法.每个访客将负责不同的操作.当定义一个新的访问者时,它必须被传递到对象结构.
Visitable : 是声明接受操作的抽象.这是访问者对象能够“访问”对象的入口点.集合中的每个对象都应该实现此抽象,以便能够被访问.
ConcreteVisitable: 这些类实现 Visitable 接口或类并定义接受操作.使用acce ...
设计模式-创建型-单例模式(Singleton)
参考文献
https://java-design-patterns.com/patterns/singleton/
https://www.oodesign.com/singleton-pattern
https://refactoringguru.cn/design-patterns/singleton
单例模式
保证一个类只有一个实例,并提供一个访问该实例的全局节点.
组件
在单例模式中,只有Singleton这一个角色.
Singleton角色中有一个返回唯一实例的static方法.该方法总是返回同一个实例.
实现方式
在类中添加一个私有静态成员变量用于保存单例实例.
声明一个公有静态构建方法用于获取单例实例.
在静态方法中实现"延迟初始化". 该方法会在首次被调用时创建一个新对象, 并将其存储在静态成员变量中. 此后该方法每次被调用时都返回该实例.
将类的构造函数设为私有. 类的静态方法仍能调用构造函数, 但是其他对象不能调用.
检查客户端代码, 将对单例的构造函数的调用替换为对其静态构建方法的调用.
具体实现
懒汉式
12345678910 ...