正在加载图片...
$18.4 APPLICATIONS OF ONCE ROUTINES 649 created by the first.Although the creation will never be executed again,nothing prevents callers from modifying the object through the reference.Therefore the mechanism provides shared objects rather than constant ones. An example of a shared object,cited at the beginning of this chapter,is a window showing error messages in an interactive system.Assume we have decided that any component of the system that detects a user error may output a message to that window, through a call of the form Message_window.put_text ("Appropriate error message") Here message window is of type WINDOW,with class WINDOW declared as class WINDOW creation make feature make (...is --Create window at size and position indicated by arguments. do...end text:STRING --Text to be displayed in window put text (s:STRING)is --Make s the text to be displayed in window. do text=s end .Other features.… end--class WINDOW Obviously Message window must be the same for all components of the system This is achieved by declaring the corresponding feature as a once function: Message window:WINDOW is --Window where error messages will be output once !Result.make ("...Size and position arguments...") end In this case the message window object must be shared by all its users,but it is not a constant object:each call to pur text changes the object by putting its own chosen text in it.The best place to declare Message window is a class from which all system components needing access to the message window will inherit. In the case of a shared object that denotes a constant,such as i,you may want to disallow calls of the form i.some_procedure that might change the fields.To achieve this,simply include clauses i.x=0 and i.y=/in the class invariant.§18.4 APPLICATIONS OF ONCE ROUTINES 649 created by the first. Although the creation will never be executed again, nothing prevents callers from modifying the object through the reference. Therefore the mechanism provides shared objects rather than constant ones. An example of a shared object, cited at the beginning of this chapter, is a window showing error messages in an interactive system. Assume we have decided that any component of the system that detects a user error may output a message to that window, through a call of the form Message_window ● put_text ("Appropriate error message") Here message_window is of type WINDOW, with class WINDOW declared as class WINDOW creation make feature make (…) is -- Create window at size and position indicated by arguments. do … end text: STRING -- Text to be displayed in window put_text (s: STRING) is -- Make s the text to be displayed in window. do text := s end … Other features … end -- class WINDOW Obviously Message_window must be the same for all components of the system. This is achieved by declaring the corresponding feature as a once function: Message_window: WINDOW is -- Window where error messages will be output once !! Result ● make ("…Size and position arguments…") end In this case the message window object must be shared by all its users, but it is not a constant object: each call to put_text changes the object by putting its own chosen text in it. The best place to declare Message_window is a class from which all system components needing access to the message window will inherit. In the case of a shared object that denotes a constant, such as i, you may want to disallow calls of the form i ● some_procedure that might change the fields. To achieve this, simply include clauses i ● x = 0 and i ● y = 1 in the class invariant
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有