设计模式以及stopWatch使用
1. 单例模式 (Singleton Pattern)
确保一个类只有一个实例,并提供全局访问点。
public class Singleton {
private static volatile Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}
2. 工厂模式 (Factory Pattern)
定义一个创建对象的接口,让子类决定实例化哪个类。
// 产品接口
public interface Product {
void use();
}
// 具体产品
public class ConcreteProductA implements Product {
@Override
public void use() {
System.out.println("Using Product A");
}
}
public class ConcreteProductB implements Product {
@Override
public void use() {
System.out.println("Using Product B");
}
}
// 工厂类
public class ProductFactory {
public static Product createProduct(String type) {
if ("A".equals(type)) {
return new ConcreteProductA();
} else if ("B".equals(type)) {
return new ConcreteProductB();
}
return null;
}
}
3. 观察者模式 (Observer Pattern)
定义对象间的一对多依赖关系,当一个对象改变状态时,所有依赖它的对象都会收到通知。
import java.util.*;
// 观察者接口
public interface Observer {
void update(String message);
}
// 主题接口
public interface Subject {
void addObserver(Observer observer);
void removeObserver(Observer observer);
void notifyObservers(String message);
}
// 具体主题
public class ConcreteSubject implements Subject {
private List<Observer> observers = new ArrayList<>();
private String state;
@Override
public void addObserver(Observer observer) {
observers.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observers.remove(observer);
}
@Override
public void notifyObservers(String message) {
for (Observer observer : observers) {
observer.update(message);
}
}
public void setState(String state) {
this.state = state;
notifyObservers("State changed to: " + state);
}
}
// 具体观察者
public class ConcreteObserver implements Observer {
private String name;
public ConcreteObserver(String name) {
this.name = name;
}
@Override
public void update(String message) {
System.out.println(name + " received: " + message);
}
}
4. 策略模式 (Strategy Pattern)
定义一系列算法,把它们一个个封装起来,并且使它们可相互替换。
// 策略接口
public interface Strategy {
int doOperation(int num1, int num2);
}
// 具体策略实现
public class OperationAdd implements Strategy {
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}
public class OperationSubtract implements Strategy {
@Override
public int doOperation(int num1, int num2) {
return num1 - num2;
}
}
public class OperationMultiply implements Strategy {
@Override
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);
}
}
5. 装饰器模式 (Decorator Pattern)
动态地给一个对象添加一些额外的职责,就增加功能来说,装饰器模式相比生成子类更为灵活。
// 组件接口
public interface Component {
void operation();
}
// 具体组件
public class ConcreteComponent implements Component {
@Override
public void operation() {
System.out.println("Basic operation");
}
}
// 装饰器基类
public abstract class Decorator implements Component {
protected Component component;
public Decorator(Component component) {
this.component = component;
}
@Override
public void operation() {
component.operation();
}
}
// 具体装饰器
public class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addedBehavior();
}
private void addedBehavior() {
System.out.println("Added behavior A");
}
}
public class ConcreteDecoratorB extends Decorator {
public ConcreteDecoratorB(Component component) {
super(component);
}
@Override
public void operation() {
super.operation();
addedBehavior();
}
private void addedBehavior() {
System.out.println("Added behavior B");
}
}
使用示例
import org.springframework.util.StopWatch;
public class Main {
public static void main(String[] args) {
// 创建StopWatch实例
StopWatch stopWatch = new StopWatch("Performance Test");
// 开始第一个任务
stopWatch.start("Task 1");
Singleton singleton = Singleton.getInstance();
System.out.println(singleton);
stopWatch.stop();
// 开始第二个任务
stopWatch.start("Task 2");
ProductFactory factory = new ProductFactory();
Product a = factory.createProduct("A");
a.use();
stopWatch.stop();
// 开始第三个任务
stopWatch.start("Task 3");
ConcreteSubject subject = new ConcreteSubject();
ConcreteObserver observer = new ConcreteObserver("999");
ConcreteObserver observer1 = new ConcreteObserver("100");
subject.addObserver(observer);
subject.addObserver(observer1);
subject.setState("234234");
Component component = new ConcreteComponent();
component.operation();
Decorator decorator = new ConcreteDecoratorA(component);
decorator.operation();
Decorator decorator1 = new ConcreteDecoratorB(decorator);
decorator1.operation();
stopWatch.stop();
// 输出结果
System.out.println(stopWatch.prettyPrint());
System.out.println("Total time: " + stopWatch.getTotalTimeMillis() + "ms");
}
}
1.引入jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.21</version>
</dependency>
2.stopwatch实现
打印
Singleton@511d50c0
Using Product A
999 received: State changed to: 234234
100 received: State changed to: 234234
Basic operation
Basic operation
Added behavior A
Basic operation
Added behavior A
Added behavior B
StopWatch 'Performance Test': running time = 2928200 ns
---------------------------------------------
ns % Task name
---------------------------------------------
000349600 012% Task 1
000585800 020% Task 2
001992800 068% Task 3
Total time: 2ms

浙公网安备 33010602011771号