coen244: inheritance & composition using unified …coen244: inheritance & composition...
TRANSCRIPT
COEN244: Inheritance & Compositionusing Unified Modeling language (UML)
Aishy Amer
Electrical & Computer Engineering
OutlineModeling with UML
UML basic notation
Case study:video rental store
c© A. Amer UML 1
Modeling problems with UML
When a OO program becomes sufficiently complex,
it becomes difficult to visualize
the relationships between classes to implement
A common approach is to use graphical notation
to describe the relationships among real-life entities whose
behavior the application should emulatecircles and cylinders,customers and accounts, inventory items and theirsuppliers
c© A. Amer UML 2
Modeling problems with UML
OO analysis describes
the real-life system activities in the form of cooperating classes
OO design describes
the structural elements of the system:system architecture, subsystems, classes link
c© A. Amer UML 3
Modeling problems with UML
Design and analysis are similar butdesign gives more details such as additional links or improveperformance
OOP:
making system components as independent as possible
but describe the cooperation appropriately and precise
Can you separate analysis, design and implementation in OOP?
To which extent?
c© A. Amer UML 4
Modeling problems with UML
OOP: Analysis, design and implementation are stronglyrelatedWhat are the advantages?
OOP: no ad hoc object relationships
Hence we need a unified graphical tool (notation) thatan analysts, designer and programmer use
UML is a solution
Basic the idea :an object is a system component with its data,
functions, and relation to other objects
The result is object diagrams that are shared between
developers/teamsc© A. Amer UML 5
UML basic notation
c© A. Amer UML 6
Associations among classes
Association: connection between classes
c© A. Amer UML 7
Aggregation, composition, inheritance
Aggregation: one object is part of another object oranother object contains the first object (has it)
c© A. Amer UML 8
Multiplicity of relationships
c© A. Amer UML 9
A case study: video rental store
For a rental store: a program should keep track of customersborrowing and returning movies
What are the objects?
Rental item data: title, quantity on hand, & ID
Customers data: name, phone, number of movies borrowedand their IDs
The movie data is stored in a file with a letter indicating themovie category (’f’ for feature, ’c’ for comedy, ’h’ for horror)
When data is read into memory, the information is storednumerically (1=’f’, 2=’c’ 3=’h’)
c© A. Amer UML 10
A case study: video rental store
when data is displayed it is displayed as words (’feature’,’comedy’, ’horror’)
when a customer returns a movie, the user enters his/herphone number to search the database
after verification: the quantity on hand is decremented andmovie id is added to the list of movie id borrowed by thiscustomer
c© A. Amer UML 11
A case study: video rental store
Analysis and design:UML notation for classes with their attributes and operations
c© A. Amer UML 12
A case study: video rental store
Analysis and design: class relationships
c© A. Amer UML 13
A case study: video rental store
Design and implementation:UML class diagram for the program (adds more details)
c© A. Amer UML 14
A case study: main()
/ / L i s t i n g 14.16 Implementat ion o f f u n c t i o n main ( ) Store ( f i l e v ideo . cpp ) .#include <iostream >using namespace std ;#include " s to re . h " / / t h i s i s a necess i ty
i n t main ( ) {Inven to ry inv ; Store s to re ; / / de f ine ob jec tss to re . loadData ( inv ) ; / / load datawhile ( t rue ){ i n t r e s u l t = s to re . f indCustomer ( i nv ) ; / / check r e s u l t s
i f ( r e s u l t == 0) break ; / / te rmina te programi f ( r e s u l t == 2) / / 1 i f not founds to re . processItem ( inv ) ; } / / process the casset te
s to re . saveData ( inv ) ; / / save databasereturn 0;
}
c© A. Amer UML 15
A case study: Comments
main() does not ask the user to enter data and does notoutput anything itself
Try to find outwho is asking the user to enter the phone number of acustomer?Where is an error message is printed in case thecustomer ID does not exists?Who is responsible to update the files? etc.
⇒ A well-designed program pushes responsibility from clientcode to server code
⇒ Client code thus expresses the meaning of computations, notdetails of computations
c© A. Amer UML 16
A case study: video rental store
/ / L i s t i n g 14.6 Class s p e c i f i c a t i o n f o r the Item c lass ( f i l e i tem . h ) .# i fnde f ITEM_H#define ITEM_Hclass I tem {
protected :char t i t l e [ 2 6 ] ;i n t id , quant , category ;
publ ic :void set ( const char ∗s , i n t num, i n t qty , i n t type ) ;i n t getQuant ( ) const ;i n t ge t Id ( ) const ;void get I tem ( char ∗ name, i n t &num, i n t & qty , i n t &type ) const ;void p r i n t I t e m ( ) const ;void i nc rQty ( i n t qty ) ;
} ;#endi f
c© A. Amer UML 17
A case study: video rental store
/ / L i s t i n g 14.7 Class s p e c i f i c a t i o n f o r the Customer c lass ( f i l e customer . h ) .# i fnde f CUSTOMER_H#define CUSTOMER_Hclass Customer {
char name[ 2 0 ] , phone [ 1 5 ] ;i n t count ;i n t movies [ 1 0 ] ;
publ ic :Customer ( ) ;void set ( const char ∗nm, const char ∗ph ) ;void addMovie ( i n t i d ) ;i n t removeMovie ( i n t i d ) ;void getCustomer ( char ∗nm, char ∗ph , i n t &cnt , i n t m[ ] ) const ;
} ;#endi f
c© A. Amer UML 18
A case study: video rental store
/ / L i s t i n g 14.8 Implementat ion o f c lass Item ( f i l e i tem . cpp ) .#include <iostream >using namespace std ;#include " i tem . h " / / t h i s i s a necess i ty
void I tem : : se t ( const char ∗s , i n t num, i n t qty , i n t type ){ s t r cpy ( t i t l e , s ) ; i d =num; quant=qty ; category=type ; }
i n t I tem : : getQuant ( ) const / / used by Inven to ry : : checkOut ( ){ return quant ; }
i n t I tem : : ge t Id ( ) const{ return i d ; } / / i n p r i n t R e n t a l ( ) , checkOut ( ) , checkIn ( )
void I tem : : get I tem ( char ∗ name, i n t &num,i n t & qty , i n t &type ) const / / used by F i l e : : saveItem ( )
{ s t r cpy (name, t i t l e ) ; num = i d ; q ty = quant ; type = category ; }
c© A. Amer UML 19
A case study: video rental store
void I tem : : p r i n t I t e m ( ) const / / used by p r i n t R e n t a l ( ){ cout . s e t f ( i os : : l e f t , i os : : a d j u s t f i e l d ) ;
cout . width ( 5 ) ; cout << i d ; / / i t knows i t s p r i n t formatscout . width ( 2 7 ) ; cout << t i t l e ;switch ( category ) { / / d i f f e r e n t i tem subtypescase 1: cout << " fea tu re " ; break ;case 2: cout << " comedy " ; break ;case 3: cout << " ho r ro r " ; break ; }cout << endl ; }
void I tem : : i nc rQty ( i n t qty ) / / used i n checkOut ( ) , checkIn ( ){ quant += qty ; }
c© A. Amer UML 20
A case study: video rental store
/ / L i s t i n g 14.9 Implementat ion o f c lass Customer ( f i l e customer . cpp ) .#include <iostream >using namespace std ;#include " customer . h " / / t h i s i s a necess i ty
Customer : : Customer ( ) { count = 0 ; }
void Customer : : se t ( const char ∗nm, const char ∗ph ){ s t r cpy (name,nm) ; s t r cpy ( phone , ph ) ; } / / i n appendCust ( )
void Customer : : addMovie ( i n t i d ){ movies [ count ++] = i d ; } / / i n appendCust ( ) , checkOut ( )
c© A. Amer UML 21
A case study: video rental store
i n t Customer : : removeMovie ( i n t i d ) / / used i n checkIn ( ){ i n t i dx ;
fo r ( i dx =0; i dx < count ; i dx ++) / / f i n d the moviei f ( movies [ i dx ] == i d ) break ;
i f ( i dx == count ) return 0; / / g ive up i f not foundwhile ( i dx < count − 1){ movies [ i dx ] = movies [ i dx + 1 ] ; / / s h i f t t a i l to the l e f t
i dx ++; }count−−; / / decrement movie countreturn 1; } / / r e p o r t success
void Customer : : getCustomer ( char ∗nm, char ∗ph , / / saveData ( )i n t &cnt , i n t m[ ] ) const / / I nven to ry : : getCustomer ( )
{ s t r cpy (nm, name ) ; s t r cpy ( ph , phone ) ; cn t = count ;fo r ( i n t i =0; i < count ; i ++)
m[ i ] = movies [ i ] ; }
c© A. Amer UML 22
A case study: video rental store
/ / L i s t i n g 14.10 Class s p e c i f i c a t i o n f o r the Inven to ry c lass ( f i l e i nven to ry . h# i fnde f INVENTORY_H#define INVENTORY_H#include " i tem . h "#include " customer . h "
class I nven to ry {
protected :enum { MAXM = 5 , MAXC = 4 } ; / / j u s t f o r the pro to typeI tem i t e m L i s t [MAXM] ;Customer c u s t L i s t [MAXC] ;i n t i temCount , custCount ;i n t i temIdx , cus t Idx ;
c© A. Amer UML 23
A case study: video rental store
publ ic :I nven to ry ( ) ;void appendItem ( const char ∗ t t l , i n t id , i n t qty , i n t cat ) ;void appendCust ( const char ∗ nm, const char ∗ ph , i n t cnt , const i n t ∗m) ;i n t get I tem ( Item& item ) ;i n t getCustomer ( char ∗ nm, char ∗ ph , i n t &cnt , i n t ∗m) ;void p r i n t R e n t a l ( i n t i d ) ;i n t checkOut ( i n t i d ) ;void checkIn ( i n t i d ) ;
} ;
#endi f
c© A. Amer UML 24
A case study: video rental store
/ / L i s t i n g 14.11 Implementat ion o f c lass Inven to ry ( f i l e i nven to ry . cpp ) .#include <iostream >using namespace std ;#include " i nven to ry . h " / / t h i s i s a necess i tyI nven to ry : : I nven to ry ( )
{ i temCount = i temIdx = 0; custCount = cus t Idx = 0; }void I nven to ry : : appendItem ( const char ∗ t t l , i n t id , i n t qty , i n t cat ){ i f ( i temCount == MAXM) / / used i n loadData ( )
{ cout << " \ nNo space to i n s e r t i tem " ; }else
{ i t e m L i s t [ i temCount ++ ] . se t ( t t l , id , qty , ca t ) ; } }
void I nven to ry : : appendCust ( const char ∗ nm, const char ∗ ph ,i n t cnt , const i n t ∗movie )
{ i f ( custCount == MAXC) / / used i n loadData ( ){ cout << " \ nNo space to i n s e r t customer " ; return ; }
c u s t L i s t [ custCount ++ ] . se t (nm, ph ) ;fo r ( i n t j =0; j < cnt ; j ++)
c u s t L i s t [ custCount −1] . addMovie ( movie [ j ] ) ; }c© A. Amer UML 25
A case study: video rental store
i n t I nven to ry : : get I tem ( Item &item ) / / used i n saveData ( ){ i f ( i temIdx == itemCount )
{ i temIdx = 0; return 0; }i tem = i t e m L i s t [ i temIdx ++ ] ;return 1; }
i n t I nven to ry : : getCustomer ( char ∗ nm, char ∗ ph , i n t &cnt , i n t ∗m){ i f ( cus t Idx == custCount ) / / i n f indCustomer ( ) , saveData ( )
{ cus t Idx = 0; return 0; }c u s t L i s t [ cus t Idx ++ ] . getCustomer (nm, ph , cnt ,m) ;return 1; }
void I nven to ry : : p r i n t R e n t a l ( i n t i d ) / / used i n f indCustomer ( ){ fo r ( i temIdx = 0; i temIdx < itemCount ; i temIdx ++)
{ i f ( i t e m L i s t [ i temIdx ] . ge t Id ( ) == i d ){ i t e m L i s t [ i temIdx ] . p r i n t I t e m ( ) ; break ; } }
i temIdx = 0 ; }
c© A. Amer UML 26
A case study: video rental store
i n t I nven to ry : : checkOut ( i n t i d ) / / used i n processItem ( ){ fo r ( i temIdx = 0; i temIdx < itemCount ; i temIdx ++)
i f ( i t e m L i s t [ i temIdx ] . ge t Id ( ) == i d ) break ;i f ( i temIdx == itemCount ){ i temIdx = cus t Idx = 0; return 0; }i f ( i t e m L i s t [ i temIdx ] . getQuant ( ) == 0){ i temIdx = cus t Idx = 0; return 1; }i t e m L i s t [ i temIdx ] . i nc rQty ( −1) ;c u s t L i s t [ cus t Idx − 1 ] . addMovie ( i d ) ;i temIdx = cus t Idx = 0; return 2; }
void I nven to ry : : checkIn ( i n t i d ) / / used i n processItem ( ){ i f ( c u s t L i s t [ cus t Idx − 1 ] . removeMovie ( i d )==0)
{ cout << " Movie i s not found \ n " ; i temIdx = cus t Idx = 0; return ; }fo r ( i temIdx = 0; i temIdx < itemCount ; i temIdx ++)
{ i f ( i t e m L i s t [ i temIdx ] . ge t Id ( ) == i d ){ i t e m L i s t [ i temIdx ] . i nc rQty ( 1 ) ; break ; } }
i temIdx = cus t Idx = 0; cout << " Movie i s re tu rned \ n " ; }
c© A. Amer UML 27
A case study: File class
/ / L i s t i n g 14.12 Class s p e c i f i c a t i o n f o r the F i l e c lass ( f i l e f i l e . h ) .# i fnde f FILE_H#define FILE_H#include " i tem . h "#include <fstream >using namespace std ;class F i l e {
fs t ream f ;s t a t i c void t r i m ( char b u f f e r [ ] ) ;enum { TWIDTH = 27 , IWIDTH = 5 , QWIDTH = 6 , NWIDTH = 18 , PWIDTH = 16 } ;
publ ic :F i l e ( const char name [ ] , i n t mode ) ;i n t get I tem ( char ∗ t t l , i n t &id , i n t &qty , char &type ) ;void saveItem ( const I tem &item ) ;i n t getCustomer ( char ∗name, char ∗phone , i n t &count , i n t ∗m) ;void saveCustomer ( const char ∗nm, const char ∗ph ,
i n t cnt , i n t ∗m) ;} ;#endi f
c© A. Amer UML 28
A case study: File class implemetation
/ / L i s t i n g 14.13 Implementat ion o f c lass F i l e ( f i l e f i l e . cpp ) .#include <iostream >using namespace std ;#include " f i l e . h " / / t h i s i s a necess i ty
F i l e : : F i l e ( const char name [ ] , i n t mode){ f . open (name, mode ) ; / / used i n loadData ( ) , saveData ( )
i f ( f . f a i l ( ) ) / / i f ( f . is_open ( ) ) i s OK, too{ cout <<" F i l e i s not open \ n " ; e x i t ( 1 ) ; } }
i n t F i l e : : get I tem ( char ∗ t t l , i n t &id , i n t &qty , char &type ){ char b u f f e r [ 2 0 0 ] ; / / i n loadData ( )
f . get ( bu f fe r ,TWIDTH ) ;t r i m ( b u f f e r ) ;s t r cpy ( t t l , b u f f e r ) ; / / i t knows f i l e s t r u c t u r ef >> i d ; f >> qty ; f >> type ; f . g e t l i n e ( bu f fe r , 4 ) ;i f ( ! f ) return 0;return 1; }
c© A. Amer UML 29
A case study: video rental store
void F i l e : : saveItem ( const I tem &item ) / / i n saveData ( ){ char t t [ 2 7 ] ; i n t id , qty , type ;
i tem . get I tem ( t t , id , qty , type ) ;f . s e t f ( i os : : l e f t , i os : : a d j u s t f i e l d ) ;f . w id th (TWIDTH ) ; f << t t ; / / i t knows f i l e formatf . s e t f ( i os : : r i g h t , i os : : a d j u s t f i e l d ) ;f . w id th ( IWIDTH ) ; f << i d ;f . w id th (QWIDTH) ; f << qty ;switch ( type ) { / / d i f f e r e n t f o r d i f f e r e n t subtypes
case 1: f << " f \ n " ; break ;case 2: f << " c \ n " ; break ;case 3: f << " h \ n " ; break ; } }
c© A. Amer UML 30
A case study: video rental store
i n t F i l e : : getCustomer ( char ∗name, char ∗phone , i n t &count , i n t ∗m){ char b u f f e r [ 2 0 0 ] ; / / i n loadData ( )
f . get ( bu f fe r ,NWIDTH ) ;t r i m ( b u f f e r ) ;s t r cpy (name, b u f f e r ) ;f >> b u f f e r ; f >> count ; / / i t knows f i l e s t r u c t u r es t r cpy ( phone , b u f f e r ) ;fo r ( i n t i =0; i < count ; i ++)
f >> m[ i ] ;f . g e t l i n e ( bu f fe r , 2 ) ;i f ( ! f ) return 0;return 1; }
c© A. Amer UML 31
A case study: video rental store
void F i l e : : saveCustomer ( const char ∗nm, const char ∗ph ,i n t cnt , i n t ∗m) / / i n saveData ( )
{ f . s e t f ( i os : : l e f t , i os : : a d j u s t f i e l d ) ; f . w id th (NWIDTH ) ;f << nm;f . s e t f ( i os : : r i g h t , i os : : a d j u s t f i e l d ) ; f . w id th (PWIDTH ) ;f << ph << endl << cnt ; / / i t knows f i l e s t r u c t u r efo r ( i n t i =0; i < cnt ; i ++){ f . w id th ( 6 ) ; f << m[ i ] ; }f << endl ; }
void F i l e : : t r i m ( char b u f f e r [ ] ) / / i n get I tem ( ) , getCustomer ( ){ fo r ( i n t j = s t r l e n ( b u f f e r )−1; j >0; j −−)
i f ( b u f f e r [ j ]== ’ ’ | | b u f f e r [ j ]== ’ \ n ’ )b u f f e r [ j ] = ’ \0 ’ ;
elsebreak ; }
c© A. Amer UML 32
A case study: store class
/ / L i s t i n g 14.14 Class s p e c i f i c a t i o n f o r the Store c lass ( f i l e s to re . h ) .# i fnde f STORE_H#define STORE_H#include " i nven to ry . h "#include " f i l e . h "
class Store {publ ic :
void loadData ( Inven to ry &inv ) ;i n t f indCustomer ( Inven to ry& inv ) ;void processItem ( Inven to ry& inv ) ;void saveData ( Inven to ry &inv ) ;
} ;#endi f
c© A. Amer UML 33
A case study: store class implementation
/ / L i s t i n g 14.15 Implementat ion o f c lass Store ( f i l e s to re . cpp ) .#include <iostream >using namespace std ;#include " s to re . h " / / t h i s i s a necess i ty
void Store : : loadData ( Inven to ry &inv ){ F i l e i temsIn ( " Item . dat " , i os : : i n ) ; / / i tem database
char t t l [ 2 7 ] , category ; i n t id , qty , type ; / / i tem datacout << " Loading database . . . " << endl ;while ( i temsIn . get I tem ( t t l , id , qty , category )==1) / / read i n{ switch ( category ) { / / se t category f o r the subtype
case ’ f ’ : type = 1; break ;case ’ c ’ : type = 2; break ;case ’ h ’ : type = 3; break ; }
i nv . appendItem ( t t l , id , qty , type ) ; }F i l e cus t I n ( " Cust . dat " , i os : : i n ) ; / / customer databasechar name[ 2 5 ] , phone [ 1 5 ] ; i n t movies [ 1 0 ] , count ;while ( cus t I n . getCustomer (name, phone , count , movies )==1){ i nv . appendCust (name, phone , count , movies ) ; } } / / pump data
c© A. Amer UML 34
A case study: video rental store
i n t Store : : f indCustomer ( Inven to ry& inv ){ char b u f f e r [ 2 0 0 ] ; char name[ 2 5 ] , phone [ 1 3 ] ;
i n t count , movies [ 1 0 ] ;cout << " Enter customer phone ( or press Return to q u i t ) " ;c i n . g e t l i n e ( bu f fe r , 1 5 ) ;i f ( strcmp ( bu f fe r , " " )==0) return 0; / / q u i t i f no data enteredbool found = fa lse ;while ( i nv . getCustomer (name, phone , count , movies ) != 0)
{ i f ( strcmp ( bu f fe r , phone ) == 0) / / search f o r the phone{ found = t rue ; break ; } } / / s top i f phone found
i f ( ! found ){ cout << " \ nCustomer i s not found " << endl ;
return 1; } / / g ive up i f not foundcout . s e t f ( i os : : l e f t , i os : : a d j u s t f i e l d ) ;cout . width ( 2 2 ) ; cout << name << phone << endl ; / / p r i n t datafo r ( i n t j = 0 ; j < count ; j ++)
{ i nv . p r i n t R e n t a l ( movies [ j ] ) ; } / / p r i n t movie Id ’ scout << endl ;return 2; } / / success code
c© A. Amer UML 35
A case study: video rental store
void Store : : processItem ( Inven to ry& inv ){ i n t cmd, r e s u l t , i d ;
cout << " Enter movie i d : " ;c i n >> i d ; / / search a t t r i b u t ecout << " Enter 1 to check out , 2 to check i n : " ;c in >> cmd ;i f (cmd == 1){ r e s u l t = inv . checkOut ( i d ) ; / / analyze r e t u r n value
i f ( r e s u l t == 0) / / not foundcout << " Movie i s not found " << endl ;else i f ( r e s u l t == 1) / / out o f s tockcout << " Movie i s out o f s tock " << endl ;else / / i t i s a success
cout << " Renting i s conf i rmed \ n " ; }else i f (cmd == 2)inv . checkIn ( i d ) ; / / feedback i n checkIn ( )c in . get ( ) ; } / / e l i m i n a t e CR from l i n e
c© A. Amer UML 36
A case study: video rental store
void Store : : saveData ( Inven to ry &inv ){ F i l e itemsOut ( " I tem . out " , i os : : out ) ; I tem item ; / / i tem f i l e
while ( i nv . get I tem ( i tem ) ) / / no i n t e r n a l s t r u c t u r ei temsOut . saveItem ( i tem ) ; / / save each i tem
F i l e custOut ( " Cust . out " , i os : : out ) ; / / customer output f i l echar name[ 2 5 ] , phone [ 1 3 ] ; i n t count , movies [ 1 0 ] ;cout << " Saving database . . . " << endl ;while ( i nv . getCustomer (name, phone , count , movies ) ) / / pump data
custOut . saveCustomer (name, phone , count , movies ) ; }
c© A. Amer UML 37