21xrx.com
2025-03-23 07:24:59 Sunday
文章检索 我的文章 写文章
Java开发常用的设计模式及代码案例
2023-06-15 13:47:04 深夜i     --     --
Java开发 设计模式 单例模式 工厂模式 观察者模式

设计模式是用来解决软件设计中常见问题的可重复解决方案。在Java开发中,设计模式是非常常见且重要的,能够帮助开发者提高代码质量、可维护性和可扩展性。下面将介绍Java开发常用的设计模式及其代码案例。

1.单例模式

单例模式是一种保证在整个应用中只存在一个实例对象的设计模式,它可以避免过多的对象创建造成的系统资源浪费问题。以下是单例模式的代码案例:

public class Singleton {  
  private static Singleton instance = null;  
  private Singleton()   
    
  public static Singleton getInstance() {  
    if (instance == null) {  
      synchronized (Singleton.class) {  
        if (instance == null) {  
          instance = new Singleton();  
        }  
      }  
    }  
    return instance;  
  }  
}

2.工厂模式

工厂模式是一种基于接口的设计模式,它提供了一种创建对象的最佳方式。工厂模式封装了对象创建的细节,使得代码更加灵活和可维护。以下是工厂模式的代码案例:

public interface Shape {  
  void draw();  
} 
public class Circle implements Shape {  
  @Override  
  public void draw() {  
    System.out.println("Draw a circle");  
  }  
}  
public class Rectangle implements Shape {  
  @Override  
  public void draw() {  
    System.out.println("Draw a rectangle");  
  }  
} 
public class ShapeFactory {  
  public Shape getShape(String shapeType) {  
    if (shapeType == null)   
      return null;  
      
    if (shapeType.equalsIgnoreCase("CIRCLE")) {  
      return new Circle();  
    } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {  
      return new Rectangle();  
    }  
    return null;  
  }  
}

3.观察者模式

观察者模式是一种对象间的一对多依赖关系。当一个对象的状态发生改变时,它所有的依赖对象都将得到通知并自动更新。以下是观察者模式的代码案例:

public interface Observer {  
  void update(float temperature, float humidity, float pressure);  
}  
public interface Subject {  
  void registerObserver(Observer o);  
  void removeObserver(Observer o);  
  void notifyObservers();  
}  
public class WeatherData implements Subject {  
  private List
  observers;  
 
  private float temperature;  
  private float humidity;  
  private float pressure;  
  public WeatherData() {  
    observers = new ArrayList
  ();  
 
  }  
  @Override  
  public void registerObserver(Observer o) {  
    observers.add(o);  
  }  
  @Override  
  public void removeObserver(Observer o) {  
    if (observers.indexOf(o) >= 0) {  
      observers.remove(o);  
    }  
  }  
  @Override  
  public void notifyObservers() {  
    for (Observer o : observers) {  
      o.update(temperature, humidity, pressure);  
    }  
  }  
  public void measurementsChanged() {  
    notifyObservers();  
  }  
  public void setMeasurements(float temperature, float humidity, float pressure) {  
    this.temperature = temperature;  
    this.humidity = humidity;  
    this.pressure = pressure;  
    measurementsChanged();  
  }  
} 
public class CurrentConditionsDisplay implements Observer {  
  private float temperature;  
  private float humidity;  
  private Subject weatherData;  
  public CurrentConditionsDisplay(Subject weatherData) {  
    this.weatherData = weatherData;  
    weatherData.registerObserver(this);  
  }  
  @Override  
  public void update(float temperature, float humidity, float pressure) {  
    this.temperature = temperature;  
    this.humidity = humidity;  
    display();  
  }  
  public void display() {  
    System.out.println("Current temperature and humidity: " + temperature + "F degrees and " + humidity + "% humidity");  
  }  
}

  
  

评论区