Chúng tôi xem xét khởi tạo trong Chương 3, nhưng tôi sẽ lặp lại các thông tin ở đây, cụ thể hơn là các bộ sưu cấu trúc địa phương để một phương pháp, loại hình của nó có thể được suy ra từ khởi tạo và việc kê khai var được cho phép, như trong: var cấu trúc mới | Quiz Match the Mediator Pattern Players with the Mailing List Illustration To test whether you understand the Mediator pattern cover the lefthand column of the table below and see if you can identify its players and methods among the items from the illustrative example Figure 9-5 as shown in the righthand column. Then check your answers against the lefthand column. Colleague Mediator Respond Callback Send Receive A user subscribed to a mailing list The mailing list moderator Calls back the signed-on users A means of contacting all users on the list Mailing a message to the list Receiving a message from the list The Mediator pattern makes provisions for more than one mediator. For example there may be many different mailing lists created under one mailing system. Each list may have a different moderator different rules of engagement and a different list of users but the structure of the lists is identical. Therefore creating a new Mediator is merely an instantiation operation and does not require subclassing or an interface. Implementation Key points of the Mediator pattern are Each Colleague is passed a Mediator at instantiation and keeps it as a private reference. Each Mediator keeps a list of signed-on Colleagues as a private reference. The callback to the colleagues is a very thin interface just a method. Therefore a dictionary list names with callbacks of Colleagues in the Mediator would be overkill we can just use the delegate mechanism to chain the Receive methods. On the other hand within one mediator system there could be Colleague types that implement the Receive method differently. This variation is shown in the theory code in Example 9-3. Example 9-3. Mediator pattern theory code 1 using System 2 using 3 4 class MediatorPattern 5 202 Chapter 9 Behavioral Patterns Iterator Mediator and Observer Example 9-3. Mediator pattern theory code continued 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 .