Design Pattern 17-Mediator

xiaoxiao2021-03-06  55

Using system; using system.collections;

Namespace Pattern {///

/// summary description for class1. /// / * This mode definition is simple to encapsulate a series of object interactions very simple to use a mediation object. Intermediaries

Enable the objects that do not need to be referenced to each other, thereby coupling their coupling, and can be independently changed

The interaction between them. What is the benefit? It is obviously to reduce complexity. Make more

Multiple relationships. Now become 1-to-many relationship. Because all objects are only single lines with Mediator intermediaries

Department. The intermediary hermit includes the relationship between the object. The following is a simple example. Such as: Xiao Wang

To give the manuscript to Xiao Li. Xiao Li wants to give the book to Xiao Zhang. Xiao Zhang wants to give the pen to Xiao Wang. And Xiao Zhang wants

Have the portable to Xiao Li. This will be more complicated if the interaction between objects and objects. If they only

It is given the item to a diploma. It is responsible for the belongings by the intermediaries. Such everyone only and the intermediary

Establish a connection. The relationship between the intermediaries maintained the object. * /

/ / Start from the interface PUBLIC Interface IPerson {String Name {Get;} ArrayList Article {Get; Set;} // Multiple items may have multiple items}

Public class Wang: iPerson {private string name = "wang"; private arraylist article;

Public string name {get {return name;}}

Public arraylist article {get {raeturn articles;}} // ....... do other things}

Public class zhang: iPerson {private string name = "zhang"; private arraylist article;

Public string name {get {return name;}}

Public arraylist article {get {raeturn articles;}} // ....... do other things}

Public class lee: iperson {private string name = "lee"; private arraylist article;

Public string name {get {return name;}}

Public arraylist article {get {raeturn articles;}} // ....... do other things}

// Mediator public interface imediator {// Exchange item Void SWEAPArticle (IPerson P1, IPerson P2, Object At);}

Public class concretemediator: iMediator {public void SWEAPArticle (IPerson P1, IPerson P2, Object At) {p1.Article.Add (at); p2.Article.remove (at);}}

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

New Post(0)