Design Patterns in Java- A simplified Guide #1
来源:dev.to
时间:2024-12-15 16:25:15 455浏览 收藏
在文章实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《Design Patterns in Java- A simplified Guide #1》,聊聊,希望可以帮助到正在努力赚钱的你。

欢迎阅读这个由 3 部分组成的指南。在这篇文章中,我将提供设计模式的基本介绍以及示例。
这篇文章将介绍什么是设计模式、为什么我们应该了解它们、设计模式的类型和创建模式。
快速链接:
- 创作模式
- 结构模式
- 行为模式
什么是设计模式?
设计模式是软件开发中常见问题的经过验证的解决方案。它们帮助开发人员以结构化的方式解决复杂的任务,并允许他们重用成功的解决方案,使他们的代码更易于理解和维护。将设计模式视为解决构建软件时出现的典型问题的蓝图。
为什么我们应该了解它们?
了解设计模式可以帮助开发人员以结构化且高效的方式解决常见问题,使代码更具可重用性、可维护性和可扩展性。
想象一下餐厅的厨房。厨师按照食谱(设计模式)准备菜肴。厨师没有每次都重新发明流程,而是使用经过验证的步骤来始终如一地提供相同质量的菜肴。同样,设计模式可以帮助开发人员高效地解决问题,而无需每次都重新发明解决方案。
设计模式类型:
在 java 中,设计模式分为三大类:
1。创建模式:这些模式有助于以灵活且可重用的方式创建对象。
2.结构模式:这些模式重点关注对象如何排列和连接以形成更大的系统。
3.行为模式: 这些模式处理对象如何相互交互和通信。
创作模式
创建模式专注于对象创建,使以灵活、可重用的方式创建对象变得更加容易。这些模式有助于确保对象创建过程独立于使用它的系统。
抽象工厂:
想象一个游戏,您可以在不同的“主题”(例如中世纪、科幻)之间进行选择。抽象工厂可用于创建特定于所选主题的相关对象集(例如角色、武器和设置),而无需更改游戏的核心逻辑。
interface gamefactory {
character createcharacter();
weapon createweapon();
}
class medievalfactory implements gamefactory {
public character createcharacter() {
return new knight();
}
public weapon createweapon() {
return new sword();
}
}
class scififactory implements gamefactory {
public character createcharacter() {
return new robot();
}
public weapon createweapon() {
return new lasergun();
}
}
建造者:
考虑制作一个复杂的三明治,您可以在其中自定义成分(例如面包、肉类、蔬菜的类型)。构建器模式有助于逐步组装这些成分。
class sandwich {
private string bread;
private string meat;
private string cheese;
public void setbread(string bread) { this.bread = bread; }
public void setmeat(string meat) { this.meat = meat; }
public void setcheese(string cheese) { this.cheese = cheese; }
}
class sandwichbuilder {
private sandwich sandwich = new sandwich();
public sandwichbuilder addbread(string bread) {
sandwich.setbread(bread);
return this;
}
public sandwichbuilder addmeat(string meat) {
sandwich.setmeat(meat);
return this;
}
public sandwichbuilder addcheese(string cheese) {
sandwich.setcheese(cheese);
return this;
}
public sandwich build() {
return sandwich;
}
}
单例:
整个应用程序中您只需要一个数据库连接实例。单例模式确保仅创建数据库连接的单个实例。
class databaseconnection {
private static databaseconnection instance;
private databaseconnection() {} // private constructor
public static databaseconnection getinstance() {
if (instance == null) {
instance = new databaseconnection();
}
return instance;
}
}
原型:
想象一下复制复杂的对象,例如包含各种元素的“高级”文档模板。您可以复制文档原型并进行修改,而不是从头开始创建它。
abstract class documentprototype {
abstract documentprototype clone();
}
class document extends documentprototype {
private string content;
public document(string content) {
this.content = content;
}
public documentprototype clone() {
return new document(this.content);
}
}
对象池:
假设打印系统中的打印机对象数量有限。对象池无需每次都创建新的打印机对象,而是有助于高效地管理和重用打印机。
class printer {
public void print() {
system.out.println("printing document...");
}
}
class objectpool {
private list<printer> availableprinters = new arraylist<>();
public printer getprinter() {
if (availableprinters.isempty()) {
return new printer();
}
return availableprinters.remove(0);
}
public void releaseprinter(printer printer) {
availableprinters.add(printer);
}
}
工厂方法:
您正在构建一个需要不同类型车辆的系统。工厂方法模式允许您根据特定条件决定创建哪种车辆。
interface Vehicle {
void drive();
}
class Car implements Vehicle {
public void drive() {
System.out.println("Driving a car");
}
}
class Bike implements Vehicle {
public void drive() {
System.out.println("Riding a bike");
}
}
abstract class VehicleFactory {
abstract Vehicle createVehicle();
}
class CarFactory extends VehicleFactory {
public Vehicle createVehicle() {
return new Car();
}
}
class BikeFactory extends VehicleFactory {
public Vehicle createVehicle() {
return new Bike();
}
}
今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
158 收藏
-
451 收藏
-
242 收藏
-
243 收藏
-
450 收藏
-
271 收藏
-
149 收藏
-
267 收藏
-
220 收藏
-
337 收藏
-
470 收藏
-
361 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习