1、Lecture 12. System Design: Restaurant Example,Object Oriented Technology and System Modeling面向对象技术与系统建模,Instructor: Yushan Sun Spring Term 2013,面向对象分析、设计的主要步骤以及产生的主要文件,Database,显示层,应 用 层,Presentation,1,Restaurant,getBookings() getCustomer() getTable() makeReservation(),date,BookingSystem,Booking,cov
2、ers date time,getDate() getDetails() setArrivalTime() setTable(),Table,number places,WalkIn,Reservation,arrivalTime,setArrivalTime(),*,*,*,1,current,01,selected,Must be one of the Current bookings,*,*,*,1,Cancel() display() makeReservation() recordArrival() selectBooking() undateDisplay(),Bookings f
3、or the same table must not overlap,Customer,name phoneNumber,永久数据存储层,订餐系统系统的分析模型-分层情况,6-RESTAURANT SYSTEM-DESIGN,Chapter 5已经完成了应用层用例实现。We have finished application-layer use case realizations in Chapter 5. 本章任务是将建模应用到整个系统(包括其他层)Now we extend this level of modeling to the whole system.,本章研究输入, 输出, 永久
4、数据存储 Now, some basic strategies for handling Input, output and persistent storage will be discussed从分析得到的类图需要加细。 The analysis class diagram will be refined to contain fuller information about data types, message parameters and so on.,6-RESTAURANT SYSTEM-DESIGN,Presentation,Application,Storage,6-REST
5、AURANT SYSTEM-DESIGN,在设计结束时,我们对系统有一个比较完全的了解,系统实现可以开始. By the end of the design stage, we aim to have an understanding of the system that is detailed enough to permit implementation to begin.,6-RESTAURANT SYSTEM-DESIGN,Some assumptions 餐馆定单系统是桌面单用户. The restaurant booking system will be implemented a
6、s a single-user desktop application (在局域网下,稍加修改,可以是多用户的). Java应用,基于Windows的用户界面. We will design it as a Java application with a window-based user interface. 用关系型数据库存储永久数据. A relational database will be used to store permanent data about bookings, customers and so on.,内容提要:Receiving Input From The Us
7、er (Mark Priestley: 6.1) Producing Output (Mark Priestley: 6.2) Observer Pattern publisher-subscriber example Eaxmples of Observable/Observer,Receiving Input From The User 从用户接收输入,6.1 RECEIVING INPUT FROM THE USER,评论:在分析阶段,用户输入端系统消息被直接发送到应用层的BookingSystem, 这不合适。 In system analysis, system messages f
8、rom the user were directed to a control object BookingSystem, which is an application layer object, This is not proper.,We should use an object in the presentation layer to receive user input and forward requests on to the control object. The object in the presentation layer that receives user input
9、 can reasonably be characterized as a boundary object.,6.1 RECEIVING INPUT FROM THE USER,正确的做法: 应该利用表现层接收用户输入,并且经由显示层将 请求转送到控制对象,Fig 6.1 collaboration diagram to handle a system message,6.1 RECEIVING INPUT FROM THE USER,:StaffUI,presentation,:BookingSystem,Application,Display(date),staff,submit(date
10、),显示层,应用层,The StaffUI boundary object receives this message, and then delegates the responsibility for dealing with it to the control object in the application layer.,About Display bookings use case the user first selects an appropriate menu option, which causes a dialogue box to appear the user ent
11、ers the required date The user clicks an OK button to submit the request to the system.,6.1 RECEIVING INPUT FROM THE USER,menu,dialogue box,date,用户与系统的交互产生的事件必须由系统处理The users interact with the system by using a GUI, and so events generated needs to be handled by the system.事件将被用户界面捕捉,并且发送到定单系统 The e
12、vents would be detected by the user interface, and passed on to the booking system as a getBooking message See Fig 6.2.,6.1 RECEIVING INPUT FROM THE USER,Fig 6.2. select a booking,mouseDown(pos),getBookings(time, table),getDetails(),6.1 RECEIVING INPUT FROM THE USER,: BookingSystem,应用层,Pos:屏幕上的鼠标点击处
13、座标,In the message getBookings(time, table) the position is replaced by a table number and a time which enables the booking system to identify a unique booking.,应用层与显示层的关系 应用层不应该知道StaffUI的任何细节。The application layer should not know anything about the details of the interface StaffUI翻译鼠标坐标到对于应用有用的信息。St
14、affUI translates mouse coordinates into information that is meaningful to the application. StaffUI 还负责产生输出。StaffUI is also for producing the displayed output, by mapping the table numbers and times of the bookings to the relevant screen co-ordinates.,6.1 RECEIVING INPUT FROM THE USER,Move a booking
15、from one table to another can be done by dragging the booking rectangle from one location on the screen to another. Corresponding user-generated events: mouse down mouse move mouse up,6.1 RECEIVING INPUT FROM THE USER,调换桌位(拖拽方式),b1,b2,b4,b5,b3,b6,b7,t11,t12,t13,t14,t15,t21,t22,t23,t24,t25,t31,t32,t3
16、3,t34,t35,t41,t42,t43,t44,t45,b8,b9,t51,t52,t53,t54,t55,6.1 RECEIVING INPUT FROM THE USER,Fig 6.3. transferring a booking to a new table,mouseDown(pos),getBookings(time, table),mouseMove(pos),mouseUp(pos),transfer(time, table),6.1 RECEIVING INPUT FROM THE USER,只有mouse up时,消息才最后被送到booking system,边界对象
17、必须记忆交互的当前状态 Boundary object must remember the current state of the interaction with the user and when to send a system message.,6.1 RECEIVING INPUT FROM THE USER,问题:怎样记录当前状态?,回答:使用一个变量记录状态 例如:state,6.1 RECEIVING INPUT FROM THE USER,Return,PRODUCING OUTPUT 产生输出,6.2 PRODUCING OUTPUT,The StaffUI class
18、has two distinct roles: 1)将用户信息发送到控制类. It receives messages from the user and forwards them to the controller class, 2)将系统返回的数据显示给用户. It also plays the role of a view class: Display the output from the system.,输出机制:每当应用数据状态改变时,数据在屏幕上的显示也应该被相应地被更新. Whenever the state of the application data changes,
19、the presentation of this data on the screen is updated so that what the user sees is consistent with the system state. 视图与模型通知改变The view is informed of changes in the model.,6.2 PRODUCING OUTPUT,实现界面更新的几种方式 Option 1: Use polling technique (意见调查) StaffUI makes periodic enquiries of the application cl
20、asses, to find out if anything has changed (传统的方法). Polling requires classes in the presentation layer to make calls on classes in the application layer 例如,在StaffUI类中写一个方法,每隔3秒钟调用BookingSystem的某个方法,检查数据是否改变了。,6.2 PRODUCING OUTPUT,浪费处理时间. It is expensive in terms of wasted processing time. 空转、作了无用功.
21、the system state will not change from one enquiry to another, a lot of enquiries is for nothing 数据量大时候,每检查一遍,太费劲. If there is a significant amount of data in the model, it can be quite a laborious task to examine it to see if anything has changed,6.2 PRODUCING OUTPUT,缺点,Option 2: Use Observer Patter
22、n (观察者模式) The application classes inform the view classes whenever anything has changed, so that updates to the view only take place when required.,6.2 PRODUCING OUTPUT,问题:应用层的类怎样通知显示层?,现在的情况适合使用观察者模式 The present situation contains elements of both these conditions: Change in an application object c
23、hange in the view object, and We want the application object to notify the view object of this change,6.2 PRODUCING OUTPUT,Return,The Observer Pattern 观察者模式,3. Observer Pattern,DESCRIPTION The Observer pattern is useful for designing a consistent communication model between a set of dependent object
24、s and an object that they are dependent onThis allows the dependent objects to have their state synchronized with the object that they are dependent on.,观察者 (Observers) : the set of dependent objects 主题 (Subject): object that Observers are dependent on In order to accomplish this, the Observer patte
25、rn suggests a publisher-subscriber model leading to a clear boundary between the set of Observer objects, and the Subject object.,3. Observer Pattern,3. Observer Pattern,publisher-subscriber model (出版/订阅模型), Observable,register(obs: Observer) unRegister(obs: Observer) notifyObservers(), Observer,syn
26、cronizeState(),ConcreteObserver,syncronizeState(),Subject,getState(),1*,1*,1*,1*,被观察者,观察者,3. Observer Pattern,观察者对象依赖于主题对象的状态 A typical observer is an object with interest or dependency in the state of the subject. A subject can have more than one such observer. Each of these observers needs to know
27、 when the subject undergoes a change in its state.,The subject needs to maintain a data structure such as a vector to hold dynamically added observers An observer needs to register itself as an observer with the subject by calling the register method of class Subject. Whenever the subjects state has
28、 changed, it notifies all of its registered observers by using notifyObservers. Upon receiving the notification from the subject, each of the observers queries the subject object to synchronize its state with that of the subjects. Then according to the new state, the observer decides to do something
29、.,3. Observer Pattern,Publisher-subscriber策略工作机制,3. Observer Pattern,notifyObservers(),观察者1,观察者2,观察者3,被观察者,Return,观察者1,观察者2,观察者3,Publisher-subscriber Example,4. publisher-subscriber example,Example: There are two groups of animals, tiger group and antelope group, both watching the grass state of a g
30、reat plain. When the grass becomes green, both antelope and tiger group will come, and When the grass becomes yellow, both antelope and tiger group will leave.In such case, we can use observer pattern Encapsulate the great plain as a class that implements interface Observable, and encapsulate the Ti
31、ger and Antelope as two classes that implement interface Observer,4. publisher-subscriber example, Observable,PlainGUI,notifyObservers() register(obs: Observer) unRegister(obs:Observer),notifyObservers() register(obs: Observer) unRegister(obs:Observer), Observer,TigerGUI,AntelopeGUI,takeAction( s: O
32、bservable),takeAction(s Observable),takeAction(s Observable),notifies,1 1*,1 1*,1 1*,One observer will register itself to the observable notifyObservers() will notify the observer that the state of the Object PlainGUI has changed, and then the TigerGUI and AntelopeGUI object will do something by tak
33、eAction(s Observable) in which, if necessary, will in turn call observable to check the state change.,4. publisher-subscriber example,Initial graphical interface for observable and observers,4. publisher-subscriber example,被观察者选择绿色,则观察者的页面也变成绿色,4. publisher-subscriber example,被观察者选择黄色,则观察者的页面也变成黄色,4
34、. publisher-subscriber example,public interface Observable public void notifyObservers();public void register(Observer obs);public void unRegister(Observer obs); ,4. publisher-subscriber example,public class PlainGUI extends JFrame implements Observablepublic PlainGUI() throws Exception observersLis
35、t = new Vector();/ public void register(Observer obs) observersList.addElement(obs);public void unRegister(Observer obs)observersList.removeElement(obs);public void notifyObservers() for (int i = 0; i observersList.size(); i+) Observer observer = (Observer) observersList.elementAt(i);observer.takeAc
36、tion(this); ,4. publisher-subscriber example,class ButtonHandler implements ActionListener public void actionPerformed(ActionEvent e) if (e.getActionCommand().equals(SUBMIT) String state = (String)stateCmBox.getSelectedItem();setGrassState(state);notifyObservers(); / end of class,4. publisher-subscr
37、iber example,public interface Observer public void takeAction(Observable subject); ,4. publisher-subscriber example,public class AntelopeGUI extends JFrame implements Observerpublic AntelopeGUI(PlainGUI objPlainGUI) throws ExceptionplainGuiObj = objPlainGUI;plainGuiObj.register(this); /自己给自己注册public
38、 void takeAction(Observable subject) if (subject = plainGuiObj) grassState = plainGuiObj.getGrassState().trim();if (grassSpareTo(“Green“)= 0 ) actionText.setBackground(Color.green);actionText.setText(“Antelopes entering the plain.“);else if (grassSpareTo(“Yellow“)= 0 )actionText.setBackground(Color.
39、yellow);actionText.setText(“Antelopes leaving the plain.“); ,4. publisher-subscriber example,public class TigerGUI extends JFrame implements Observerpublic TigerGUI(PlainGUI objPlainGUI) throws Exception plainGuiObj = objPlainGUI;plainGuiObj.register(this);public void takeAction(Observable subject)
40、if (subject = plainGuiObj)grassState = plainGuiObj.getGrassState().trim();if (grassSpareTo(“Green“)= 0 )actionText.setBackground(Color.green);actionText.setText(“Tigers entering the plain.“);else if (grassSpareTo(“Yellow“)= 0 )actionText.setBackground(Color.yellow);actionText.setText(“Tigers leaving
41、 the plain.“); ,4. publisher-subscriber example,/自己给自己注册,public class TestObserverObservable public static void main(String args) /create an observable objectPlainGUI plainObj= new PlainGUI();/Create Observer objectsnew TigerGUI ( plainObj );new AntelopeGUI( plainObj ); ,4. publisher-subscriber exam
42、ple,Return,Observable-observer Example,Java supports Observanle/Observer strategy for event handling Observable class Observer interface,5. Eaxmples of Observable/Observer,Observable,addObserver(observer:Observer) setChanged() notifyObservers(event: Object), Observer,update(o: Observable, e: Object)
43、,Question: How to realize Observable/Observer strategy?,Observable,ConcreteObservable,addObserver(observer:Observer) setChanged() notifyObservers(event: Object), Observer,ConcreteObserver1,ConcreteObserver2,update(o: Observable, e: Object),addObserver notifyObservers,1 1*,1 1*,1 1*,update(o: Observa
44、ble, e: Object),update(o: Observable, e: Object),J2SDK1.4,J2SDK1.4,5. Eaxmples of Observable/Observer,Observable/Observer strategy,Java中已经实现的类,Java中的接口,方法update的名字不能改变。注意:两个参数,5. Eaxmples of Observable/Observer,Design of the plain/Animal problem using the Observable/Observer strategy,Observable,Plai
45、nGUI,addObserver(observer:Observer) setChanged() notifyObservers(event: Object),TigerGUI,AntelopeGUI,addObserver notifyObservers,1 1*,1 1*,1 1*,update(o: Observable, e: Object),update(o: Observable, e: Object),Java中已经实现的类,Java中的接口,方法update的名字不能改变。注意:两个参数,关于方法update update(o: Observable, e: Object) 第
46、一个参数o代表被观察者对象,例如PlainGUI对象 第二个参数e代表观察者关心的对象,例如封装在 String对象中的草原颜色,5. Eaxmples of Observable/Observer,Mechanism for Observable/Observer strategy,添加观察者、注册. An observer can be added to an observable object can add (通常是一个应用程序或者observer对象) 被观察者通知/观察者更新. setChanged() and notifyObservers() will notify the o
47、bserver that the state of the Object PlainGUI has changed, and then the update method in TigerGUI and AntelopeGUI objects will be automatically invoked to do something In the update method, in turn, observable can be called to check the state change.,Observable/observer策略工作机制,Initial graphical inter
48、face for observable and observers,4. publisher-subscriber example,被观察者选择绿色,则观察者的页面也变成绿色,4. publisher-subscriber example,被观察者选择黄色,则观察者的页面也变成黄色,4. publisher-subscriber example,public class PlainGUI extends Observable public PlainGUI() throws ExceptionobserversList = new Vector();public void setGrassState(String st)grassState = st;/一些其它的代码class ButtonHandler implements ActionListenerpublic void actionPerformed(ActionEvent e)if (e.getActionCommand().equals(SUBMIT)String state = (String)stateCmBox.getSelectedItem();setGrassState(state);setChanged(); notifyObservers(grassState); /这两行代码必须同时出现 ,