
欢迎阅读这个由 3 部分组成的指南。在这篇文章中,我将提供设计模式的基本介绍以及示例。
这篇文章将介绍什么是设计模式、为什么我们应该了解它们、设计模式的类型和创建模式。
快速链接:
什么是设计模式?
立即学习“Java免费学习笔记(深入)”;
设计模式是软件开发中常见问题的经过验证的解决方案。它们帮助开发人员以结构化的方式解决复杂的任务,并允许他们重用成功的解决方案,使他们的代码更易于理解和维护。将设计模式视为解决构建软件时出现的典型问题的蓝图。
为什么我们应该了解它们?
了解设计模式可以帮助开发人员以结构化且高效的方式解决常见问题,使代码更具可重用性、可维护性和可扩展性。
想象一下餐厅的厨房。厨师按照食谱(设计模式)准备菜肴。厨师没有每次都重新发明流程,而是使用经过验证的步骤来始终如一地提供相同质量的菜肴。同样,设计模式可以帮助开发人员高效地解决问题,而无需每次都重新发明解决方案。
设计模式类型:
在 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();
}
}
以上就是Design Patterns in Java- A simplified Guide #1的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号