设计模式是软件开发中常用的解决问题的模式化方案。在Java中,有许多常见的设计模式可以用来解决不同类型的问题。本篇博客将详细介绍一些常见的Java设计模式,并提供相应的代码示例。
1. 单例模式(Singleton Pattern)
单例模式是最简单和最常见的设计模式之一。它确保一个类只有一个实例,并提供一个全局访问点。
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
2. 工厂模式(Factory Pattern)
工厂模式是一种创建对象的模式,使用工厂方法替代直接调用构造函数来创建对象。它隐藏了对象的创建逻辑并提供一个统一的接口。
public interface Shape {
void draw();
}
public class Circle implements Shape {
@Override
public void draw() {
System.out.println("画一个圆形");
}
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("画一个矩形");
}
}
public class ShapeFactory {
public Shape getShape(String shapeType) {
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
}
return null;
}
}
public class Main {
public static void main(String[] args) {
ShapeFactory shapeFactory = new ShapeFactory();
Shape shape1 = shapeFactory.getShape("CIRCLE");
shape1.draw();
Shape shape2 = shapeFactory.getShape("RECTANGLE");
shape2.draw();
}
}
3. 观察者模式(Observer Pattern)
观察者模式是一种对象间的一对多的依赖关系,当一个对象的状态发生变化时,它的所有依赖者都会收到通知并自动更新。
import java.util.ArrayList;
import java.util.List;
public interface Subject {
void addObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers();
}
public interface Observer {
void update(String message);
}
public class WeatherStation implements Subject {
private List<Observer> observers = new ArrayList<>();
private String weatherMessage;
@Override
public void addObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(weatherMessage);
}
}
public void setWeatherMessage(String weatherMessage) {
this.weatherMessage = weatherMessage;
notifyObservers();
}
}
public class MobileApp implements Observer {
@Override
public void update(String message) {
System.out.println("Mobile App 收到天气更新:" + message);
}
}
public class WebApp implements Observer {
@Override
public void update(String message) {
System.out.println("Web App 收到天气更新:" + message);
}
}
public class Main {
public static void main(String[] args) {
WeatherStation weatherStation = new WeatherStation();
MobileApp mobileApp = new MobileApp();
WebApp webApp = new WebApp();
weatherStation.addObserver(mobileApp);
weatherStation.addObserver(webApp);
weatherStation.setWeatherMessage("晴天");
weatherStation.removeObserver(webApp);
weatherStation.setWeatherMessage("下雨");
}
}
4. 策略模式(Strategy Pattern)
策略模式是一种行为型模式,它定义了一系列的算法,并将每个算法封装成独立的类,使得它们可以互相替换。客户端可以根据需求选择不同的算法来完成特定的任务。
public interface SortStrategy {
void sort(int[] array);
}
public class BubbleSortStrategy implements SortStrategy {
@Override
public void sort(int[] array) {
// 冒泡排序算法
}
}
public class QuickSortStrategy implements SortStrategy {
@Override
public void sort(int[] array) {
// 快速排序算法
}
}
public class SortManager {
private SortStrategy sortStrategy;
public SortManager(SortStrategy sortStrategy) {
this.sortStrategy = sortStrategy;
}
public void setSortStrategy(SortStrategy sortStrategy) {
this.sortStrategy = sortStrategy;
}
public void sortArray(int[] array) {
sortStrategy.sort(array);
}
}
public class Main {
public static void main(String[] args) {
SortManager sortManager = new SortManager(new BubbleSortStrategy());
int[] array = {5, 2, 8, 1, 9};
sortManager.sortArray(array);
sortManager.setSortStrategy(new QuickSortStrategy());
sortManager.sortArray(array);
}
}
以上介绍了四种常见的Java设计模式:单例模式、工厂模式、观察者模式和策略模式。学习并灵活运用设计模式可以提高代码的可重用性和可维护性,并使你的程序更具扩展性和可靠性。
本文来自极简博客,作者:樱花飘落,转载请注明原文链接:Java中常见的设计模式详解