
欢迎阅读这个由 3 部分组成的指南。在这篇文章中,我将提供设计模式的基本介绍以及示例。
这篇文章将介绍什么是设计模式、为什么我们应该了解它们、设计模式的类型和行为模式。
快速链接:
什么是设计模式?
立即学习“Java免费学习笔记(深入)”;
设计模式是软件开发中常见问题的经过验证的解决方案。它们帮助开发人员以结构化的方式解决复杂的任务,并允许他们重用成功的解决方案,使他们的代码更易于理解和维护。将设计模式视为解决构建软件时出现的典型问题的蓝图。
为什么我们应该了解它们?
了解设计模式可以帮助开发人员以结构化且高效的方式解决常见问题,使代码更具可重用性、可维护性和可扩展性。
想象一下餐厅的厨房。厨师按照食谱(设计模式)准备菜肴。厨师没有每次都重新发明流程,而是使用经过验证的步骤来始终如一地提供相同质量的菜肴。同样,设计模式可以帮助开发人员高效地解决问题,而无需每次都重新发明解决方案。
设计模式类型:
在 java 中,设计模式分为三大类:
1。创建模式:这些模式有助于以灵活且可重用的方式创建对象。
2.结构模式:这些模式重点关注对象如何排列和连接以形成更大的系统。
3.行为模式: 这些模式处理对象如何相互交互和通信。
行为模式
行为模式涉及对象如何相互交互和通信。
责任链:
在订单处理系统中,您可能对订单有不同级别的审批(例如,小订单交给店员,大订单交给经理)。责任链模式允许您沿着一系列处理程序传递请求,直到有人处理它为止。
abstract class orderhandler {
protected orderhandler nexthandler;
public void setnexthandler(orderhandler handler) {
this.nexthandler = handler;
}
public abstract void handleorder(int amount);
}
class clerkhandler extends orderhandler {
public void handleorder(int amount) {
if (amount < 500) {
system.out.println("clerk handling order");
} else if (nexthandler != null) {
nexthandler.handleorder(amount);
}
}
}
class managerhandler extends orderhandler {
public void handleorder(int amount) {
if (amount >= 500) {
system.out.println("manager handling order");
} else if (nexthandler != null) {
nexthandler.handleorder(amount);
}
}
}
命令:
在文本编辑器中,命令模式可用于定义复制、粘贴和撤消等操作,以便将每个操作封装为一个对象。
interface command {
void execute();
}
class copycommand implements command {
private texteditor editor;
public copycommand(texteditor editor) {
this.editor = editor;
}
public void execute() {
editor.copytext();
}
}
class texteditor {
public void copytext() {
system.out.println("text copied");
}
}
观察者:
在天气监测系统中,只要天气条件发生变化,就需要更新多个设备(例如移动应用程序、气象站)。
interface observer {
void update(string weather);
}
class weatherstation {
private list<observer> observers = new arraylist<>();
public void addobserver(observer observer) {
observers.add(observer);
}
public void notifyobservers(string weather) {
for (observer observer : observers) {
observer.update(weather);
}
}
}
class weatherapp implements observer {
public void update(string weather) {
system.out.println("weather update: " + weather);
}
}
策略:
您可以使用不同的策略来计算运输成本(例如,常规运输、加急运输)。策略模式允许您在这些策略之间动态切换。
interface shippingstrategy {
double calculateshippingcost(double weight);
}
class regularshipping implements shippingstrategy {
public double calculateshippingcost(double weight) {
return weight * 2;
}
}
class expeditedshipping implements shippingstrategy {
public double calculateshippingcost(double weight) {
return weight * 5;
}
}
模板方法:
在游戏中,您可能有不同类型的角色,它们遵循一组相似的攻击步骤,但具体细节有所不同。模板方法允许您定义通用算法,同时允许子类覆盖某些步骤。
abstract class GameCharacter {
public void attack() {
moveToTarget();
performAttack();
celebrate();
}
protected void moveToTarget() {
System.out.println("Moving to target...");
}
protected abstract void performAttack();
protected void celebrate() {
System.out.println("Celebrating after attack...");
}
}
class Warrior extends GameCharacter {
protected void performAttack() {
System.out.println("Warrior performs sword attack!");
}
}
以上就是Design Patterns in Java- A simplified Guide #3的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号