21xrx.com
2024-11-22 10:12:40 Friday
登录
文章检索 我的文章 写文章
Java设计模式课后答案大全-更好的学习方式
2023-06-12 20:35:36 深夜i     --     --
Java设计模式 习题 答案

设计模式在Java语言中的应用是非常普遍的,它们是一种被广泛使用的程序设计思想。学习设计模式对于提高Java编程技巧具有非常大的帮助,但是,对于初学者来说,设计模式的理解和应用非常困难。为了更好地帮助大家理解Java设计模式,提高学生的掌握程度,本文重新整理了设计模式的课后习题解答案。

以下是一些Java设计模式课后习题的答案:

1.策略模式

'''

public interface TravelStrategy {

  public void travelWay();

}

//公共的子类

public abstract class AbstractTravelStrategy implements TravelStrategy {

  public void travelWay() {

    System.out.println("旅游路线:");

    getWay();

  }

   public abstract void getWay();

}

//具体策略A

public class TrainTravelStrategy extends AbstractTravelStrategy {

  public void getWay() {

    System.out.println("坐火车去旅游");

  }

}

//具体策略B

public class PlaneTravelStrategy extends AbstractTravelStrategy {

  public void getWay() {

    System.out.println("坐飞机去旅游");

  }

}

//具体策略C

public class CarTravelStrategy extends AbstractTravelStrategy {

  public void getWay() {

    System.out.println("开车去旅游");

  }

}

public class TravelContext {

  private TravelStrategy strategy;

  public TravelContext(TravelStrategy strategy)

    this.strategy = strategy;

  public void setStrategy(TravelStrategy strategy)

    this.strategy = strategy;

  public void executeStrategy() {

    strategy.travelWay();

  }

}

public class Main {

  public static void main(String[] args) {

    TravelContext context = new TravelContext(new TrainTravelStrategy());

    context.executeStrategy();

    context.setStrategy(new PlaneTravelStrategy());

    context.executeStrategy();

    context.setStrategy(new CarTravelStrategy());

    context.executeStrategy();

  }

}

'''

2.单例模式

class Singleton {

  //在静态初始化器中创建单例实例,这段代码保证线程安全

  private static Singleton uniqueInstance = new Singleton();

  //Singleton 类只有一个构造方法,并且被声明为 private,防止其它类使用 new Singleton() 创建对象

  private Singleton() {}

  //用getInstance()方法实例化该类,并且只能被该方法调用一次。该方法可以通过多线程并发访问,因此需要同步化处理(Synchronized)。

  public static synchronized Singleton getInstance()

    return uniqueInstance;

}

'''

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 ArrayList 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) {

    int i = observers.indexOf(o);

    if (i >= 0) {

      observers.remove(i);

    }

  }

  public void notifyObservers() {

    for (int i = 0; i < observers.size(); i++) {

      Observer observer = (Observer) observers.get(i);

      observer.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);

  }

  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");

  }

}

public class Main {

  public static void main(String[] args) {

    WeatherData weatherData = new WeatherData();

    CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData);

    weatherData.setMeasurements(80, 65, 30.4f);

    weatherData.setMeasurements(82, 70, 29.2f);

    weatherData.setMeasurements(78, 90, 29.2f);

  }

}

'''

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复