21xrx.com
2025-03-26 19:43:56 Wednesday
文章检索 我的文章 写文章
Java设计模式课后答案详解及代码实现
2023-06-16 12:45:28 深夜i     --     --
简单工厂模式 工厂方法模式 工厂接口

在学习Java设计模式时,练习并掌握设计模式的应用是很重要的。所以我们准备了课后答案以帮助您更好地理解和应用设计模式。本文将为大家详细解答Java设计模式中常见的问题,并提供相应的代码示例。

1. 工厂模式

问题1:简单工厂模式和工厂方法模式有何不同?

代码实现:

简单工厂模式:

interface Product {
  void show();
}
class ConcreteProduct1 implements Product {
  public void show() {
   System.out.println("ConcreteProduct1");
  }
}
class ConcreteProduct2 implements Product {
  public void show() {
   System.out.println("ConcreteProduct2");
  }
}
class Factory {
  public static Product create(String type) {
   if (type.equalsIgnoreCase("ConcreteProduct1")) {
     return new ConcreteProduct1();
   } else if (type.equalsIgnoreCase("ConcreteProduct2")) {
     return new ConcreteProduct2();
   }
   return null;
  }
}
public class SimpleFactoryPatternDemo {
  public static void main(String[] args) {
   Product product1 = Factory.create("ConcreteProduct1");
   Product product2 = Factory.create("ConcreteProduct2");
   product1.show();
   product2.show();
  }
}

工厂方法模式:

interface Product {
  void show();
}
class ConcreteProduct1 implements Product {
  public void show() {
   System.out.println("ConcreteProduct1");
  }
}
class ConcreteProduct2 implements Product {
  public void show() {
   System.out.println("ConcreteProduct2");
  }
}
interface Factory {
  Product create();
}
class ConcreteFactory1 implements Factory {
  public Product create() {
   return new ConcreteProduct1();
  }
}
class ConcreteFactory2 implements Factory {
  public Product create() {
   return new ConcreteProduct2();
  }
}
public class FactoryMethodPatternDemo {
  public static void main(String[] args) {
   Factory factory1 = new ConcreteFactory1();
   Factory factory2 = new ConcreteFactory2();
   Product product1 = factory1.create();
   Product product2 = factory2.create();
   product1.show();
   product2.show();
  }
}

2. 单例模式

问题2:如何保证线程安全的懒汉式单例模式?

代码实现:

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

关键词:懒汉式,线程安全,Singleton

3. 装饰者模式

问题3:装饰者模式的优点是什么?

代码实现:

interface Component {
  void operation();
}
class ConcreteComponent implements Component {
  public void operation() {
   System.out.println("ConcreteComponent");
  }
}
abstract class Decorator implements Component {
  Component component;
  public Decorator(Component component)
   this.component = component;
 
  public void operation() {
   component.operation();
  }
}
class ConcreteDecorator1 extends Decorator {
  public ConcreteDecorator1(Component component) {
   super(component);
  }
  public void operation() {
   super.operation();
   addedBehavior1();
  }
  void addedBehavior1() {
   System.out.println("ConcreteDecorator1");
  }
}
class ConcreteDecorator2 extends Decorator {
  public ConcreteDecorator2(Component component) {
   super(component);
  }
  public void operation() {
   super.operation();
   addedBehavior2();
  }
  void addedBehavior2() {
   System.out.println("ConcreteDecorator2");
  }
}
public class DecoratorPatternDemo {
  public static void main(String[] args) {
   Component component = new ConcreteComponent();
   component = new ConcreteDecorator1(component);
   component = new ConcreteDecorator2(component);
   component.operation();
  }
}

优点:可以动态地添加、删除或更改对象功能,不需要通过继承来实现,避免了继承带来的子类数量的爆炸性增长。

关键词:装饰者模式,动态添加功能,避免继承

总结

通过以上的代码实现示例及问题解答,相信大家对Java设计模式的常见问题有了更深入的理解和掌握。需要注意的是,实战应用将更加复杂和抽象,建议大家多多练习并不断总结,提升自己的设计模式应用能力。

  
  

评论区