Design Pattern 18-intermediary pattern of behavioral pattern

Source: Internet
Author: User

Definition:MediatorPattern encapsulates a series of object interactions with an intermediary object. The intermediary makes the objects do not need to be explicitly referenced to each other, so that the coupling is loose and the interaction between them can be changed independently.

Type:Object behavior mode

Overview:

Object-Oriented Programming encourages behavior distribution to objects. These benefits enhance the reusability of objects, but the negative effect is that when multiple objects interact, the coupling will inevitably be greatly improved.

The intermediary model is proposed to solve the problem of improved coupling when multiple objects interact with each other. The intermediary mode abstracts interaction behaviors of different objects to achieve decoupling. In this way, each object only needs to be handed over to the intermediary without paying attention to too many objects.

For example, a project team has a project manager and several project developers. For example, if developers need to develop materials, they need to directly find the project manager and the project manager should coordinate the purchasers for procurement. Another example is that developers need to know a specific requirement, and they also need to directly find the project manager, and then the project manager will ask the sales staff for more detailed requirements. In short, developers can directly find the project manager for what they need, instead of caring about who they are looking for and how they can communicate with objects. In this way, developers will not interact with other objects, and there will be no great coupling. It is pure, just focus on their own development. In this way, people who pass the technical skills can act as developers without having to have any relationships or communication skills. This improves the reusability of developers.

Class diagram:


Participants: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPG9sIHR5cGU9 "1">

  • Mediator, which provides interfaces for interacting with Staff.
    1. ProjectManger implements specific interfaces for interaction with different employees.
      1. Staff, which provides basic interfaces for members to interact with Mediator.
      2. Developer, Buyer, and Seller complete their respective work, and then tell the specific project manager through the Meditor interface.

        Sample Code:

        Using System; using System. collections. generic; using System. text; namespace MediatorPattern {abstract class Mediator {public abstract void Comunicate (string message, Staff colleague);} class ProjectManger: Mediator {private Developer staff1; private Buyer staff2; private Seller staff3; public Developer Staff1 {set {staff1 = value ;}} public Buyer Staff2 {set {staff2 = value ;}} public Seller Staff3 {set {staff3 = value ;}} public override void Comunicate (string message, Staff staff) {staff. notify (message) ;}abstract class Staff {protected Mediator; public Staff (mediator Mediator) {this. mediator = mediator;} public abstract void y (string message);} class Developer: Staff {public Developer (Mediator mediator): base (mediator) {} public void Ask (string message) {mediator. comunicate (message, this);} public override void Policy (string message) {Console. writeLine ("Developer completed development task" + message);} class Buyer: Staff {public Buyer (Mediator mediator): base (mediator) {} public void Send (string message) {mediator. comunicate (message, this);} public override void Policy (string message) {Console. writeLine ("the purchaser knows" + message);} class Seller: Staff {public Seller (Mediator mediator): base (mediator) {} public void Tell (string message) {mediator. comunicate (message, this);} public override void Policy (string message) {Console. writeLine ("sales personnel already know" + message);} class Program {static void Main (string [] args) {ProjectManger mediator = new ProjectManger (); developer staff1 = new Developer (mediator); Buyer staff2 = new Buyer (mediator); Seller staff3 = new Seller (mediator); mediator. staff1 = staff1; mediator. staff2 = staff2; mediator. staff3 = staff3; staff1.Ask ("circuit board required"); staff2.Send ("purchased Circuit Board"); staff1.Ask ("customer requirement required "); staff3.Tell ("already under investigation ");}}}
        Applicability:
        1. A group of objects communicate in a well-defined but complex manner. The resulting dependency structure is confusing and hard to understand.
        2. An object references many other objects and communicates directly with these objects, making it difficult to reuse the object.
          1. I want to customize the behavior of a partition in multiple classes without generating too many subclasses.

            Advantages and disadvantages:

            1. Advantages: reduce the coupling between objects and improve the reusability of objects.
              1. Disadvantages: if there are too many interaction objects, the intermediary will be very complicated and difficult to manage.

                References:

                1. Design Patterns-reusable Object-Oriented Software basics
                2. Big talk Design Model

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.