标签:使用 http请求 data ati ffline tco drive 应用程序 分拆
控制器是视图的行为。假设你希望有不同的行为。能够直接换一个控制器。
JSP的唯一工作就是产生页面表现模型的视图,以及进一步动作所须要的全部控件。
//MVC //模型 public interface BeatModelInterface { //这些方法让控制器调用。控制器依据用户的操作对模型做出适当的处理 void initialize(); void on(); void off(); void setBPM(int bpm); //这些方法同意视图和控制器取得状态,而且成为观察者。 int getBPM(); void registerObserver(BeatObserver o); void removeObserver(BeatObserver o); void registerObserver(BPMObserver o); void removeObserver(BPMObserver o); } public class BeatModel implements BeatModelInterface, MetaEventListener { Sequencer sequencer; //两种观察者。一种观察节拍,一种观察BPM变化 ArrayList<BeatObserver> beatObservers = new ArrayList<BeatObserver>(); ArrayList<BPMObserver> bpmObservers = new ArrayList<BPMObserver>(); int bpm = 90; Sequence sequence; Track track; public void initialize() { setUpMidi(); buildTrackAndStart(); } public void on() { sequencer.start(); setBPM(90); } public void off() { setBPM(0); sequencer.stop(); } public void setBPM(int bpm) { this.bpm = bpm; sequencer.setTempoInBPM(getBPM()); notifyBPMObservers(); } public int getBPM() { return bpm; } void beatEvent() { notifyBeatObservers(); } //注冊观察者、通知观察者的代码 //处理节拍的MIDI代码 } //视图 public class DJView implements ActionListener, BeatObserver, BPMObserver { //视图持有模型和控制器的引用 BeatModelInterface model; ControllerInterface controller; //显示控制button JFrame viewFrame; JPanel viewPanel; BeatBar beatBar; JLabel bpmOutputLabel; JFrame controlFrame; JPanel controlPanel; JLabel bpmLabel; JTextField bpmTextField; JButton setBPMButton; JButton increaseBPMButton; JButton decreaseBPMButton; JMenuBar menuBar; JMenu menu; JMenuItem startMenuItem; JMenuItem stopMenuItem; public DJView(ControllerInterface controller, BeatModelInterface model) { this.controller = controller; this.model = model; model.registerObserver((BeatObserver) this); model.registerObserver((BPMObserver) this); } public void createView() { // Create all Swing components here } //模型发生变化是,updateBPM()方法会被调用 public void updateBPM() { if (model != null) { int bpm = model.getBPM(); if (bpm == 0) { if (bpmOutputLabel != null) { bpmOutputLabel.setText("offline"); } } else { if (bpmOutputLabel != null) { bpmOutputLabel.setText("Current BPM: " + model.getBPM()); } } } } public void updateBeat() { if (beatBar != null) { beatBar.setValue(100); } } } //控制器 public interface ControllerInterface { //视图能调用全部的控制器方法都在丽丽 void start(); void stop(); void increaseBPM(); void decreaseBPM(); void setBPM(int bpm); } //实现ControllerInterface接口 public class BeatController implements ControllerInterface { BeatModelInterface model; DJView view; public BeatController(BeatModelInterface model) { this.model = model; //把控制器当參数传入创建视图的构造器中 view = new DJView(this, model); view.createView(); view.createControls(); view.disableStopMenuItem(); view.enableStartMenuItem(); model.initialize(); } public void start() { model.on(); view.disableStartMenuItem(); view.enableStopMenuItem(); } public void stop() { model.off(); view.disableStopMenuItem(); view.enableStartMenuItem(); } public void increaseBPM() { int bpm = model.getBPM(); model.setBPM(bpm + 1); } public void decreaseBPM() { int bpm = model.getBPM(); model.setBPM(bpm - 1); } public void setBPM(int bpm) { model.setBPM(bpm); } } public class DJTestDrive { public static void main(String[] args) { BeatModelInterface model = new BeatModel(); //由控制器创建视图 ControllerInterface controller = new BeatController(model); } }
标签:使用 http请求 data ati ffline tco drive 应用程序 分拆
原文地址:http://www.cnblogs.com/blfbuaa/p/7101440.html