generic collection class motivation 1
play

Generic Collection Class: Motivation (1) class STRING _STACK - PowerPoint PPT Presentation

Generic Collection Class: Motivation (1) class STRING _STACK feature { NONE } -- Implementation imp : ARRAY [ STRING ] ; i : INTEGER Use of Generics feature -- Queries count : INTEGER do Result := i end -- Number of items on stack. top : STRING


  1. Generic Collection Class: Motivation (1) class STRING _STACK feature { NONE } -- Implementation imp : ARRAY [ STRING ] ; i : INTEGER Use of Generics feature -- Queries count : INTEGER do Result := i end -- Number of items on stack. top : STRING do Result := imp [ i ] end -- Return top of stack. feature -- Commands push ( v : STRING ) do imp [ i ] := v ; i := i + 1 end EECS3311 A & E: Software Design -- Add ’v’ to top of stack. pop do i := i - 1 end Fall 2020 -- Remove top of stack. end C HEN -W EI W ANG ○ Does how we implement string stack operations (e.g., top , push , pop ) depends on features specific to element type STRING (e.g., at , append )? [ NO! ] ○ How would you implement another class ACCOUNT STACK ? 3 of 9 Learning Objectives Generic Collection Class: Motivation (2) class ACCOUNT _STACK feature { NONE } -- Implementation imp : ARRAY [ ACCOUNT ] ; i : INTEGER feature -- Queries count : INTEGER do Result := i end -- Number of items on stack. top : ACCOUNT do Result := imp [ i ] end Upon completing this lecture, you are expected to understand: -- Return top of stack. 1. How to write a generic class (as a supplier ) feature -- Commands push ( v : ACCOUNT ) do imp [ i ] := v ; i := i + 1 end 2. How to use a generic class (as a client ) -- Add ’v’ to top of stack. pop do i := i - 1 end -- Remove top of stack. end ○ Does how we implement account stack operations (e.g., top , push , pop ) depends on features specific to element type ACCOUNT (e.g., deposit , withdraw )? [ NO! ] ○ A collection (e.g., table, tree, graph) is meant for the storage and retrieval of elements, not how those elements are manipulated. 2 of 9 4 of 9

  2. Generic Collection Class: Supplier Generic Collection Class: Client (1.2) ● Your design “smells” if you have to create an almost identical As client , declaring ss: STACK[ ACCOUNT ] instantiates every new class (hence code duplicates ) for every stack element occurrence of G as ACCOUNT . type you need (e.g., INTEGER , CHARACTER , PERSON , etc.). class STACK [ � ● Instead, as supplier , use G to parameterize element type: G ACCOUNT] feature { NONE } -- Implementation imp : ARRAY [ � class STACK [G] G ACCOUNT ] ; i : INTEGER feature { NONE } -- Implementation feature -- Queries imp : ARRAY [ G ] ; i : INTEGER count : INTEGER do Result := i end feature -- Queries -- Number of items on stack. count : INTEGER do Result := i end top : � G ACCOUNT do Result := imp [ i ] end -- Number of items on stack. -- Return top of stack. top : G do Result := imp [ i ] end feature -- Commands -- Return top of stack. push ( v : � G ACCOUNT ) do imp [ i ] := v ; i := i + 1 end feature -- Commands -- Add ’v’ to top of stack. push ( v : G ) do imp [ i ] := v ; i := i + 1 end pop do i := i - 1 end -- Add ’v’ to top of stack. -- Remove top of stack. pop do i := i - 1 end end -- Remove top of stack. end 5 of 9 7 of 9 Generic Collection Class: Client (1.1) Generic Collection Class: Client (2) As client , instantiate the type of G to be the one needed. As client , declaring ss: STACK[ STRING ] instantiates every 1 test_stacks : BOOLEAN occurrence of G as STRING . 2 local 3 ss : STACK [ STRING ] ; sa : STACK [ ACCOUNT ] class STACK [ � G STRING] 4 s : STRING ; a : ACCOUNT feature { NONE } -- Implementation 5 do imp : ARRAY [ � 6 ss . push ("A") G STRING ] ; i : INTEGER 7 ss . push ( create { ACCOUNT }. make ("Mark", 200)) feature -- Queries 8 s := ss . top count : INTEGER do Result := i end 9 a := ss . top -- Number of items on stack. 10 top : � sa . push ( create { ACCOUNT }. make ("Alan", 100)) G STRING do Result := imp [ i ] end 11 sa . push ("B") -- Return top of stack. 12 a := sa . top feature -- Commands 13 s := sa . top push ( v : � 14 G STRING ) do imp [ i ] := v ; i := i + 1 end end -- Add ’v’ to top of stack. ● L3 commits that ss stores STRING objects only. pop do i := i - 1 end -- Remove top of stack. ○ L8 and L10 valid ; L9 and L11 invalid . end ● L4 commits that sa stores ACCOUNT objects only. ○ L12 and L14 valid ; L13 and L15 invalid . 6 of 9 8 of 9

  3. Index (1) Learning Objectives Generic Collection Class: Motivation (1) Generic Collection Class: Motivation (2) Generic Collection Class: Supplier Generic Collection Class: Client (1.1) Generic Collection Class: Client (1.2) Generic Collection Class: Client (2) 9 of 9

Recommend


More recommend