Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.

Similar presentations


Presentation on theme: "© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice."— Presentation transcript:

1 © Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd 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. For more material see http://eiffel.com

2 © Bertrand Meyer and Yishai Feldman Multiple Inheritance class COMPANY_PLANE inherit PLANE ASSET feature  Any feature that is specific to company planes (rather than applying to all planes or all assets)  end passenger_count altitude position speed take_off land set_speed purchase_price resale_value depreciate resell

3 © Bertrand Meyer and Yishai Feldman Domain Examples WATCH CALCULATOR WATCH_ CALCULATOR WATCH_ CALCULATOR BOAT PLANE HYDROPLANE VEHICLE HOUSE MOBILE_HOME TRAIN_CAR RESTAURANT EATING_CAR

4 © Bertrand Meyer and Yishai Feldman Software Examples deferred class NUMERIC feature  infix "+", infix "–", infix "  ", prefix "/", zero, one  end deferred class COMPARABLE feature  infix " ="  end expanded class REAL inherit NUMERIC COMPARABLE feature  end

5 © Bertrand Meyer and Yishai Feldman Implementation Inheritance class COMPOSITE_FIGURE inherit FIGURE LINKED_LIST [FIGURE] feature display is do from start until after loop item. display forth end

6 © 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 Result := (count = capacity) end

7 © Bertrand Meyer and Yishai Feldman 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 bounded: count <=capacity

8 © Bertrand Meyer and Yishai Feldman Structure Inheritance (Mixins 1) STORABLE * A A B B Inheritance of a structural property, describing one aspect of independent abstractions.

9 © Bertrand Meyer and Yishai Feldman Facility Inheritance (Mixins 2) Inheritance of a set of related facilities. HISTORY A A B B

10 © Bertrand Meyer and Yishai Feldman Renaming class SANTA_BARBARA inherit LONDON rename foo as fog end NEW_YORK rename foo as zoo end feature  end foo

11 © Bertrand Meyer and Yishai Feldman 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 … end

12 © Bertrand Meyer and Yishai Feldman 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

13 © 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

14 © Bertrand Meyer and Yishai Feldman Example: ARRAYED_STACK (3) feature-- Element change 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

15 © 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

16 © 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 feature  end

17 © Bertrand Meyer and Yishai Feldman 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.

18 © Bertrand Meyer and Yishai Feldman Sharing and Replication

19 © Bertrand Meyer and Yishai Feldman Attribute Replication

20 © 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.

21 © Bertrand Meyer and Yishai Feldman Single Name Rule Two different effective features of a class may not have the same final name.

22 © Bertrand Meyer and Yishai Feldman Example class A feature good: INTEGER end class B inherit A feature bad: REAL end class C inherit A feature bad: REAL end class D inherit -- Illegal class! B C end A A B B C C D D class D inherit -- Now valid B rename bad as now_ok end C end

23 © Bertrand Meyer and Yishai Feldman Conflicting Redefitions A A B B C C D D f f ++

24 © Bertrand Meyer and Yishai Feldman Undefine in Repeated Inheritance class D inherit B C undefine f end end A A B B C C D D f f ++

25 © Bertrand Meyer and Yishai Feldman Undefine in Multiple Inheritance class D inherit B C rename g as f undefine f end end B B C C D D f g

26 © Bertrand Meyer and Yishai Feldman Replication class D inherit B select g end C end A A B B C C D D f f  g class D inherit B C select f end end local x : A … !D! x x. f

27 © Bertrand Meyer and Yishai Feldman 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.

28 © Bertrand Meyer and Yishai Feldman Advanced Example WINDOW WINDOW_ WITH_BORDER WINDOW_ WITH_BORDER WINDOW_ WITH_MENU WINDOW_ WITH_MENU WINDOW_WITH BORDER_AND_MENU WINDOW_WITH BORDER_AND_MENU

29 © 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

30 © 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

31 © 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

32 © 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


Download ppt "© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice."

Similar presentations


Ads by Google