Java设计模式

设计模式基础概念

设计模式是解决软件设计中常见问题的可复用方案,分为创建型、结构型和行为型三大类。理解这些模式能提升代码的可维护性和扩展性。

创建型模式

单例模式
确保一个类只有一个实例,并提供全局访问点。

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

工厂模式
通过工厂类创建对象,隐藏具体实现细节。

public interface Product {
    void use();
}
public class ConcreteProduct implements Product {
    @Override
    public void use() {
        System.out.println("Using ConcreteProduct");
    }
}
public class ProductFactory {
    public Product createProduct() {
        return new ConcreteProduct();
    }
}

结构型模式

适配器模式
使不兼容的接口能够协同工作。

public class LegacySystem {
    public void legacyRequest() {
        System.out.println("Legacy request");
    }
}
public interface Target {
    void request();
}
public class Adapter implements Target {
    private LegacySystem legacySystem;
    public Adapter(LegacySystem legacySystem) {
        this.legacySystem = legacySystem;
    }
    @Override
    public void request() {
        legacySystem.legacyRequest();
    }
}

装饰器模式
动态地为对象添加功能。

public interface Component {
    void operation();
}
public class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("Basic operation");
    }
}
public class Decorator implements Component {
    private Component component;
    public Decorator(Component component) {
        this.component = component;
    }
    @Override
    public void operation() {
        component.operation();
        System.out.println("Added behavior");
    }
}

行为型模式

观察者模式
定义对象间的一对多依赖关系,当一个对象状态改变时,所有依赖者都会收到通知。

import java.util.ArrayList;
import java.util.List;
public interface Observer {
    void update(String message);
}
public class ConcreteObserver implements Observer {
    @Override
    public void update(String message) {
        System.out.println("Received: " + message);
    }
}
public class Subject {
    private List<Observer> observers = new ArrayList<>();
    public void addObserver(Observer observer) {
        observers.add(observer);
    }
    public void notifyObservers(String message) {
        for (Observer observer : observers) {
            observer.update(message);
        }
    }
}

策略模式
定义一系列算法,将每个算法封装起来并使它们可以互相替换。

public interface Strategy {
    int execute(int a, int b);
}
public class AddStrategy implements Strategy {
    @Override
    public int execute(int a, int b) {
        return a + b;
    }
}
public class Context {
    private Strategy strategy;
    public Context(Strategy strategy) {
        this.strategy = strategy;
    }
    public int executeStrategy(int a, int b) {
        return strategy.execute(a, b);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值