Download presentation
Presentation is loading. Please wait.
1
1 Colleague Colleague1Colleague2 Mediator mediator ConcreteMediator Mediator Design Pattern Intent –Define a class that encapsulates how a set of objects interact. –Control the interaction in a centralized (single) place –Promote loose-coupling among objects 1
2
2 OrderProcParticipant Faculty Mediator mediator OrderProcMediator 1 initOrderProc(f: OrderProcParticipant) getStatus(): OrderProcStatus OrderAccount orderInitiatororderaccount
3
3 OrderProcParticipant Faculty Mediator mediator OrderProcMediator 1 initOrderProc(f: OrderProcParticipant) initOrderProc(f: OrderProcParticipant, o: Order, a: Accout) getStatus(): OrderProcStatus OrderAccount orderInitiatororderaccount If( f’s type is not Faculty) throw an exception; orderInitiator = f; order = o; account = a; f is the owner of a? order.getItem().getCost() < account.balance()? status = OrderProcStatus.AUTHORIZED returns status; What if a more complicated business Process is implemented? Purchaser, supervisor, auditor, etc. etc.
4
4 IMParticipant PremiumUserRegularUser Mediator mediator ConfCallMediator 1 showMsg(msg: String) register(p: IMParticipant) sendMsg(msg: String) showAd(ad: Ad) pUsers rUsers register(p: IMParticipant) sendMsg(msg: String)
5
5 IMParticipant PremiumUserRegularUser Mediator mediator ConfCallMediator 1 showMsg(msg: String) register(p: IMParticipant) sendMsg(msg: String) showAd(ad: Ad) pUsers rUsers register(p: IMParticipant) sendMsg(msg: String) if( p’s type is PremiumUser ){ pUsers.add(p); p.log(); } if( p’s type is RegularUser) rUser.add(p); for each of pUsers { each.showMsg( msg ); } for each of rUsers { each.showAd( new Ad(…) ); each.showMsg( msg ); } The Strategy pattern used. What if new user types are added?
6
6Producer-Consumer Producer –A thread (or a group of threads) that generates data to be processed. Consumer –A thread (or a group of threads) that takes those data and process them. Data (variable/queue) Producers Consumer
7
7 Producer-consumer is used in the Future design pattern. –Future as a mediator between two threads One for producing a real pizza The other for waiting for and consuming a real pizza with a future object (a fake pizza)
8
8HW Read on a book chapter on the Mediator design pattern. Implement a producer-consumer program with a Fibonacci sequence generator –A producer generates a Fibonacci sequence with a given number, and put it to a channel. –A consumer takes the Fibonacci sequence from the channel. If it is not available yet (if it has not been generated by a producer), wait for it. Due: Nov 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.