滨城区亿耀图文设计中心

图文设计/网站设计/UI设计/前端设计

UML图与设计模式:软件设计的可视化指南

UML(统一建模语言)图是软件工程用于可视化、指定、构造文档系统架构工具设计模式解决软件设计常见问题的可重用方案。UML图可以用来表示这些设计模式,帮助开发理解和实现这些模式。以下是几种常见的设计模式及其在UML图中的表示:

1. 单例模式(Singleton Pattern)

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

UML图与设计模式:软件设计的可视化指南

UML图表示:

  • 类图:Singleton类有一个私有静态成员变量instance和一个私有构造函数。它还提供一个公共静态方法getInstance()获取实例。

案例

public class Singleton {
    private static Singleton instance;

    private Singleton() {}

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

2. 工厂模式(Factory Pattern)

工厂模式定义了一个创建对象的接口,但让子类决定实例化哪个类。

UML图表示:

  • 类图:有一个抽象Product和具体类ConcreteProductFactory类有一个方法createProduct(),返回Product类型

案例:

public abstract class Product {
    public abstract void use();
}

public class ConcreteProduct extends Product {
    public void use() {
        System.out.println("Using ConcreteProduct");
    }
}

public class Factory {
    public Product createProduct() {
        return new ConcreteProduct();
    }
}

3. 观察者模式(Observer Pattern)

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

UML图表示:

  • 类图Subject类有一个Observer列表,并提供attach()detach()notifyObservers()方法。Observer类有一个update()方法。

案例:

import java.util.ArrayList;
import java.util.List;

public interface Observer {
    void update(String message);
}

public class Subject {
    private List<Observer> observers = new ArrayList<>();

    public void attach(Observer observer) {
        observers.add(observer);
    }

    public void notifyObservers(String message) {
        for (Observer observer : observers) {
            observer.update(message);
        }
    }
}

public class ConcreteObserver implements Observer {
    public void update(String message) {
        System.out.println("Received message: " + message);
    }
}

4. 策略模式(Strategy Pattern)

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

UML图表示:

  • 类图Strategy接口定义了一个execute()方法。ConcreteStrategyAConcreteStrategyB实现了Strategy接口。Context类有一个Strategy成员变量,并提供setStrategy()executeStrategy()方法。

案例:

public interface Strategy {
    void execute();
}

public class ConcreteStrategyA implements Strategy {
    public void execute() {
        System.out.println("Executing Strategy A");
    }
}

public class ConcreteStrategyB implements Strategy {
    public void execute() {
        System.out.println("Executing Strategy B");
    }
}

public class Context {
    private Strategy strategy;

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }

    public void executeStrategy() {
        strategy.execute();
    }
}

5. 适配器模式(Adapter Pattern)

适配器模式将一个类的接口转换客户端期望的另一个接口。

UML图表示:

  • 类图Target接口定义了客户端期望的方法。Adaptee类有一个不兼容的方法。Adapter类实现了Target接口,并在内部使用Adaptee

案例:

public interface Target {
    void request();
}

public class Adaptee {
    public void specificRequest() {
        System.out.println("Adaptee's specific request");
    }
}

public class Adapter implements Target {
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    public void request() {
        adaptee.specificRequest();
    }
}

6. 装饰器模式(Decorator Pattern)

装饰器模式动态地给对象添加职责

UML图表示:

  • 类图Component接口定义了基本操作。ConcreteComponent实现了Component接口。Decorator类实现了Component接口,并持有一个Component引用。ConcreteDecorator扩展了Decorator,并添加了额外的行为。

案例:

public interface Component {
    void operation();
}

public class ConcreteComponent implements Component {
    public void operation() {
        System.out.println("ConcreteComponent operation");
    }
}

public abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    public void operation() {
        component.operation();
    }
}

public class ConcreteDecorator extends Decorator {
    public ConcreteDecorator(Component component) {
        super(component);
    }

    public void operation() {
        super.operation();
        additionalOperation();
    }

    private void additionalOperation() {
        System.out.println("Additional operation");
    }
}

7. 代理模式(Proxy Pattern)

代理模式为其他对象提供一个代理以控制对这个对象的访问。

UML图表示:

  • 类图Subject接口定义了request()方法。RealSubject实现了Subject接口。Proxy类实现了Subject接口,并持有一个RealSubject引用。

案例:

public interface Subject {
    void request();
}

public class RealSubject implements Subject {
    public void request() {
        System.out.println("RealSubject handling request");
    }
}

public class Proxy implements Subject {
    private RealSubject realSubject;

    public Proxy(RealSubject realSubject) {
        this.realSubject = realSubject;
    }

    public void request() {
        System.out.println("Proxy handling request");
        realSubject.request();
    }
}

8. 命令模式(Command Pattern)

命令模式将请求封装为对象,从而使你可以用不同的请求、队列或者日志来参数化其他对象。

UML图表示:

  • 类图Command接口定义了execute()方法。ConcreteCommand实现了Command接口,并持有一个Receiver引用。Invoker类持有一个Command引用,并调用execute()方法。

案例:

public interface Command {
    void execute();
}

public class ConcreteCommand implements Command {
    private Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    public void execute() {
        receiver.action();
    }
}

public class Receiver {
    public void action() {
        System.out.println("Receiver performing action");
    }
}

public class Invoker {
    private Command command;

    public void setCommand(Command command) {
        this.command = command;
    }

    public void executeCommand() {
        command.execute();
    }
}

9. 外观模式(Facade Pattern)

外观模式为子系统中的一组接口提供一个一致的界面,从而简化子系统的使用。

UML图表示:

  • 类图Facade类封装了多个子系统的接口,并提供了一个简化的接口。

案例:

public class SubsystemA {
    public void operationA() {
        System.out.println("SubsystemA operation");
    }
}

public class SubsystemB {
    public void operationB() {
        System.out.println("SubsystemB operation");
    }
}

public class Facade {
    private SubsystemA subsystemA;
    private SubsystemB subsystemB;

    public Facade() {
        subsystemA = new SubsystemA();
        subsystemB = new SubsystemB();
    }

    public void operation() {
        subsystemA.operationA();
        subsystemB.operationB();
    }
}

10. 模板方法模式(Template Method Pattern)

模板方法模式定义了一个操作中的算法的骨架,而将一些步骤延迟到子类中。

UML图表示:

  • 类图AbstractClass定义了一个模板方法templateMethod(),其中包含多个抽象方法primitiveOperation1()primitiveOperation2()ConcreteClass实现了这些抽象方法。

案例:

public abstract class AbstractClass {
    public void templateMethod() {
        primitiveOperation1();
        primitiveOperation2();
    }

    protected abstract void primitiveOperation1();
    protected abstract void primitiveOperation2();
}

public class ConcreteClass extends AbstractClass {
    protected void primitiveOperation1() {
        System.out.println("ConcreteClass operation 1");
    }

    protected void primitiveOperation2() {
        System.out.println("ConcreteClass operation 2");
    }
}

总结

UML图是理解和实现设计模式的重要工具。通过UML图,开发者可以清晰地看到类之间的关系、接口的定义以及方法的调用流程。每个设计模式都有其特定的UML表示方式,帮助开发者更好地理解和应用这些模式。

Powered By 滨城区亿耀图文设计中心

Copyright Your WebSite.Some Rights Reserved. 鲁ICP备2023008258号