220 likes | 231 Views
This exercise session focuses on inheritance, deferred classes, redefinition, renaming, the Observer pattern, and genericity in programming. It also explores the relation of Lego bricks and their features.
E N D
1 Introduction to Programming Bertrand Meyer Exercise Session 13 10. November 2008
This week Inheritance: Deferred classes & features Redefinition, renaming Observer Pattern Genericity Hint for Assignment
Let's play Lego! Inheritance Relation of Lego bricks BRICK LEGO_BRICK LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED
class BRICK deferred class BRICK feature-- Access width: INTEGER depth: INTEGER height: INTEGER color: COLOR volume: INTEGER deferred end end
Class LEGO_BRICK class LEGO_BRICK inherit BRICK feature-- Access number_of_nubs: INTEGER do Result:= ... end volume: INTEGER do Result := ... end end Inherit all features of class BRICK New feature, calculate all nubs Implementation of `volume'
Class LEGO_BRICK_SLANTED class LEGO_BRICK_SLANTED inherit LEGO_BRICK redefine volume end feature -- Access volume: INTEGER do Result:= ... end end Feature `volume' is going to be redefined (=changed). `volume' comes from LEGO_BRICK
Class LEGO_BRICK_WITH_HOLE class LEGO_BRICK_WITH_HOLE inherit LEGO_BRICK redefine volume end feature -- Access volume: INTEGER do Result:= ... end end Feature `volume' is going to be redefined (=changed). `volume' comes from LEGO_BRICK
Notation Notation: Deferred * Effective + Redefinition ++ * volume* BRICK + volume+ LEGO_BRICK volume++ volume++ + + LEGO_BRICK_WITH_HOLE LEGO_BRICK_SLANTED
Deferred & effective Deferred Deferred classes can possess deferred features A class with at least one deferred feature must be declared as deferred A deferred feature does not have an implementation yet Attributes can't be deferred (because they don't need a feature body anyways) Deferred classes cannot be instantiated and hence cannot contain a createclause Effective Effective classes do not possess deferred features (the “standard case”) Effective features have an implementation of their feature body (“do .. end”)
Precursor If a feature was redefined, but you still wish to call the old one, use the precursor keyword volume: INTEGER do Result:= Precursor - ... end
Types in the source code Expressions have a static type For attributes and functions it is denoted after the declaration ':' name: STRING list: LINKED_LIST [INTEGER] A class is the static part of a program, which contains only static types
Type of an object The type of an object is set upon creation local lego: LEGO_BRICK brick: BRICK create {LEGO_BRICK} brick-- explicit -- same as -- create lego; brick := lego createlego-- implicit Objects belong to the dynamic part of a program ->dynamic types
Dynamic types local a_bag: SOME_BAG do create {MONEY_BAG} a_bag.put (10) What is the static type of `a_bag'? What's the dynamic type of the object denoted by `a_bag'? Hands-On SOME_BAG MONEY_BAG
The Observer Pattern * * PUBLISHER OBSERVER update* subscribe subscribed attach detach trigger GUI_CLASS APP_CLASS update+ * Deferred (abstract) Inherits from + Effective (implemented) Client (uses)
Observer pattern Publisher keeps a list of observers: subscribed: LINKED_LIST [OBSERVER] To register itself, an observer may execute subscribe (some_publisher) where subscribe is defined in OBSERVER: subscribe (p: PUBLISHER) -- Make current object observep. require publisher_exists: p /= Void dop.attach (Current) end
Attaching an observer In class PUBLISHER: attach (s: OBSERVER) -- Registersas subscriber to current publisher. requiresubscriber_exists: s /= Voiddosubscribed.extend (s) end The invariant of PUBLISHER includes the clause subscribed /= Void (List subscribed is created by creation procedures of PUBLISHER)
Triggering an event trigger -- Ask all observers to -- react to current event. dofromsubscribed.startuntilsubscribed.afterloopsubscribed.item. subscribed.forthendend Each descendant of OBSERVER defines its own versionof update * * update* PUBLISHER OBSERVER attach detach GUI_CLASS APP_CLASS update+ update
Changing the implementation of the pattern How would the implementation of the Observer pattern change if: each observer subscribes not itself, but one of its routines to the publisher when the event occurs, the publisher must call these subscribed routines with information about the event Hands-On
Exercise: a news agency Consider you must create an application which allows a news agency to dispatch news to various papers, radio and TV stations, etc. How would you design and implement this application? Hands-On
Genericity But what if a certain box should only contain Lego bricks, i.e. all kinds of them (slanted, 2x4, etc.)? class BOX [G] feature wipe_out is ... put (v: G) is ... end
For Assignment 7 21 • Vector Geometry