Docsity

Mediator Pattern: Object Interactions at MSU CSE 335, Spring 2006, Study notes of Computer Science

Partial preview of the text

Download Mediator Pattern: Object Interactions at MSU CSE 335, Spring 2006 and more Study notes Computer Science in PDF only on Docsity!

Software Design

CSE 335, Spring 2006

Object-oriented programming: The Mediator Pattern

Stephen Wagner

Michigan State University

A Simple Example

• Three objects : a scrollbar, a left button, and a right button

• Clicking the buttons moves the scroll bar

• Each button should only be active if the scrollbar can be moved in the appropriate

direction

X

X 

X

X

• Assume that buttons and scrollbars can have multiple listeners.

• How to implement?

Synchronization Collaboration

• What happens when we enter a new mask?

Synchronization Collaboration

• What happens when we enter a new mask?

delete:Button copy:Button

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

deactivate()

masks:TextBox chdir:Button

announceNewValue() update()

files:ListBox

deactivate()

deactivate()

Synchronization Collaboration

• What happens when we change directory?

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

deactivate()

buttonPressed(”Chdir”)

deactivate()

update()

getValue()

chdir:Button masks:TextBox files:ListBox copy:Button delete:Button

Problem

• Assembly from reusable collaborations can lead to tight coupling between

application objects

• Disadvantages:

– The adaptor classes will be overly complicated and not reusable

– Complicates configuration

– Distributes “invariant logic” among a large class of entities

– Complicates collaboration synthesis due to large number of related

collaborations (update cycles)

Mediated vs. Distributed Collaboration

ListBox

TextBox Button

Mediator

Button

Button

ListBox

TextBox Button

Button

Button

XX

P

P

P

P

P

P

P

P

P

P

P

P

P

P

P

P

P

P

P

Mediated vs. Distributed Collaboration

• The Mediator class is designed to solve a specific problem

– It will be tightly coupled with all of the other objects in the collaboration

– It will generally not be reusable

• The other objects will be unadapted and reusable

• In the distributed design

– There will be a large number of adaptor classes

– None of these classes will be reusable

Mediator Synthesizes Listener Roles

class Director : public ButtonListener,

public TextboxListener,

public ListBoxListener

public:

Director();

void buttonPressed(const string &);

void notify(const string &);

void entryAction(const string &);

private:

Button *copy;

Button *delete;

ListBox *fileList;

TextBox *fileMasks; }

The Mediator Class

class Director : public ButtonListener,

public TextboxListener,

public ListBoxListener

• The Mediator class implements a number of interfaces

• The Mediator class is not an adaptor class

– it does not adapt an existing clas

• The Mediator class will contain private members

– It needs to have pointers to all the objects in the collaboration

– It could have additional members

– It could also have additional member functions

Mediator Localizes Configuration Code

Director::Director()

ok = new Button();

cancel = new Button();

fontList = new ListBox();

fontName = new TextBox();

ok.registerListener(this);

cancel.registerListener(this);

fontList.registerListener(this);

fontName.registerListener(this);

• Why do objects not register with the mediator?

What’s Wrong Here?

Director::Director()

Button ok;

Button cancel;

ListBox fontList;

TextBox fontName;

ok.registerListener(this);

cancel.registerListener(this);

fontList.registerListener(this);

fontName.registerListener(this);