Design mode Mediator - Unicom (original)

xiaoxiao2021-03-06  68

Design mode Mediator - Unicom

After dinner at noon, I (133 users) will go to the high-tech Unicom Hall with a colleague (130 users), Li Si (131 users) to Gaoxinliantonghall. In GaoXinliantonghall, we found that because there was only one staff in the lobby at noon, we had one to handle the payment business, first of all Zhang San (130 users), then Li Si (131 users) The last is me (133 users).

Ok, let's take a look at how this process is implemented:

1. Let's first define such an interface class such as liantonghall:

Public interface liantonghall {

Public void IdentifyUsertype (Liantonguser User, String Type); / / Determine User Type

Public void getusermoney (String type); // get the money for users

}

2. Define the specific implementation of the Unicom Hall (Liantonghall):

Public class concreteliantonghall imports Liantonghall {

Private USER130 USER130;

PRIVATE USER131 USER131;

Private USER133 USER133;

Public void IdentifyUsertype (Liantonguser User, String Type) {

IF (Type == "130") {

User130 = (user130) user; // 130 users

}

ELSE IF (Type == "131") {

User131 = (user131) user; // 131 users

}

Else IF (Type == "133") {

User133 = (user133) user; // 133 users

}

}

Public void getusermoney (String Type) {// Get the money of users

IF (Type == "130") {

User131.pleasewait (); // 131 users please wait

User133.pleasewait (); // 133 users please wait

}

ELSE IF (Type == "131") {

User130.pleasewait (); // 130 users please wait

User133.pleasewait (); // 133 users please wait

}

Else IF (Type == "133") {

User130.pleasewait (); // 130 users please wait

User131.pleasewait (); // 131 users please wait

}

}

}

3. Define Unicom users (LianTongUser) interface class:

Public interface liantonguser {

Public void handinmoney (); // pay

Public void pleasewait (); // Waiting

}

4. Define the implementation class of the Unicom user (LianTongUser) interface:

A: 130 Users (user130) public class user130 imports liantonguser {

PRIVATE FINAL STRING TYPE = "130";

Private Liantonghall Liantonghall;

Public USER130

THIS.LIANTONGHALL = liantonghall;

LianTonghall.IdentifyUsertype (this, type); // Unicom Hall judgment is the kind of user

}

Public void handinmoney () {

System.out.println ("130 users are paying!");

LianTongHall.getusermoney (Type); // Unicom Hall gets the money

}

Public void pleasewait () {

System.out.println ("130 users please wait for a while!");

}

}

B: 131 users (user131)

Public class user131 imports liantonguser {

PRIVATE FINAL STRING TYPE = "131";

Private Liantonghall Liantonghall;

Public User131 (LiantongHall Liantonghall) {

THIS.LIANTONGHALL = liantonghall;

LianTonghall.IdentifyUsertype (this, type); // Unicom Hall judgment is the kind of user

}

Public void handinmoney () {

System.out.println ("131 users are paying!");

LianTongHall.getusermoney (Type); // Unicom Hall gets the money

}

Public void pleasewait () {

System.out.println ("131 users please wait a while!");

}

}

C: 133 users (User133)

Public class user133 imports liantonguser {

PRIVATE FINAL STRING TYPE = "133";

Private Liantonghall Liantonghall;

Public user133 (LiantongHall Liantonghall) {

THIS.LIANTONGHALL = liantonghall;

LianTonghall.IdentifyUsertype (this, type); // Unicom Hall judgment is the kind of user

}

Public void handinmoney () {

System.out.println ("133 users are paying!");

LianTongHall.getusermoney (Type); // Unicom Hall gets the money

}

Public void pleasewait () {

System.out.println ("133 users please wait a while!");

}

}

5, write test classes:

Public class test {

Public static void main (String [] args) {

Liantonghall GaoXinliantonghall = New ConcreteliantongHall (); // High-tech Unicom Hall

User130 zhangsan = new user130 (gauxinliantonghall); // Zhang 3

User131 lisi = new user131 (gauxinliantonghall); // Li Si

User133 me = new user133 (gauxinliantonghall); // I

Zhangsan.handinmoney (); // Zhang three more money

Lisi.handinmoney (); // Li Si Division

Me.handinmoney (); // I pay

}

}

6. Description:

A: Mediator Definition: Use a mediation object to encapsulate a range of object interactions.

B: Each member must know the intermediary object and contact the intermediary object instead of contact with other members.

C: In this example, the intermediary object is equivalent to our Unicom Hall, and we have a relationship with the Unicom Hall, Zhang San, Li Si and I have no relationship.

转载请注明原文地址:https://www.9cbs.com/read-105972.html

New Post(0)