正在加载图片...
PUBLIC ANSWERS TO EXERCISES PA14 Chapter 5. Modelling with classes E5.1 No public answer E52 p. 160 Designing associations among classes Note that the exercise says take care to specif labels for the associations', however, we have only shown labels where the default has'would not suffice. In many of the following, differences in class names are a)*We will see later on that aggregation could be used here; however, at this point in the book the following would be a reasonable answer. Making the multiplicity on the vehicle end o.I might be even better allowing for wheels to exist on their own, without being attached to vehicles Vehicle E5.3 p. 160 Explaining the consequences of associations in terms of creation and destruction of instances for association classes, which are described on page 16r ls to each other and which turm out to be candidates This exercise has several parts that are strongly analogou a)*A Vehicle could exist, and then a wheel could be created and associated with it; conversely, the wheels could exist first. Similarly, the order in which these associated objects are destroyed does not matter. c)*Under normal circumstances in the real world, a country exists over a longer period of time than any of its heads of state. However, if we imagine an information system storing biographical information, then a person might be created in the system who later on is recorded as the head of state of a country. So there re no necessary constraints on the order of creation or destruction of these instances e)*This is similar to parts b and d. The Registration would be created after the Member and the Activity, and would have to be destroyed no later than these associated instances. (In some circumstances, the Member and the Registration could be created together) E5.4 p. 160 Reading associations in both directions The exact phrasing of answers to this question will, of course, vary considerably. This exercise has proven particularly valuable at getting students to check their work and detect errors in their class diagrams. Creating these answers even helped us make improvements to our answers to E5.2. Different answers to E52 would result in different answers to this question a)*. A Vehicle can have no wheels, or an unspecified number of wheels A Wheel al ways belongs to exactly one Vehicle c)*. A Country has either zero or one known Headofstate A Headofstate can head any number of countrys e)*. A Member can register in any number of activities(can have any number of Registrations) Each Registration involves registering exactly one Member in exactly one Activity An Activity can have an arbitrary number of Registrations E5.5 P. 162 Adding association classes The question doesnt require a list of attributes, but we show attributes below to help explain the purpose of the association class a)*A reasonable association class might be called PlayingRecord or something similar. The attributes needed would depend on the type of sport, but might include positionPlayed goalsScored penalties and timeOnField Player SportGame Playing Record meonField THESE ANSWERS SUPPORT THE BOOK OBJECT ORIENTED SOFTWARE ENGINEERINGPUBLI C A NSW ERS T O EX E RCISE S PA14 T HE SEAN SW E RS SU PPORTT HEBO OK O BJ ECT O RI E NTEDSO FTW AR E E NG INE ER ING : Chapt er 5. Model li ng wi th cl asses E 5.1 No public answe r E 5.2 p. 160 D esigning associations among classes N ote that the exe rc isesays ‘ ta ke ca r e to spec ify … la be ls forthe assoc ia tions’; howe ver , w e have only shown labels w he r e the def a ult ‘ ha s’ would not suff ice . I n ma ny of the f ollow ing, diff e re nc e s in class name s ar e possible . a )*W e will se e late r on that aggre ga tion c ould be used he re ; how eve r, a t this point in the book the f ollow ing w ould be a re asona ble a nsw er . Making the multiplic ity on the Vehicle end 0..1 might be e ven be tter , a llow ing f or whe els to exist on the ir ow n, without be ing a tta ched to ve hicle s. * Vehicle Wheel E 5.3 p. 160 E xplaining thec onseque nce s of assoc iations in te rm s of cre ation and de struc tion of instanc es T his e xe rc ise ha s se ver al pa rts tha t a re strongly a na logous to ea c h other , a nd w hic h tur n out to be ca ndida tes f ora ssocia tion classes, w hich ar e descr ibe d on pa ge 161. a )*A Vehicle could e xist, a nd the n a Wheel could be c re ate d and a ssociate d with it; c onver se ly, the whe els c ould exist f ir st. Simila r ly, theor de r in which these a ssoc iated obje c ts a r e de str oye d doe s not matter . c )*U nder norma l cir cumstance s in ther e al w orld, a country exists ove r a longe rper iod oftimetha n a ny ofits hea ds of state. Howe ver , if we imagine a n inf ormation system stor ing biogra phica l inf ormation, the n a per son might be cr ea ted in the syste m who late r on is r e corde d as the hea d of sta te of a c ountr y. So ther e a reno nec e ssar y c onstr aints on theor de rof c re ation ordestruction ofthe se insta nc e s. e )*T his is similar to pa rts b a nd d. T he Registration would be c re ate d af ter the Member and the Activity, a nd would have to be de str oye d no la te r tha n the se assoc ia ted instanc es. ( In some c ir cumstanc es, the Member and the Registration could be c re ate d togethe r ). E 5.4 p. 160 R eading assoc iations in both dire c tions. T he e xac t phr asing of a nsw er s to this que stion w ill, of cour se, va ry c onside ra bly. This exe rc ise has pr ove n par tic ular ly va lua blea t getting stude nts to c he ck theirw or k a nd de te c t er r or s in the irc lass dia gr a ms. Cre ating the se answ e rs e ven he lped us ma ke impr ove me nts to our a nsw er s to E 5.2. Diff e re nt answ e rs to E 5.2 w ould r esult in dif fe r ent a nswe r s to this question. a )*• A Vehicle ca n have no Wheels, oran unspec ified numbe r of Wheels. • A Wheel alwa ys be longs to e xac tly one Vehicle. c )*• A Country ha s e ithe rz er o or oneknow n HeadOfState. • A HeadOfState ca n hea d a ny number of Countrys e )*• A Member ca n r egisterin a ny numbe r ofac tivitie s ( ca n havea ny numbe r of Registrations) • E ac h Registration involve s r egister ing e xa c tly one Member in e xac tly one Activity • A n Activity ca n have a n ar bitra r y numbe r of Registrations E 5.5 p. 162 A dding association classes. T he question doe sn’t re quire a list of a ttr ibute s, but w e show attributes be low to he lp expla in the pur pose of thea ssocia tion class. a )*A r ea sonable assoc ia tion c la ss might be c alle d PlayingRecord or some thing simila r . The a ttr ibute s nee de d w ould de pend on the type of sport, but might inc lude positionPlayed, goalsScored, penalties and timeOnField * * Player SportGame PlayingRecord positionPlayed goalsScored penalties timeOnField
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有