Return to Snippet

Revision: 5017
at February 5, 2008 16:59 by narkisr


Initial Code
// The mediator class which connects all the colleges (buttons in this case)
// and updates them all in reaction to informations from any of them 
package design.patterns.mediator
public class Mediator {
    def show
    def buttons = []

    void registerButton(b) {
        buttons << b
    }
    void registerDisplay(d) {
        show = d
    }
    void mediate(disable, text) {
        buttons.each {
            if (it == disable) {
                it.enabled - false
            }
            else {
                it.enabled = true
            }
        }
        show.setText(text)
    }
}

// The demo UI which demonstrates the usage
package design.patterns.mediator.ui
import groovy.swing.SwingBuilder
import design.patterns.mediator.Mediator

public class MediatorDemo {

    public static void main(String[] args) {
        def Mediator mediator = new Mediator()
        def frame = new SwingBuilder().frame() {
            panel() {
                label(text: 'just starting') {mediator.registerDisplay(current)}
                button() {
                    mediator.registerButton(current)
                    action(name: 'view', closure: {mediator.mediate(current, 'viewing ..')})
                }
                button() {
                    mediator.registerButton(current)
                    action(name: 'book', closure: {mediator.mediate(current, 'booking ..')})
                }

                button() {
                    mediator.registerButton(current)
                    action(name: 'search', closure: {mediator.mediate(current, 'searching ..')})
                }

            }
        }
        frame.setSize(400, 200);
        frame.setVisible(true);
    }

}

Initial URL

                                

Initial Description
Mediator design pattern implemented in Groovy (based upon http://www.javacamp.org/designPattern/).

Initial Title
Mediator Design pattern Groovy implementation

Initial Tags
design, groovy

Initial Language
Groovy