设计模式 – 中介者模式(Mediator Pattern)

内容纲要

中介者模式(Mediator Pattern)是一种行为型设计模式,它用于降低多个对象之间通信的复杂性。通过引入一个中介者对象,各个对象之间不再直接通信,而是通过中介者对象进行间接通信。这样可以减少对象之间的依赖,降低耦合,使得对象更易于复用和维护。

让我们来看一个Java代码示例,以帮助你更好地理解中介者模式。

首先,我们创建一个Mediator接口,它定义了一个sendMessage方法,用于在Colleagues之间发送消息:

public interface Mediator {
    void sendMessage(String message, Colleague sender);
}

接下来,我们创建一个Colleague抽象类,它包含了一个Mediator对象的引用,并提供了一些通用方法:

public abstract class Colleague {
    protected Mediator mediator;

    public Colleague(Mediator mediator) {
        this.mediator = mediator;
    }

    public abstract void receiveMessage(String message);
    public void sendMessage(String message) {
        mediator.sendMessage(message, this);
    }
}

现在我们可以创建具体的Colleague类,例如DeveloperTester

public class Developer extends Colleague {
    public Developer(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void receiveMessage(String message) {
        System.out.println("Developer received: " + message);
    }
}

public class Tester extends Colleague {
    public Tester(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void receiveMessage(String message) {
        System.out.println("Tester received: " + message);
    }
}

接下来,我们创建一个具体的中介者类ProjectManager,实现Mediator接口:

public class ProjectManager implements Mediator {
    private Developer developer;
    private Tester tester;

    public void setDeveloper(Developer developer) {
        this.developer = developer;
    }

    public void setTester(Tester tester) {
        this.tester = tester;
    }

    @Override
    public void sendMessage(String message, Colleague sender) {
        if (sender instanceof Developer) {
            tester.receiveMessage(message);
        } else if (sender instanceof Tester) {
            developer.receiveMessage(message);
        }
    }
}

最后,我们创建一个测试类来演示中介者模式:

public class MediatorPatternDemo {
    public static void main(String[] args) {
        ProjectManager mediator = new ProjectManager();

        Developer developer = new Developer(mediator);
        Tester tester = new Tester(mediator);

        mediator.setDeveloper(developer);
        mediator.setTester(tester);

        developer.sendMessage("Code completed.");
        tester.sendMessage("Test completed.");
    }
}

运行这个示例,你将看到以下输出:

Tester received: Code completed.
Developer received: Test completed.

通过上述代码示例,你可以看到中介者模式是如何降低对象之间通信复杂性的。在这个例子中,DeveloperTester之间不再直接通信,而是通过ProjectManager进行间接通信。这有助于将系统模块化,使得各个模块之间的依赖更少,更易于维护和扩展。当我们需要添加新的Colleague类时,例如ProductManager,只需修改ProjectManager中介者类,而不需要修改其他Colleague类。


下面我们来创建一个ProductManager类:

public class ProductManager extends Colleague {
    public ProductManager(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void receiveMessage(String message) {
        System.out.println("Product Manager received: " + message);
    }
}

接着,我们需要在ProjectManager中介者类中添加ProductManager的引用,并在sendMessage方法中处理ProductManager的消息传递:

public class ProjectManager implements Mediator {
    private Developer developer;
    private Tester tester;
    private ProductManager productManager;

    public void setDeveloper(Developer developer) {
        this.developer = developer;
    }

    public void setTester(Tester tester) {
        this.tester = tester;
    }

    public void setProductManager(ProductManager productManager) {
        this.productManager = productManager;
    }

    @Override
    public void sendMessage(String message, Colleague sender) {
        if (sender instanceof Developer) {
            tester.receiveMessage(message);
            productManager.receiveMessage(message);
        } else if (sender instanceof Tester) {
            developer.receiveMessage(message);
            productManager.receiveMessage(message);
        } else if (sender instanceof ProductManager) {
            developer.receiveMessage(message);
            tester.receiveMessage(message);
        }
    }
}

现在,我们可以在测试类中添加ProductManager实例,并通过中介者模式进行通信:

public class MediatorPatternDemo {
    public static void main(String[] args) {
        ProjectManager mediator = new ProjectManager();

        Developer developer = new Developer(mediator);
        Tester tester = new Tester(mediator);
        ProductManager productManager = new ProductManager(mediator);

        mediator.setDeveloper(developer);
        mediator.setTester(tester);
        mediator.setProductManager(productManager);

        developer.sendMessage("Code completed.");
        tester.sendMessage("Test completed.");
        productManager.sendMessage("Product requirements updated.");
    }
}

运行这个示例,你将看到以下输出:

Tester received: Code completed.
Product Manager received: Code completed.
Developer received: Test completed.
Product Manager received: Test completed.
Developer received: Product requirements updated.
Tester received: Product requirements updated.

通过使用中介者模式,我们可以在不改变已有Colleague类的情况下,轻松地添加新的Colleague类并实现相应的通信。这种模式在处理复杂的交互系统时非常有用,因为它降低了系统的复杂性和耦合度。

Leave a Comment

您的电子邮箱地址不会被公开。 必填项已用*标注

close
arrow_upward