21xrx.com
2025-03-25 05:45:11 Tuesday
文章检索 我的文章 写文章
Java设计模式课后题解析与代码案例
2023-06-16 17:07:10 深夜i     9     0
Java设计模式 代码案例 工厂模式 单例模式 观察者模式

Java设计模式是Java开发中必须了解的一门重要课程,针对课后题提供了详细解析与相应代码案例,让大家能够更好地理解和应用Java设计模式。以下为题目及详细解析与代码案例。

### 题目一

设计一个简单的工厂模式:根据用户的选择,动态实例化产品对象并调用其方法输出。

解析:工厂模式是一种创建型模式,通过定义工厂类,由工厂类对产品对象进行初始化、创建和管理,将对象的实例化过程封装起来,客户端只需要知道要实例化哪一个产品即可,将对象实例化的具体过程交给工厂类完成。下面是代码案例:

interface Product {
  void show();
}
class ProductOne implements Product {
  public void show() {
    System.out.println("This is product one");
  }
}
class ProductTwo implements Product {
  public void show() {
    System.out.println("This is product two");
  }
}
class ProductFactory {
  public Product createProduct(String type) {
    if (type.equals("one")) {
      return new ProductOne();
    } else if (type.equals("two")) {
      return new ProductTwo();
    } else
      return null;
    
  }
}
class Client {
  public static void main(String[] args) {
    ProductFactory factory = new ProductFactory();
    Product product1 = factory.createProduct("one");
    Product product2 = factory.createProduct("two");
    product1.show();
    product2.show();
  }
}

### 题目二

设计一个单例模式:实现一个线程安全的单例模式,并对外提供一个获取实例对象的静态方法。

解析:单例模式是一种创建型模式,常用于需要确保唯一性的对象实例化过程,保证系统中该对象只存在一个实例,并且提供一个全局访问该对象的方法。下面是线程安全单例的代码案例:

class Singleton {
  private static Singleton instance = null;
  private Singleton()
  
  public synchronized static Singleton getInstance() {
    if (instance == null) {
      instance = new Singleton();
    }
    return instance;
  }
}
class Client {
  public static void main(String[] args) {
    Singleton singleton1 = Singleton.getInstance();
    Singleton singleton2 = Singleton.getInstance();
    System.out.println(singleton1);
    System.out.println(singleton2);
    System.out.println(singleton1 == singleton2);
  }
}

### 题目三

设计一个观察者模式:实现一个主题和两个观察者,当主题状态发生改变时,通知所有的观察者更新状态。

解析:观察者模式是一种行为型模式,主要用于对象之间的一对多依赖关系,当一个对象状态发生改变时,通知所有依赖于它的对象。下面是观察者模式的代码案例:

interface Observer {
  void update(float temperature, float humidity, float pressure);
}
interface Subject {
  void registerObserver(Observer o);
  void removeObserver(Observer o);
  void notifyObserver();
}
class WeatherData implements Subject {
  private List
  observers;
 
  private float temperature;
  private float humidity;
  private float pressure;
  public WeatherData() {
    observers = new ArrayList
  ();
 
  }
  public void registerObserver(Observer o) {
    observers.add(o);
  }
  public void removeObserver(Observer o) {
    observers.remove(o);
  }
  public void notifyObserver() {
    for (Observer observer : observers) {
      observer.update(temperature, humidity, pressure);
    }
  }
  public void setMeasurements(float temperature, float humidity, float pressure) {
    this.temperature = temperature;
    this.humidity = humidity;
    this.pressure = pressure;
    measurementsChanged();
  }
  public void measurementsChanged() {
    notifyObserver();
  }
}
class CurrentConditionsDisplay implements Observer {
  private float temperature;
  private float humidity;
  public void update(float temperature, float humidity, float pressure) {
    this.temperature = temperature;
    this.humidity = humidity;
    display();
  }
  public void display() {
    System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity");
  }
}
class StatisticsDisplay implements Observer {
  private float temperature;
  public void update(float temperature, float humidity, float pressure) {
    this.temperature = temperature;
    display();
  }
  public void display() {
    System.out.println("Temperature statistics: " + temperature + "F degrees");
  }
}
class WeatherStation {
  public static void main(String[] args) {
    WeatherData weatherData = new WeatherData();
    CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay();
    StatisticsDisplay statisticsDisplay = new StatisticsDisplay();
    weatherData.registerObserver(currentDisplay);
    weatherData.registerObserver(statisticsDisplay);
    weatherData.setMeasurements(80, 65, 30.4f);
    weatherData.setMeasurements(82, 70, 29.2f);
  }
}

  
  

评论区