1+ //-------------------------------------------------------------------------------------
2+ // MediatorExample2.cs
3+ //-------------------------------------------------------------------------------------
4+
5+ /*
6+ * It is used to handle communication between related objects(Colleagues)
7+ *
8+ * All communication is handled by the mediator
9+ * the colleagues don't need to know anything about each other
10+ *
11+ * GOF: Allows loose coupling by encapsulating the way disparate sets of objects
12+ * interact and communicate with each other.Allows for the actions
13+ * of each object set to vary independently of one another
14+ *
15+ *
16+ **/
17+
18+ using UnityEngine ;
19+ using System . Collections ;
20+ using System . Collections . Generic ;
21+
22+ namespace MediatorExample2
23+ {
24+
25+ public class MediatorExample2 : MonoBehaviour
26+ {
27+ void Start ( )
28+ {
29+ StockMediator nyse = new StockMediator ( ) ;
30+
31+ // important here is:
32+ // in this example they both might be doing nothing different, but
33+ // they could be totally different objects and calculate stuff different or so
34+ // but still be able to talk to the mediator the same easy way
35+ // that's why we have different objects here:
36+ GormanSlacks broker = new GormanSlacks ( nyse ) ;
37+ JTPoorman broker2 = new JTPoorman ( nyse ) ;
38+
39+ nyse . AddColleague ( broker ) ;
40+ nyse . AddColleague ( broker2 ) ;
41+
42+ // because they call methods on the same mediator object they talk to the same mediator
43+ // who handles all the stock exanche and keeps track of that. so the brokers by themselves
44+ // don't know anything about each other. which is a good thing :-)
45+ broker . SaleOffer ( Stock . MSFT , 100 ) ;
46+ broker . SaleOffer ( Stock . GOOG , 50 ) ;
47+
48+ broker2 . BuyOffer ( Stock . MSFT , 100 ) ;
49+ broker2 . SaleOffer ( Stock . NRG , 10 ) ;
50+
51+ broker . BuyOffer ( Stock . NRG , 10 ) ;
52+ broker . BuyOffer ( Stock . NRG , 50 ) ;
53+
54+ nyse . PrintStockOfferings ( ) ;
55+ }
56+
57+
58+ }
59+
60+ // I like using enums more than using strings
61+ // because it prevents typos and I don't need to remember strings ;)
62+ public enum Stock
63+ {
64+ MSFT ,
65+ GOOG ,
66+ NRG
67+ } ;
68+
69+
70+ public class StockOffer
71+ {
72+ public int stockShares { get ; private set ; }
73+ public Stock stock { get ; private set ; }
74+ public int colleagueCode { get ; private set ; }
75+
76+ public StockOffer ( int numOfShares , Stock stock , int collCode )
77+ {
78+ this . stockShares = numOfShares ;
79+ this . stock = stock ;
80+ this . colleagueCode = collCode ;
81+ }
82+ }
83+
84+
85+ public abstract class Colleague
86+ {
87+ private Mediator mediator ;
88+ private int colleagueCode ;
89+
90+ public Colleague ( Mediator mediator )
91+ {
92+ this . mediator = mediator ;
93+ }
94+
95+ public void SetCode ( int code )
96+ {
97+ colleagueCode = code ;
98+ }
99+
100+ public void SaleOffer ( Stock stock , int shares )
101+ {
102+ mediator . SaleOffer ( stock , shares , this . colleagueCode ) ;
103+ }
104+
105+ public void BuyOffer ( Stock stock , int shares )
106+ {
107+ mediator . BuyOffer ( stock , shares , this . colleagueCode ) ;
108+ }
109+ }
110+
111+
112+ public class GormanSlacks : Colleague
113+ {
114+ // using : base() like here calls the constructor of the base class with the arguments passed in
115+ // here it calls "Colleague(Mediator mediator)"
116+ public GormanSlacks ( Mediator mediator ) : base ( mediator )
117+ {
118+ Debug . Log ( "Gorman Slacks signed up with the stockexange" ) ;
119+ }
120+ }
121+
122+ public class JTPoorman : Colleague
123+ {
124+ public JTPoorman ( Mediator mediator ) : base ( mediator )
125+ {
126+ Debug . Log ( "JT Poorman signed up with the stockexange" ) ;
127+ }
128+ }
129+
130+
131+
132+
133+
134+
135+ public interface Mediator
136+ {
137+ void AddColleague ( Colleague colleague ) ;
138+ void SaleOffer ( Stock stock , int shares , int code ) ;
139+ void BuyOffer ( Stock stock , int shares , int code ) ;
140+ }
141+
142+
143+ public class StockMediator : Mediator
144+ {
145+ private List < Colleague > colleagues ;
146+ private List < StockOffer > buyOffers ;
147+ private List < StockOffer > sellOffers ;
148+
149+ private int colleagueCodes = 0 ;
150+
151+ public StockMediator ( )
152+ {
153+ colleagues = new List < Colleague > ( ) ;
154+ buyOffers = new List < StockOffer > ( ) ;
155+ sellOffers = new List < StockOffer > ( ) ;
156+ }
157+
158+ #region Mediator implementation
159+ public void AddColleague ( Colleague colleague )
160+ {
161+ this . colleagues . Add ( colleague ) ;
162+ colleagueCodes += 1 ;
163+ colleague . SetCode ( colleagueCodes ) ;
164+ }
165+
166+ public void SaleOffer ( Stock stock , int shares , int code )
167+ {
168+ bool stockSold = false ;
169+
170+ // see if someone is willing to buy:
171+ for ( int i = 0 ; i < buyOffers . Count ; i ++ )
172+ {
173+ StockOffer offer = buyOffers [ i ] ;
174+ // check if the stock is the same:
175+ if ( offer . stock == stock && offer . stockShares == shares )
176+ {
177+ Debug . Log ( shares + " shares of " + stock + " stocks sold to colleague with code " + code ) ;
178+
179+ buyOffers . Remove ( offer ) ;
180+ stockSold = true ;
181+ }
182+
183+ if ( stockSold ) break ;
184+ }
185+
186+ if ( ! stockSold )
187+ {
188+ Debug . Log ( shares + " shares of " + stock + " stocks added to inventory" ) ;
189+ StockOffer offer = new StockOffer ( shares , stock , code ) ;
190+ sellOffers . Add ( offer ) ;
191+ }
192+ }
193+
194+ public void BuyOffer ( Stock stock , int shares , int code )
195+ {
196+ bool stockBought = false ;
197+
198+ // see if someone is willing to buy:
199+ for ( int i = 0 ; i < sellOffers . Count ; i ++ )
200+ {
201+ StockOffer offer = sellOffers [ i ] ;
202+ // check if the stock is the same:
203+ if ( offer . stock == stock && offer . stockShares == shares )
204+ {
205+ Debug . Log ( shares + " shares of " + stock + " stocks bought by colleague with code " + code ) ;
206+
207+ sellOffers . Remove ( offer ) ;
208+ stockBought = true ;
209+ }
210+
211+ if ( stockBought ) break ;
212+ }
213+
214+ if ( ! stockBought )
215+ {
216+ Debug . Log ( shares + " shares of " + stock + " stocks added to inventory" ) ;
217+ StockOffer offer = new StockOffer ( shares , stock , code ) ;
218+ buyOffers . Add ( offer ) ;
219+ }
220+ }
221+ #endregion
222+
223+
224+ public void PrintStockOfferings ( )
225+ {
226+ Debug . Log ( "For Sale: " + sellOffers . Count ) ;
227+ foreach ( StockOffer offer in sellOffers )
228+ {
229+ Debug . Log ( offer . stock + " - " + offer . stockShares + " - " + offer . colleagueCode ) ;
230+ }
231+
232+
233+ Debug . Log ( "For Buy: " + buyOffers . Count ) ;
234+ foreach ( StockOffer offer in buyOffers )
235+ {
236+ Debug . Log ( offer . stock + " - " + offer . stockShares + " - " + offer . colleagueCode ) ;
237+ }
238+ }
239+ }
240+
241+ }
0 commit comments