Intermediary mode analysis, structure diagram and basic code, mediation structure diagram code

Source: Internet
Author: User

Intermediary mode analysis, structure diagram and basic code, mediation structure diagram code
Zookeeper

Definition: a mediation object is used to encapsulate a series of object interactions. 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.
Applicability: The intermediary mode is generally used in scenarios where a group of objects communicate in a well-defined but complex manner and when you want to customize a behavior distributed in multiple classes, and do not want to generate too many child classes.
Advantage: the emergence of intermediaries reduces the coupling of various objects, so that various object classes and intermediary classes can be changed and reused independently. For example, changes in any country will not affect other countries, this is just a change with the Security Council. Secondly, because we abstract the object writing, we use mediation as an independent concept and encapsulate it in an object, in this way, the objects of interest will be transferred from the actions of the objects themselves to the interactions between them, that is, to look at the system from a macro perspective. For example, the Palestinian-Israeli conflict can only be regarded as a conflict between countries. Therefore, their views may be narrow, but from the perspective of the United Nations Security Council, we can look at this issue from a global and objective perspective and make contributions in mediation and peacekeeping.
Disadvantage: as the intermediary controls centralization, the interaction complexity becomes the complexity of the intermediary, which makes the intermediary more complex than any object.
Structure:


Basic code:

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace intermediary Mode
{
Class Program
{
Static void Main (string [] args)
{
ConcreteMediator m = new ConcreteMediator ();

ConcreteColleague1 c1 = new ConcreteColleague1 (m );
ConcreteColleague2 c2 = new ConcreteColleague2 (m );

M. Colleague1 = c1;
M. Colleague2 = c2;

C1.Send ("have you eaten? ");
C2.Send ("no, are you going to treat me? ");

Console. Read ();
}
}

Abstract class Mediator
{
Public abstract void Send (string message, Colleague colleague );
}

Class ConcreteMediator: Mediator
{
Private ConcreteColleague1 colleague1;
Private ConcreteColleague2 colleague2;

Public ConcreteColleague1 Colleague1
{
Set {colleague1 = value ;}
}

Public ConcreteColleague2 Colleague2
{
Set {colleague2 = value ;}
}

Public override void Send (string message, Colleague colleague)
{
If (colleague = colleague1)
{
Colleague2.Notify (message );
}
Else
{
Colleague1.Notify (message );
}
}
}

Abstract class Colleague
{
Protected Mediator mediator;

Public Colleague (Mediator mediator)
{
This. mediator = mediator;
}
}

Class ConcreteColleague1: Colleague
{
Public ConcreteColleague1 (Mediator mediator)
: Base (mediator)
{

}

Public void Send (string message)
{
Mediator. Send (message, this );
}

Public void y (string message)
{
Console. WriteLine ("colleague 1 get information:" + message );
}
}

Class ConcreteColleague2: Colleague
{
Public ConcreteColleague2 (Mediator mediator)
: Base (mediator)
{
}

Public void Send (string message)
{
Mediator. Send (message, this );
}

Public void y (string message)
{
Console. WriteLine ("colleague 2 get information:" + message );
}
}
}


How to deal with the differences between the intermediary mode and the facade Mode)

In-depth discussion on the differences between the intermediary mode and the facade mode (appearance Mode)
------ Solution --------------------------------------------------------
The intermediary mode is to allow two classes to have a direct relationship, but to communicate with the intermediary. the intermediary is not biased towards either party,
The relationship between the two parties and the other party through the intermediary is bidirectional.
The facade mode also makes the two classes do not have a direct relationship, but the facade tends to be one side, and the other side uses the facade and one side
A link occurs, but one party may not even have a relationship with the other party through the facade, that is, the relationship tends to be unidirectional.
------ Solution --------------------------------------------------------
Differences between the intermediary mode and the facade mode:
Let me make an analogy.
Intermediary mode: Your computer CPU, graphics card power, and you, then you need to use the computer and a motherboard to complete these parts
The main board used uses the intermediary mode.
Facade mode: a class a and a class B, you create A CLASS C, the entity created in this CLASS C
An entity with class a and class B is an entity with A higher layer to operate on A and B.
Be careful.
------ Solution --------------------------------------------------------
1. The facade mode is a structural mode, while the intermediary mode is a behavior mode.
2. The facade mode provides unified interfaces for subsystems. The intermediary mode uses an intermediary object to encapsulate a series of colleagues.
Interaction of objects.
3. The facade mode protocol is unidirectional, and the intermediary mode protocol is bidirectional.
4. In facade mode, all request processing is delegated to the subsystem, while in intermediary mode, the center coordinates the colleague class and
The core itself completes the business together.
------ Solution --------------------------------------------------------
The intermediary mode is used when the parties are inconvenient or unable to directly contact each other.

How can we optimize and analyze the website code structure?

This is a conceptual problem. I cannot understand a word in a word. code optimization is often used to dynamically convert to static, and JS code optimization is implemented, there are many tag attributes, image attributes, and text links.

Website code optimization starts with the website architecture and has a clear hierarchy. The main categories are internal connections, external connections, and code simplification, such as CSS architecture, it is recommended that you first search for some basic information about the website. We hope you will succeed!

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.