Java 设计模式代码:解构设计原则5
在软件开发领域,设计模式被视为解决常见编程问题的最佳实践。Java 中的广泛设计模式为开发者提供了建立健壮、可维护和可扩展代码库的工具。本文将深入探讨 Java 中最常用的设计模式的代码示例,揭示它们如何在现实世界场景中应用。
单例模式
代码示例:
```java
public class Singleton {
private static volatile Singleton instance;
private Singleton() {
// 私有构造函数防止外部实例化
}
public static Singleton getInstance() {
if (instance == null) {
synchronized () {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
```
用途:确保在整个应用程序中只有一个特定类的实例。这对于数据库连接、日志记录系统或配置对象等资源非常有用。
建造者模式
代码示例:
```java
public class PizzaBuilder {
private int size;
private List toppings;
public PizzaBuilder setSize(int size) {
= size;
return this;
}
public PizzaBuilder addToppings(String... toppings) {
if ( == null) {
= new ArrayList();
}
(, toppings);
return this;
}
public Pizza build() {
return new Pizza(this);
}
}
public class Pizza {
private int size;
private List toppings;
public Pizza(PizzaBuilder builder) {
= ;
= ;
}
}
```
用途:创建具有不同复杂度的对象,而无需指定确切的构造顺序。这对于具有许多可选配置项或需要灵活定制的类非常有用。
工厂方法模式
代码示例:
```java
public interface Shape {
void draw();
}
public class Circle implements Shape {
@Override
public void draw() {
("Drawing a circle");
}
}
public class Rectangle implements Shape {
@Override
public void draw() {
("Drawing a rectangle");
}
}
public class ShapeFactory {
public Shape getShape(String shapeType) {
switch (shapeType) {
case "circle":
return new Circle();
case "rectangle":
return new Rectangle();
default:
throw new IllegalArgumentException("Invalid shape type");
}
}
}
```
用途:为创建对象提供一个接口,而无需将其具体类公开。这使您可以根据需要轻松添加或删除创建的新类类型,而无需修改客户端代码。
适配器模式
代码示例:
```java
public interface Target {
void operation();
}
public class Adaptee {
void specificOperation() {
("Performing specific operation");
}
}
public class Adapter implements Target {
private Adaptee adaptee;
public Adapter(Adaptee adaptee) {
= adaptee;
}
@Override
public void operation() {
();
}
}
```
用途:将一个类的接口转换为另一个客户端期望的接口。这使您可以在现有代码中使用不兼容的类,而无需重新编写它们。
策略模式
代码示例:
```java
public interface Strategy {
int execute(int a, int b);
}
public class AdditionStrategy implements Strategy {
@Override
public int execute(int a, int b) {
return a + b;
}
}
public class SubtractionStrategy implements Strategy {
@Override
public int execute(int a, int b) {
return a - b;
}
}
public class Context {
private Strategy strategy;
public void setStrategy(Strategy strategy) {
= strategy;
}
public int executeStrategy(int a, int b) {
return (a, b);
}
}
```
用途:定义一系列算法,并使它们可以互换。此模式允许算法独立于使用它们的客户端,从而提高灵活性。
2024-10-29
上一篇:Java 方法抛出异常的全面指南
下一篇:Java中的静态和实例方法
Java方法栈日志的艺术:从错误定位到性能优化的深度指南
https://www.shuihudhg.cn/133725.html
PHP 获取本机端口的全面指南:实践与技巧
https://www.shuihudhg.cn/133724.html
Python内置函数:从核心原理到高级应用,精通Python编程的基石
https://www.shuihudhg.cn/133723.html
Java Stream转数组:从基础到高级,掌握高性能数据转换的艺术
https://www.shuihudhg.cn/133722.html
深入解析:基于Java数组构建简易ATM机系统,从原理到代码实践
https://www.shuihudhg.cn/133721.html
热门文章
Java中数组赋值的全面指南
https://www.shuihudhg.cn/207.html
JavaScript 与 Java:二者有何异同?
https://www.shuihudhg.cn/6764.html
判断 Java 字符串中是否包含特定子字符串
https://www.shuihudhg.cn/3551.html
Java 字符串的切割:分而治之
https://www.shuihudhg.cn/6220.html
Java 输入代码:全面指南
https://www.shuihudhg.cn/1064.html