538bb91e7d204c051fa202c47872a455.ppt
- Количество слайдов: 32
Notice Some of the material is taken from Object-Oriented Software Construction, 2 nd edition, by Bertrand Meyer (Prentice Hall). Included here by permission of ISE, for the benefit of IDC students. Other uses, duplication or distribution require permission from ISE
Multiple Inheritance take_off land set_speed passenger_count altitude position speed purchase_price resale_value depreciate resell class COMPANY_PLANE inherit PLANE ASSET feature ¼ Any feature that is specific to company planes (rather © Bertrand Meyer andto all Feldman or all assets) ¼ than applying Yishai planes end
Domain Examples WATCH CALCULATOR BOAT WATCH_ CALCULATOR VEHICLE PLANE HYDROPLANE HOUSE TRAIN_CAR MOBILE_HOME © Bertrand Meyer and Yishai Feldman RESTAURANT EATING_CAR
Software Examples deferred class NUMERIC feature ¼ infix "+", infix "–", infix "*", prefix "/", zero, one ¼ end deferred class COMPARABLE feature ¼ infix "<", infix "<=", infix ">=" ¼ end expanded class REAL inherit NUMERIC COMPARABLE feature ¼ end © Bertrand Meyer and Yishai Feldman
Implementation Inheritance class inherit COMPOSITE_FIGURE LINKED_LIST [FIGURE] feature display is do from start until after loop item. display forth © Bertrand Meyer and Yishai Feldman
Marriage of Convenience (1( indexing description: "Stacks implemented as arrays" class ARRAYED_STACK [G] inherit STACK [G] ARRAY [G] rename count as capacity, put as array_put, … feature count: INTEGER full: BOOLEAN is do © Bertrand Meyer and Yishai Feldman Result : = (count = capacity)
Marriage of Convenience (2( put (x: G) is -- Push x on top. require not full do count : = count + 1 array_put (x, count) end invariant non_negative_count: count >= 0 redundant © Bertrand Meyer and Yishai Feldman bounded: count <=capacity --
Structure Inheritance (Mixins 1( Inheritance of a structural property, describing one aspect of independent abstractions. STORABLE * A B © Bertrand Meyer and Yishai Feldman
Facility Inheritance (Mixins 2( Inheritance of a set of related facilities. HISTORY A B © Bertrand Meyer and Yishai Feldman
Renaming class SANTA_BARBARA inherit LONDON rename foo as fog end NEW_YORK rename foo as zoo end feature foo ¼ end © Bertrand Meyer and Yishai Feldman foo
Renaming for Local Adaptation class WINDOW inherit TREE [WINDOW] rename child as subwindow, is_leaf as is_terminal, root as screen, arity as child_count, . . . RECTANGLE feature … specific window features … © Bertrand Meyer and Yishai Feldman end
Example: ARRAYED_STACK (1) class ARRAYED_STACK [G] inherit STACK [G] redefine change_top end ARRAY [G] rename count as capacity, put as array_put, make as array_make creation make © Bertrand Meyer and Yishai Feldman
Example: ARRAYED_STACK (2) feature -- Initialization make (n: INTEGER) is -- Allocate stack for at most n elements require non_negative_size: n >= 0 do array_make (1, n) ensure capacity_set: capacity = n empty: count = 0 end © Bertrand Meyer and Yishai Feldman
Example: ARRAYED_STACK (3) feature -- Element change_top (x: G) is -- Replace top element by x require not_empty: not empty do array_put (x, count) -- remove; put (x) end … other features … invariant count <= capacity end -- class ARRAYED_STACK © Bertrand Meyer and Yishai Feldman
Repeated Inheritance class DRIVER feature age: INTEGER address: STRING violation_count: INTEGER -- The number of recorded traffic violations pass_birthday is do age : = age + 1 end pay_ fee is -- Pay the yearly license fee. do ¼ end … end © Bertrand Meyer and Yishai Feldman
Intercontinental Drivers class FRENCH_US_DRIVER inherit FRENCH_DRIVER rename address as french_address, violation_count as french_violation_count, pay_fee as pay_french_fee end US_DRIVER rename address as us_address, violation_count as us_violation_count, pay_fee as pay_us_fee end © Bertrand Meyer and Yishai Feldman feature ¼ end
Repeated Inheritance Rule In a repeated descendant, versions of a repeatedly inherited feature inherited under the same name represent a single feature. Versions inherited under different names represent separate features, each replicated from the original in the common ancestor. © Bertrand Meyer and Yishai Feldman
Sharing and Replication © Bertrand Meyer and Yishai Feldman
Attribute Replication © Bertrand Meyer and Yishai Feldman
Final Name The final name of a featured declared in a class is: u For an immediate feature (that is, a feature declared in the class itself), the name under which it is declared. u For an inherited feature that is not renamed, its final name (recursively) in the parent from which it is inherited. u For a renamed feature, the name resulting from the renaming. © Bertrand Meyer and Yishai Feldman
Single Name Rule Two different effective features of a class may not have the same final name. © Bertrand Meyer and Yishai Feldman
Example class A feature A good: INTEGER end class B inherit A feature bad: REAL B C end class C inherit A feature bad: REAL D end class D inherit -- Illegal class! -- Now valid B B C rename bad as now_ok end C © Bertrand Meyer and Yishai Feldman end
Conflicting Redefitions f f ++ A B C D © Bertrand Meyer and Yishai Feldman
Undefine in Repeated Inheritance class D inherit B C f undefine f end f ++ B A C D © Bertrand Meyer and Yishai Feldman
Undefine in Multiple Inheritance class D inherit B C rename g as f undefine f end f B end C D © Bertrand Meyer and Yishai Feldman g
Replication class D inherit B f A select g end f g C B C end class D inherit B C select f end D local x : A … !D! x © Bertrand Meyer and Yishai Feldman x. f
Select Rule A class that inherits two or more different effective versions of a feature from a repeated ancestor, and does not redefine them all, must include exactly one of them in a select clause. © Bertrand Meyer and Yishai Feldman
Advanced Example WINDOW_ WITH_BORDER WINDOW_ WITH_MENU WINDOW_WITH BORDER_AND_MENU © Bertrand Meyer and Yishai Feldman
WINDOW_WITH_BORDER class WINDOW_WITH_BORDER inherit WINDOW redefine display end feature display is – Draw window and its border. do Precursor draw_border end … end © Bertrand Meyer and Yishai Feldman
WINDOW_WITH_MENU class WINDOW_WITH_MENU inherit WINDOW redefine display end feature display is – Draw window and its menu. do Precursor draw_menu end … end © Bertrand Meyer and Yishai Feldman
Combining Borders and Menus Incorrectly class WINDOW_WITH_BORDER_AND_MENU inherit WINDOW_WITH_BORDER redefine display end WINDOW_WITH_MENU redefine display end feature display is – Draw window and its border and menu. do {{WINDOW_WITH_BORDER}} Precursor {{WINDOW_WITH_MENU}} Precursor end … end © Bertrand Meyer and Yishai Feldman
Combining Borders and Menus Correctly class WINDOW_WITH_BORDER_AND_MENU inherit WINDOW_WITH_BORDER redefine display end WINDOW_WITH_MENU redefine display end WINDOW redefine display end feature display is – Draw window and its border and menu. do {{WINDOW}} Precursor draw_border draw_menu end … end © Bertrand Meyer and Yishai Feldman


