新闻中心

2026最新版 Java面向对象设计模式完整教程,新手必看

栏目:软件教程 日期: 作者:admin 阅读:4

本文面向Java开发者,系统讲解面向对象的设计模式及应用方法。内容涵盖设计模式基础、创建型模式(单例、工厂)、结构型模式(装饰器、代理)、行为型模式(观察者、策略)、实际案例应用以及最佳实践。通过实例讲解和分步操作,帮助读者快速掌握Java设计模式,提高代码复用性、可维护性和系统可扩展性。

正文教程

一、设计模式基础

  • 定义:设计模式是前人总结的代码结构和解决方案,帮助提高可维护性和复用性。

  • 分类:创建型、结构型、行为型。


二、创建型模式

  1. 单例模式(Singleton)

public class Singleton {
   private static Singleton instance;

   private Singleton() {} // 私有构造函数

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

  • 技巧:保证类全局只有一个实例,线程安全可使用synchronized

  1. 工厂模式(Factory)

interface Shape { void draw(); }

class Circle implements Shape { public void draw(){ System.out.println("Circle"); } }
class Square implements Shape { public void draw(){ System.out.println("Square"); } }

class ShapeFactory {
   public static Shape getShape(String type) {
       switch(type) {
           case "circle": return new Circle();
           case "square": return new Square();
           default: return null;
       }
   }
}

Shape s = ShapeFactory.getShape("circle");
s.draw();

  • 技巧:工厂模式封装对象创建,减少耦合


三、结构型模式

  1. 装饰器模式(Decorator)

interface Coffee { double cost(); }
class SimpleCoffee implements Coffee { public double cost(){ return 5; } }

class MilkDecorator implements Coffee {
   private Coffee coffee;
   public MilkDecorator(Coffee c){ this.coffee = c; }
   public double cost(){ return coffee.cost() + 2; }
}

Coffee coffee = new MilkDecorator(new SimpleCoffee());
System.out.println(coffee.cost());

  • 技巧:动态添加功能,无需修改原类

  1. 代理模式(Proxy)

interface Image { void display(); }
class RealImage implements Image {
   private String fileName;
   public RealImage(String fileName){ this.fileName=fileName; }
   public void display(){ System.out.println("Displaying " + fileName); }
}

class ProxyImage implements Image {
   private RealImage realImage;
   private String fileName;
   public ProxyImage(String fileName){ this.fileName = fileName; }
   public void display(){
       if(realImage==null) realImage = new RealImage(fileName);
       realImage.display();
   }
}

Image img = new ProxyImage("photo.jpg");
img.display();

  • 技巧:代理控制对象访问,可用于延迟加载或权限控制


四、行为型模式

  1. 观察者模式(Observer)

import java.util.*;

interface Observer { void update(String message); }
class User implements Observer {
   private String name;
   public User(String name){ this.name=name; }
   public void update(String message){ System.out.println(name + " received: " + message); }
}

class Subject {
   private List<Observer> observers = new ArrayList<>();
   public void attach(Observer o){ observers.add(o); }
   public void notifyAllObservers(String msg){ for(Observer o:observers) o.update(msg); }
}

Subject subject = new Subject();
subject.attach(new User("Tom"));
subject.attach(new User("Alice"));
subject.notifyAllObservers("Hello!");

  • 技巧:观察者模式适合事件通知或发布订阅系统

  1. 策略模式(Strategy)

interface Strategy { int execute(int a, int b); }
class Add implements Strategy { public int execute(int a,int b){ return a+b; } }
class Multiply implements Strategy { public int execute(int a,int b){ return a*b; } }

class Context {
   private Strategy strategy;
   public Context(Strategy strategy){ this.strategy = strategy; }
   public int executeStrategy(int a,int b){ return strategy.execute(a,b); }
}

Context ctx = new Context(new Add());
System.out.println(ctx.executeStrategy(5,3));

  • 技巧:策略模式可在运行时灵活切换算法


五、实用技巧总结

  1. 优先理解设计模式分类和适用场景

  2. 单例和工厂模式常用于对象管理

  3. 装饰器和代理增强功能,避免修改原类

  4. 观察者和策略模式处理事件和算法灵活切换

  5. 将设计模式结合项目实践,提高代码复用性和可维护性

相关资讯