For those that are familiar with the Mediator pattern...
I want to implement the Mediator pattern in Delphi, but the Delphi compiler can't handle the circular references required.
Looking at the original GOF diagram from 'Design Patterns', the Mediator has a reference to each Colleague, but many of the Colleague objects have a reference back to the Mediator.
This is not a problem in most languages, but my Delphi compiler is giving me 'F2047 Circular unit reference to ...'
Would this approach, using interfaces, be any use? (seems complicated)
I am using Delphi 2010
[Summary of solution]
Just to summarise the accepted answer: In languages that allow circular references, you can omit the abstract Mediator class (as discussed in the "Implementation" section of GoF on page 278). The only way you can implement Mediator in Delphi without an abstract Mediator class is to have all your Classes in one Unit.
Otherwise, you need an extra abstract Mediator base class in addition to the concrete subclass.
Your Uses clauses for the three Units would look like this:
ConcreteColleage1 Uses Mediator
ConcreteMediator Uses Mediator, ConcreateColleague1
Mediator (Doesn't use either)
No circular references!
I don't see where the circular dependencies arise. If you implement your classes following this diagram, no such thing should happen.
To implement this diagram in Delphi, you will indeed need to write
Mediator
interface (and have yourConcreteMediator
class implement this interface)Mediator
class with virtual methods, (and have yourConcreteMediator
class derive fromMediator
and override these methods).