/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ /* das 2. Beispiel verbessert: * model und view kommunizieren ueber events * --> lose Kopplung * view kann ohne Aenderungen am model * ausgewechselt werden */ import java.applet.Applet; import java.awt.Button; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; //-------------------- public class ModelViewController2 extends Applet { Button control1, control2; Counter model; ViewLabel view; ViewColor view2; //-------------------- public void init() { //-------------------- // die MVC Bestandteile initialisieren control1 = new Button("+1"); control1.setBackground(Color.orange); control2 = new Button("-1"); control2.setBackground(Color.pink); view = new ViewLabel(); view2 = new ViewColor(); model = new Counter(); //-------------------- // die grafischen Objekte anordnen setLayout(new GridLayout(4,1)); add(control1); add(control2); add(view); add(view2); //-------------------- // die controller Ereignisse behandeln control1.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { model.incr(+1); } } ); control2.addActionListener (new ActionListener() { public void actionPerformed(ActionEvent e) { model.incr(-1); } } ); //-------------------- // view als event listener im model registrieren model.addCounterChangedListener(view); model.addCounterChangedListener(view2); // init event ausloesen model.incr(0); } }