当前位置: 首页 > news >正文

编程中的智慧之设计模式二

设计模式:深度解析与实战应用

在上一篇文章中,我们探讨了创建型模式、结构型模式和行为模式中的一些常用模式及其Java实现。本篇将继续深入探讨设计模式,重点介绍更多的行为模式以及架构模式在实际开发中的应用。

行为模式

责任链模式(Chain of Responsibility Pattern)

责任链模式通过为多个对象处理一个请求创建一条链。每个对象都包含对另一个对象的引用,如果请求不能被处理,则将请求传递给链中的下一个对象。

public abstract class Handler {protected Handler successor;public void setSuccessor(Handler successor) {this.successor = successor;}public abstract void handleRequest(int request);
}public class ConcreteHandlerA extends Handler {@Overridepublic void handleRequest(int request) {if (request >= 0 && request < 10) {System.out.println("Handler A handled request " + request);} else if (successor != null) {successor.handleRequest(request);}}
}public class ConcreteHandlerB extends Handler {@Overridepublic void handleRequest(int request) {if (request >= 10 && request < 20) {System.out.println("Handler B handled request " + request);} else if (successor != null) {successor.handleRequest(request);}}
}public class Client {public static void main(String[] args) {Handler handlerA = new ConcreteHandlerA();Handler handlerB = new ConcreteHandlerB();handlerA.setSuccessor(handlerB);int[] requests = {5, 14, 22, 18};for (int request : requests) {handlerA.handleRequest(request);}}
}

命令模式(Command Pattern)

命令模式将请求封装成对象,从而可以使用不同的请求、队列或者日志参数化其他对象。命令模式也支持可撤销的操作。

public interface Command {void execute();
}public class Light {public void turnOn() {System.out.println("Light is on");}public void turnOff() {System.out.println("Light is off");}
}public class LightOnCommand implements Command {private Light light;public LightOnCommand(Light light) {this.light = light;}@Overridepublic void execute() {light.turnOn();}
}public class LightOffCommand implements Command {private Light light;public LightOffCommand(Light light) {this.light = light;}@Overridepublic void execute() {light.turnOff();}
}public class RemoteControl {private Command command;public void setCommand(Command command) {this.command = command;}public void pressButton() {command.execute();}
}public class Client {public static void main(String[] args) {Light light = new Light();Command lightOn = new LightOnCommand(light);Command lightOff = new LightOffCommand(light);RemoteControl remote = new RemoteControl();remote.setCommand(lightOn);remote.pressButton();remote.setCommand(lightOff);remote.pressButton();}
}

架构模式

架构模式是最高层次的模式,适用于整个应用程序的架构设计。它们定义了系统的整体结构和行为。

模型-视图-控制器(MVC)模式

MVC模式将应用程序分为三部分:模型、视图和控制器。模型代表数据和业务逻辑,视图负责显示,控制器处理输入。

// Model
public class Student {private String rollNo;private String name;public String getRollNo() {return rollNo;}public void setRollNo(String rollNo) {this.rollNo = rollNo;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}// View
public class StudentView {public void printStudentDetails(String studentName, String studentRollNo) {System.out.println("Student: ");System.out.println("Name: " + studentName);System.out.println("Roll No: " + studentRollNo);}
}// Controller
public class StudentController {private Student model;private StudentView view;public StudentController(Student model, StudentView view) {this.model = model;this.view = view;}public void setStudentName(String name) {model.setName(name);}public String getStudentName() {return model.getName();}public void setStudentRollNo(String rollNo) {model.setRollNo(rollNo);}public String getStudentRollNo() {return model.getRollNo();}public void updateView() {view.printStudentDetails(model.getName(), model.getRollNo());}
}public class MVCPatternDemo {public static void main(String[] args) {Student model = retrieveStudentFromDatabase();StudentView view = new StudentView();StudentController controller = new StudentController(model, view);controller.updateView();controller.setStudentName("John");controller.updateView();}private static Student retrieveStudentFromDatabase() {Student student = new Student();student.setName("Robert");student.setRollNo("10");return student;}
}

其他架构模式

  1. 微服务架构(Microservices Architecture):将应用程序分解为小的、自治的服务,每个服务都运行在自己的进程中,并通过轻量级机制(通常是HTTP资源API)进行通信。
  2. 事件驱动架构(Event-Driven Architecture):基于事件进行通信和协调,通常使用消息队列或事件总线。
  3. 分层架构(Layered Architecture):将应用程序分为不同的层次,每一层都具有特定的职责,如表示层、业务逻辑层和数据访问层。

结论

设计模式为开发人员提供了有效的解决方案,使他们能够应对各种软件设计问题。在实际应用中,理解并熟练使用设计模式可以显著提高软件的质量和维护性。通过本文和上一篇的介绍,相信读者对设计模式有了更深入的理解,能够在实际项目中灵活应用这些模式来构建高效、稳定的系统。


http://www.lryc.cn/news/401828.html

相关文章:

  • 基于python的百度资讯爬虫的设计与实现
  • 用 WireShark 抓住 TCP
  • Lua基础知识入门
  • 【机器学习实战】Datawhale夏令营2:深度学习回顾
  • 开发扫地机器人系统时无法兼容手机解决方案
  • Elasticsearch 角色和权限管理
  • 华为HCIP Datacom H12-821 卷42
  • 【精品资料】物业行业BI大数据解决方案(43页PPT)
  • 推荐一款处理TCP数据的架构--EasyTcp4Net
  • 2、电脑各部件品牌介绍 - 计算机硬件品牌系列文章
  • Git【撤销远程提交记录】
  • java基础学习:序列化之 - Fast serialization
  • Microsoft Build 2024 推出 .NET 9:Tensor<T>、 OpenAI Collaboration和.NET Aspire
  • 【Neural signal processing and analysis zero to hero】- 2
  • 好用的AI搜索引擎
  • 十、Java集合 ★ ✔(模块18-20)【泛型、通配符、List、Set、TreeSet、自然排序和比较器排序、Collections、可变参数、Map】
  • 阿里云开源 Qwen2-Audio 音频聊天和预训练大型音频语言模型
  • SpringBoot集成MQTT实现交互服务通信
  • python实现插入排序、快速排序
  • Spring Boot集成kudu快速入门Demo
  • html超文本传输协议
  • 利用AI辅助制作ppt封面
  • 【spring boot】初学者项目快速练手
  • Laravel+swoole 实现websocket长链接
  • 【C#】Array和List
  • SpringCloud网关的实现原理与使用指南
  • LabVIEW 与 PLC 通讯方式
  • 数据结构初阶·排序算法(内排序)
  • PL/SQL oracle上多表关联的一些记录
  • Java.Net.UnknownHostException:揭开网络迷雾,解锁异常处理秘籍