carfield.com.hkButton.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.mediator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
/**
* Basically a JButton with an ActionListener. The listener call
* <code>clicked()</code> when the button gets clicked.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
class Button extends JButton implements Colleague {
private Mediator mediator;
/**
* Creates a new <code>Button</code> object with the provided label.
*
* @param name the label for the new <code>Button</code> object
*/
public Button(String name) {
super(name);
this.setActionCommand(name);
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clicked();
}
});
}
public void clicked() {
mediator.colleagueChanged(this);
}
/**
* Implements the appropriate interface for colleage to set its
* Mediator
*
* @param mediator the new mediator
*/
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZColleague.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.mediator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements the driver for the mediator design pattern example.<p>
*
* Intent: <i>Define an object that encapsulates how a set of objects
* interact. Mediator promotes loose coupling by keeping objects from
* referring to each other explicitly, and it lets you vary their interaction
* independently.</i><p>
*
* Participatng objects are <code>Button</code>s as <i>Colleague</i>s,
* and a <code>Label</code> as <i>Mediator</i>.
*
* Every time an event of interest (a button click) occurs, the mediating
* <code>Label</code> is updated and it updates the calling button.
*
* <p><i>This is the Java version.</i><p>
*
* Both <i>Mediator</i> and <i>Colleagues</i> have to be aware of their role
* within the pattern.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Button
* @see Label
*/
public interface Colleague {
/**
* Defines the method signature for setting a Colleague's Mediator.
*
* @param mediator the new mediator
*/
public void setMediator(Mediator mediator);
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZLabel.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.mediator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import javax.swing.*;
/**
* Basically a JLabel. On object of this class is be the <i>Mediator</i>.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public class Label extends JLabel implements Mediator {
/**
* Creates a new <code>Label</code> object with the provided string.
*
* @param s the tag for the new <code>Label</code> object
*/
public Label(String s) {
super(s);
}
/**
* Implements the appropriate interface for Mediator to set label and
* button texts according to the experimental setup.
*
* @param colleague the colleague that caused the notification
*/
public void colleagueChanged(Colleague colleague) {
Button button = (Button) colleague;
if (button == Main.button1) {
this.setText("Button1 clicked");
} else if (button == Main.button2) {
this.setText("Button2 clicked");
}
button.setText("(Done)");
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZMain.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.mediator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import javax.swing.*;
import java.awt.event.*;
/**
* Implements the driver for the mediator design pattern example.<p>
*
* Intent: <i>Define an object that encapsulates how a set of objects
* interact. Mediator promotes loose coupling by keeping objects from
* referring to each other explicitly, and it lets you vary their interaction
* independently.</i><p>
*
* Participatng objects are <code>Button</code>s as <i>Colleague</i>s,
* and a <code>Label</code> as <i>Mediator</i>.
*
* Every time an event of interest (a button click) occurs, the mediating
* <code>Label</code> is updated and it updates the calling button.
*
* <p><i>This is the Java version.</i><p>
*
* Both <i>Mediator</i> and <i>Colleagues</i> have to be aware of their role
* within the pattern.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
* @see Button
* @see Label
*/
public class Main {
static JFrame frame = new JFrame("Mediator Demo");
static Button button1 = new Button("Button1");
static Button button2 = new Button("Button2");
static Label label = new Label ("Click a button!");
/**
* Implements the driver for the mediator example. It creates a small
* GUI with a label and two buttons. The buttons are <i>Colleague</i>s,
* the label is the <i>Mediator</i>.
*
* Each button click causes the mediator to update itself and the
* calling button.
*/
public static void main(String[] args) {;
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JPanel panel = new JPanel();
panel.add(label);
panel.add(button1);
panel.add(button2);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
button1.setMediator(label);
button2.setMediator(label);
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZMediator.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.mediator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Implements the driver for the mediator design pattern example.<p>
*
* Intent: <i>Define an object that encapsulates how a set of objects
* interact. Mediator promotes loose coupling by keeping objects from
* referring to each other explicitly, and it lets you vary their interaction
* independently.</i><p>
*
* Participatng objects are <code>Button</code>s as <i>Colleague</i>s,
* and a <code>Label</code> as <i>Mediator</i>.
*
* Every time an event of interest (a button click) occurs, the mediating
* <code>Label</code> is updated and it updates the calling button.
*
* <p><i>This is the Java version.</i><p>
*
* Both <i>Mediator</i> and <i>Colleagues</i> have to be aware of their role
* within the pattern.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*
*/
public interface Mediator {
/**
* Defines the method signature for notifying mediators of changes to
* colleagues. This method is called by colleagues who pass themselves
* as an argument (push).
*
* @param mediator the new mediator
*/
public void colleagueChanged(Colleague colleague);
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZMediatorImplementation.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.mediator.aspectj;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import ca.ubc.cs.spl.pattern.library.MediatorProtocol;
/**
* Concretizes the mediation relationship for <code>Button</code> (colleague)
* and <code>Label</code> (mediator). button clicks trigger updates.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 05/13/02
*/
public aspect MediatorImplementation extends MediatorProtocol {
/**
* Assings the <code>Colleague</code> role to the <code>Button</code>
* class. Roles are modeled as (empty) interfaces.
*/
declare parents: Button implements Colleague;
/**
* Assings the <code>Colleague</code> role to the <code>Label</code>
* class. Roles are modeled as (empty) interfaces.
*/
declare parents: Label implements Mediator;
/**
* Defines what changes on Colleagues cause their mediator to be notified
* (here: Button clicks)
*
* @param cs the colleague on which the change occured
*/
protected pointcut change(Colleague c):
(call(void Button.clicked()) && target(c));
/**
* Defines how the <code>Mediator</code> is to be updated when a change
* to a <code>Colleague</code> occurs. Here, the label's text is set
* depending on which button was clicked. The appropriate button's label
* is also updated.
*
* @param c the colleague on which a change of interest occured
* @param m the mediator to be notifed of the change
*/
protected void notifyMediator(Colleague c, Mediator m) {
Button button = (Button) c;
Label label = (Label) m;
if (button == Main.button1) {
label.setText("Button1 clicked");
} else if (button == Main.button2) {
label.setText("Button2 clicked");
}
button.setText("(Done)");
}
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Zfiles.lst2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Z