Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Oberon (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Message handler=== This technique consists of replacing the set of methods with a single procedure, which discriminates among the various methods: '''MODULE''' Figures; ''<span style="color: gray">(* Abstract module *)</span>'' '''TYPE''' Figure* = '''POINTER TO''' FigureDesc; Message* = '''RECORD END'''; DrawMsg* = '''RECORD''' (Message) '''END'''; ClearMsg* = '''RECORD''' (Message) '''END'''; MarkMsg* = '''RECORD''' (Message) '''END'''; MoveMsg* = '''RECORD''' (Message) dx*, dy* : '''INTEGER''' '''END'''; Handler* = '''PROCEDURE''' (f : Figure; '''VAR''' msg : Message); FigureDesc* = '''RECORD''' ''<span style="color: gray">(* Abstract *)</span>'' handle : Handler; '''END'''; '''PROCEDURE''' Handle* (f : Figure; '''VAR''' msg : Message); '''BEGIN''' f.handle(f, msg) '''END''' Handle; '''PROCEDURE''' Init* (f : Figure; handle : Handler); '''BEGIN''' f.handle := handle '''END''' Init; '''END''' Figures. We extend the generic type Figure to a specific shape: '''MODULE''' Rectangles; '''IMPORT''' Figures; '''TYPE''' Rectangle* = '''POINTER TO''' RectangleDesc; RectangleDesc* = '''RECORD''' (Figures.FigureDesc) x, y, w, h : INTEGER; '''END'''; '''PROCEDURE''' Draw* (r : Rectangle); '''BEGIN''' ''<span style="color: gray">(* ... *)</span>'' '''END''' Draw; ''<span style="color: gray">(* Other procedures here *)</span>'' '''PROCEDURE''' Handle* (f: Figure; '''VAR''' msg: Figures.Message); '''VAR''' r : Rectangle; '''BEGIN''' r := f(Rectangle); '''IF''' msg '''IS''' Figures.DrawMsg '''THEN''' Draw(r) '''ELSIF''' msg '''IS''' Figures.MarkMsg '''THEN''' Mark(r) '''ELSIF''' msg '''IS''' Figures.MoveMsg '''THEN''' Move(r, msg(Figures.MoveMsg).dx, msg(Figures.MoveMsg).dy) '''ELSE''' <span style="color: gray">(* ignore *)</span> '''END''' '''END''' Handle; '''PROCEDURE''' New* ('''VAR''' r : Rectangle); '''BEGIN''' '''NEW'''(r); Figures.Init(r, Handle) '''END''' New; '''END''' Rectangles. In the Oberon operating system both of these techniques are used for dynamic dispatch. The first one is used for a known set of methods; the second is used for any new methods declared in the extension module. For example, if the extension module Rectangles were to implement a new Rotate() procedure, within the Figures module it could only be called via a message handler.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)