java与模式,Java编程中的模式应用

原创
ithorizon 5个月前 (12-09) 阅读数 7 #综合运维

Java与设计模式

Java是一种面向对象的编程语言,设计模式是解决特定问题的通用解决方案,Java和设计模式相辅相成,可以提高代码的可读性、可维护性和可扩展性,以下是Java中常用的几种设计模式及其应用场景。

1、单例模式(Singleton Pattern)

单例模式确保一个类只有一个实例,并提供一个全局访问点,数据库连接池通常使用单例模式,以避免创建过多的数据库连接,在Java中,可以使用以下代码实现单例模式:

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

注意:使用双重检查锁定(double-checked locking)可以提高单例模式的性能。

2、工厂模式(Factory Pattern)

工厂模式定义了一个创建对象的接口,让子类决定实例化哪一个类,工厂模式使一个类的实例化延迟到其子类,以下代码展示了一个简单的工厂模式实现:

   public interface Shape {
       void draw();
   }
   
   public class Circle implements Shape {
       public void draw() {
           System.out.println("Inside Circle::draw() method.");
       }
   }
   
   public class ShapeFactory {
       public Shape getShape(String shapeType){
           if(shapeType == null){
               return null;
           }     
           if(shapeType.equalsIgnoreCase("CIRCLE")){
               return new Circle();
           }
           return null;
       }
   }

3、观察者模式(Observer Pattern)

观察者模式定义了对象之间的一对多依赖关系,当一个对象状态发生改变时,所有依赖于它的对象都会得到通知并自动更新,以下代码展示了一个简单的观察者模式实现:

   public interface Observer {
       void update(String message);
   }
   
   public class ConcreteObserver implements Observer {
       public void update(String message) {
           System.out.println("ConcreteObserver: " + message);
       }
   }
   
   public class Subject {
       private List<Observer> observers = new ArrayList<>();
       public void attach(Observer observer) {
           observers.add(observer);
       }
       public void detach(Observer observer) {
           observers.remove(observer);
       }
       public void notifyObservers() {
           for (Observer observer : observers) {
               observer.update("Subject state changed");
           }
       }
   }

注意:在实际应用中,可以使用Java内置的ObserverObservable类来实现观察者模式。

4、装饰器模式(Decorator Pattern)

装饰器模式允许向一个现有的对象添加新的功能,同时又不改变其结构,这种设计模式创建了一个装饰类,用来包装原有的类,并在保持原类方法签名的同时,提供了额外的功能,以下代码展示了一个简单的装饰器模式实现:

   public interface Component {
       void operation();
   }
   
   public class ConcreteComponent implements Component {
       public void operation() {
           System.out.println("ConcreteComponent");
       }
   }
   
   public abstract class Decorator implements Component {
       protected Component component;
       public Decorator(Component component) {
           this.component = component;
       }
       public void operation() {
           component.operation();
       }
   }
   
   public class ConcreteDecoratorA extends Decorator {
       public ConcreteDecoratorA(Component component) {
           super(component);
       }
       public void operation() {
           super.operation();
           addedBehavior();
       }
       public void addedBehavior() {
           System.out.println("ConcreteDecoratorA");
       }
   }

注意:装饰器模式可以动态地给对象添加功能,而不需要修改原有代码。

5、策略模式(Strategy Pattern)

策略模式定义了一系列算法,并将每个算法封装起来,使它们可以互换使用,策略模式让算法的变化独立于使用算法的客户,以下代码展示了一个简单的策略模式实现:

   public interface Strategy {
       int doOperation(int num1, int num2);
   }
   
   public class OperationAdd implements Strategy{
       public int doOperation(int num1, int num2) {
           return num1 + num2;
       }
   }
   
   public class OperationSubtract implements Strategy{
       public int doOperation(int num1, int num2) {
           return num1 - num2;
       }
   }
   
   public class Context {
       private Strategy strategy;
       public Context(Strategy strategy) {
           this.strategy = strategy;
       }
       public int executeStrategy(int num1, int num2) {
           return strategy.doOperation(num1, num2);
       }
   }

注意:策略模式可以轻松替换算法,而不需要修改使用算法的代码。

Java和设计模式是相辅相成的,掌握常用的设计模式,可以帮助我们写出更加优雅、可读和可维护的代码。

文章标签: java与模式


热门