ood-2009-tdd-chuong 10

Upload: thanh-duy

Post on 30-May-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    1/93

    Hanoi University of Technology1 Dai Co Viet - Hanoi - Vietnam

    Phn tch v thit khng i tng(Object Oriented System

    Analysis and Design)

    Ging vin: TS. Trn t

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    2/93

    2008-2009

    Chng 5. Thit k h thng

    5.1 Chuyn sang thit k

    5.2 Thit k lp v phng thc

    5.3 Thit k lp qun l d liu

    5.4 Thit k lp tng tc ngi-my

    5.5 Thit k lp kin trc vt l

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    3/93

    2008-2009

    5.2 Thit k lp v phng thc

    5.2.1 Gii thiu

    5.2.2 Nhng vn c bn ca thit k HT.

    5.2.3 Tiu ch thit k

    5.2.4 Hot ng thit k i tng

    5.2.5 Cc rng buc5.2.6 p dng trong CD Selection

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    4/93

    2008-2009

    Bc quan trng nht trong pha thit k hng itng l thit k lp v phng thc (hnh vi).

    Vic thit k hng i tng phi cung cp y thng tin cho vic hon thin v chnh sa nhng cngvic s c trin khai.

    Cn phi hiu khi nim: coupling, cohesion and

    connascence* restructure and optimise object designs

    constraints and contracts

    Create a method specification

    5.2.1. Gii thiu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    5/93

    2008-2009

    Vi tt c cc tng, cc lp m c s dng to racc i tng th cn phi thit k.

    Bt chp kh nng ca cc thnh phn phn mm

    c pht trin (cc th vin), Thit k chi tit cc hthng con, cc h thng vn rt quan trng: Phn mm pht trin cn phi c hiu, c t chc

    v c kt hp mt cch chnh xc vi phn mm khc. Mt s phn mm c pht trin theo theo phng php

    hng mc ch ca nhm pht trin.

    Pht trin v trin khai m khng cn vic thit k chnhxc v hon thin s dn n: Trin khai khng hiu qu: v phc tp ca nhng tng

    tc gia cc thnh phn phn mm, thnh phn h thng. Trin khai khng chnh xc, n khng tha mn nhng thit

    k v/hoc khng hot ng chnh xc/y nhng chc

    nng d kin.

    5.2.1. Gii thiu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    6/93

    2008-2009

    Trong cc HTHT, cc thay i c th din ra ti ccmc khc nhau ca s tru tng ha: bin (th hin),hnh vi (phng thc), lp v i tng, nhm, th vin,

    ng dng. Cc thay i ti cc mc khc nhau ca vic tru tng

    c th nh hng ti cc mc khc ca s tru tng. Cc thay i c th din ra ti cc mc khc nhau mt

    cch ng thi. Nu ti pha phn tch cc tng tc gia cc i tng

    ti tng min bi ton c thit k chun ( chi tit)th vic thit k chi tit s d dng v t gp kh khn.(tng t vi cc tng HCI, qun l d liu)

    Do cc lp min bi ton khi to phi c thit kchnh xc v hon thin.

    5.2.1. Gii thiu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    7/93

    2008-2009

    Cc mc tru tng

    5.2.1. Gii thiu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    8/93

    2008-2009

    5.2.2. Nhng vn c bn ca thit k HT

    Lp, i tng, phng thc thng ip: Khi c bn ca mt h thng phn mm hng i

    tng l i tng Cc i tngl th hin ca cc lp. Lp l khun dng cho vic nh ngha i tng, n

    nh ngha d liu(thuc tnh) v ccphng thcm

    mi i tng trong lp cn phi c. Thuc tnh (Attributes): cha ng thng tin miu t v

    i tng.

    Phng thc (Methods, thao tc): thc thi hot ng cai tng.

    Cc thng ip (messages): thng tin c gi ti cci tng kch hot cc phng thc

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    9/93

    2008-2009

    Tm lc (ng gi) (Encapsulation) S kt hp ca c d liu v tin trnh trong

    mt thc th. n thng tin (Information Hiding)

    Ch c thng tin m n c yu cu s

    dng mt m un phn mm c cng khaiti ngi s dng

    5.2.2. Nhng vn c bn ca thit k HT

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    10/93

    2008-2009

    a hnh thi (Polymorphism) Mt thng ip c th c phin dch khc nhau bi cc

    i tng khc nhau.

    Kt ni ng (Dynamic Binding) Lm tr vic la chn cc phng thc cho mt i tng

    cho ti thi im chy chng trnh.

    V d: Bn tin c gi ti cc i tng: hnh trn,

    hnh vung, hnh tam gic. Ty thuc i tng, phngthc v hnh s khc.

    a hnh thi l con dao 2 li: Vic quyt nh c thc hin bi h thng.

    C gp vn v thi gian chy nu i tng khng hiu

    bn tin

    5.2.2. Nhng vn c bn ca thit k HT

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    11/93

    2008-2009

    Tha k Cc lp s c sp xp theo mt cu trc phn cp,

    cc lp trn, v cc lp di. Cc lp trn hay cc lp tng quan: nm trn nh h

    thng phn cp lp Cc lp con hay cc lp c th: pha di ca h

    thng. Cc lp con s tha k cc thuc tnh v v cc phng

    thc ca cc lp cao hn trong h thng phn cp.

    Tnh tha k lm cho vic nh ngha lp c n gin

    Hai nh dng ca c ch k tha. Tha k n: lp con ch c mt lp cha. Tha k bi (phc): lp con c th c nhiu lp cha.

    5.2.2. Nhng vn c bn ca thit k HT

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    12/93

    2008-2009

    Tha k Mt vi phng php, CSDL v ngn ng lp trnh cho php lp con

    nh ngha li mt s thuc tnh v/hoc hnh v ca lp cha cachng

    nh ngha li v Xung t tha k: Vic nh ngha li c th dn timt s xung t. Vd : mt thuc tnh hoc mt hnh vi ca lp contrng tn vi thuc tnh v hnh vi ca lp cha.

    Doctor

    +computePay()

    Employee

    +computePay()

    +updateSchedule()

    Is subclass of

    a) Both have methods namedcomputePay()

    +update schedule()b) If Employee, i.e. superclassis modified, all of its subclassesare affected

    c) Subclass can cancelinheritance of methods fromits superclass by redefinitionof inherited method

    +correctMethod()

    +correctMethod()

    Redefinesinheritedmethod, sosubclass of wrong

    superclass

    5.2.2. Nhng vn c bn ca thit k HT

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    13/93

    2008-2009

    Tha k: Rumbaughs Rules

    Cc xung t tha k l do s phn lp khng tt

    Cc cu trc a-kind-of tng qut ha b xm phm. Hoc c ch tha k vi phm nguyn tc ng gi. Vd cc

    lp con truy cp trc tip vo cc thuc tnh v phngthc ca cc lp cha.

    Cc hng dn ca Jim Rumbaugh

    Query operations should not be redefined

    Methods that redefine inherited ones should only restrict the

    semantics of the inherited onesThe underlying semantics of the inherited method should neverbe changed

    The signature (argument list) of the inherited method shouldnever be changed

    5.2.2. Nhng vn c bn ca thit k HT

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    14/93

    2008-2009

    Tha k bi: mt lp con c th tha k nhiu lp chacc kiu xung t tha k tng.

    Ba kiu xung t tha k: Hai phng thc hoc thuc tnh c tha k c cng tn v

    ng ngha.

    Hai phng thc hoc thuc tnh c tha k c tn khcnhau nhng cng ng ngha (ng ngha).

    Hai phng thc hoc thuc tnh c tha k c cng tn

    nhng ng ngha khc nhau (ng m).

    V d:

    5.2.2. Nhng vn c bn ca thit k HT

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    15/93

    2008-2009

    S kt ni: th hin s ph thuc v s lin quan bn trongca cc m un trong h thng ( lp, i tng, v hnh vi).

    Kt ni tng tc gii quyt s kt ni bn trong gia ccphng thc v cc i tng thng qua trao i thng ip

    5.2.3. Tiu ch thit k - S kt ni bn trong

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    16/93

    2008-2009

    Kt ni tng tc: Law of Demeter: ti thiu ha s lng i tng c th

    nhn cc thng ip t mt i tng cho trc. Mi i tng ch nn gi thng ip ti:

    bn thn n i tng c cha ng trong mt thuc tnh ca n

    hoc mt mi tng thuc lp cha ca n. mt i tng trao i nh l tham s i vi phng

    thc. Mt i tng c to ra bi phng thc. Mt i tng c lu trong mt bin ton cc.

    S kt ni s tng nu: Phng thc gi truyn cc thuc tnh cho phng thc b

    gi. Phng thc gi ph thuc vo gi tr phn hi ca

    phng thc b gi

    5.2.3. Tiu ch thit k - S kt ni bn trong

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    17/93

    2008-2009

    Kt ni tha k - Inheritance Coupling cp ti s cht ch trong s kt ni ca cc lp trong

    mt h phn cp tha k. Kt ni tha k mc cao c th khng tt v c th gp

    cc vn v xung t tha k, kh nng nh ngha liv kt ni ng.

    Cc ngn ng lp trnh c cc kh nng khc nhau vmc kt ni tha k.

    Cn phi tm hiu s cn bng gia nhng vn xung

    t (ng gi v n thng tin) vi s kt ni mong mungia lp con v cha.

    Nn s dng tha k khi cn phi h tr vic tng qutha.

    5.2.3. Tiu ch thit k - S kt ni bn trong

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    18/93

    2008-2009

    S gn kt: vic lm cch no gii quyt mt m un bntrong mt h thng. Gn kt phng thc:

    5.2.3. Tiu ch thit k - S gn kt (cohesion)

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    19/93

    2008-2009

    Gn kt lp: s gn kt gia cc thuc tnh v ccphng thc ca lp. (T chc mt lp ntn?).

    Mt lp ch nn biu din cho mt vn (ngi lmthu, n t hng), cc thuc tnh, phng thctrong lp phi biu din cho vn .

    Mt s gn kt lp l tng: Cha ng cc phng thc m c th nhn thy t bn

    ngoi lp. Mi phng thc thc hin mt chc nng

    C cc phng thc tham chiu cc thuc tnh hocphng thc khc c nh ngha vi lp hoc lp cha.

    Khng c s kt ni dng iu khin no gia cc phngthc

    5.2.3. Tiu ch thit k - S gn kt (cohesion)

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    20/93

    2008-2009

    Hai m un (c to ra cng mt thi im) cth ph thuc ln nhau, do khi thay i mt

    m un ny dn n phi thay i m un kia. Gim thiu s ng sinh tng qut bng vic

    hn ch nhng s ng sinh khng cn thit.

    Gim thiu s ng sinh xuyn qua cc ngbin ng gi

    Tng s ng sinh trong cng mt ng bin

    ca gi.

    5.2.3. Tiu ch thit kConnascence - produced at the same time

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    21/93

    2008-2009

    5.2.4. Hot ng thit k i tngAdditional Specification ch tiu b sung

    m bo rng cc lp l cn thit v y cho vic giiquyt bi ton c bn

    Cn phi hon thnh s nhn thy (n hay c th thy)ca cc thuc tnh hoc hnh vi trong mi lp.

    Determine the signature of every method of each class Signature = name, (types of) formal parameters, type of

    result Define constraints to be preserved by objects

    Pre-conditions, post-conditions and invariants via contracts

    Manage constraint violations, e.g. via exceptions

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    22/93

    2008-2009

    5.2.4. Hot ng thit k i tngNhn din c hi s dng li.

    Mu Pattern L mt s kt hp hu ch cc lp cng tc vi nhau nhm

    cung cp mt gii php cho mt bi ton chung.

    Cc mu phn tch: mc ch ch yu l th hin min biton Cc mu thit k: phc v cho mt bi ton thit k chung

    trong mt ng cnh c th.

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    23/93

    2008-2009

    Frameworks - Khung Tp hp cc lp c trin khai v c th c s dng li nh

    l c s nn tng cho vic trin khai mt ng dng.

    Hu ht cc framework cho php to ra cc lp con k tha cc lptrong framework. Cc lp con c to ra s c s ph thuc, kt ni tha k s tng

    gia lp cha v lp con. Do s cn phi bin dch li khi mFramework b thay i bi ngi cung cp.

    Th vin lp: Th vin lp ging nh l mt framework, vi n ta c mt tp hp

    cc lp c trin khai c thit k cho vic s dng li.Framework hng vo min c th hn. Framework c th c

    xy dng bi vic s dng mt th vin lp. Th vin c th dnh c h tr cho cc s l thng k, s hc,

    qun l File, pht trin giao din ngi s dng.

    5.2.4. Hot ng thit k i tngNhn din c hi s dng li.

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    24/93

    2008-2009

    Thnh phn Component: L mu phn mm c ng gi c th c gn vi

    h thng nhm cung cp mt chc nng c th. Hin nay c nhiu thnh phn kh dng, chng c

    trin khai thng qua vic s dng cng ngh Active X.

    Cc thnh phn c th c trin khai thng qua vics dng cc th vin hay framework, tuy nhin ncng c th c s dng xy dng Framework.

    5.2.4. Hot ng thit k i tngNhn din c hi s dng li.

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    25/93

    2008-2009

    Factoring Separate out shared aspects of a method or class into a

    new method or class New class related to existing classes via inheritance,

    aggregation or association

    Normalization Identifies classes missing from the design All association and aggregation relationships converted to

    attributes of the class

    Challenge inheritance relationships to ensure they onlysupport a generalization/specialization semantics

    5.2.4. Hot ng thit k i tngCu trc li thit k - Restructuring the Design

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    26/93

    2008-2009

    Simple optimisations make an existing design moreefficient, but must not make optimised design significantlymore difficult to reason about, i.e. optimsation is a trade-

    off between efficiencyand ease of understanding[1] access paths between objects A message from one object to another may go though many

    intervening objects

    If a) the path though the intervening objects is long and b) themessage is sent frequently consider a redundant path, i.e. anadditional attribute in the calling object that stores a directconnection to object at end of path

    [2] attributes of each class If methods that use an attribute are only read and update

    methods, and only instances of a single class send thosemessages then attribute may belong to calling class.

    5.2.4. Hot ng thit k i tngTi u thit k - Optimizing the Design

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    27/93

    2008-2009

    [3] direct and indirect fan-out of each method Fan-out = number of messages sent by a method Direct Fan-out = number of messages sent by given method

    Indirect Fan-out includes number of messages sent bymethods called by other methods in a message tree If fan-out of a method significantly higher than other methods in

    a system then should be optimised, e.g. add an index toattributes used to send messages to objects in message tree

    [4] execution order of statements In frequently used methods, order of statements may be non-

    optimal Changing order should not affect result of computation

    Changing order can be more efficient, e.g. when searching, ifone attribute known to narrow search then search on thatattribute first

    [5] unnecessary recomputation Derived attributes (or active values) need not be recomputed

    until attributes that are involved in its computation change, e.g.

    5.2.4. Hot ng thit k i tngOptimizing the Design

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    28/93

    2008-2009

    OO designs assume class-based OO programminglanguage used to implement those designs

    Need to resolve any conflicts between constructsassumed in the design to be available in programminglanguage but are not present in that language e.g. [1] design uses multiple inheritance, language has only

    single inheritance [2] implementation language is object-basedand not

    class-based, i.e. implementation language supportsobject creation but not implementation inheritance

    [3] language is of another (less expressive) languageparadigm, e.g. C or Pascal are of the imperative-procedural paradigm

    5.2.4. Hot ng thit k i tngMapping Problem Domain Classes to Implementation Languages

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    29/93

    2008-2009

    [1] design uses multiple inheritance, language has onlysingle inheritance

    A) Convert multiple inheritance relationships to superclass-subclassassociation relationships If additional superclasses are concrete (objects of the class can be

    instantiated) then multiplicity from superclass to subclass is 0..1 Otherwise multiplicity is 1..1 Add an exclusive-OR constraint between associations Add appropriate methods to ensure all information remains available to

    existing class

    SuperClass1-attribute1

    -attribute2

    SuperClass2-attribute3

    -attribute4

    SuperClass3-attribute7

    -attribute8

    Class1-attribute5

    -attribute6

    Class2-attribute5

    -attribute6

    concrete

    0..*

    1..1

    0..*

    1..1

    {XOR}[1]All classes preserved[2]Increases amountof message passing[3]Adds to processing

    requirement due to XOR

    5.2.4. Hot ng thit k i tngMapping Problem Domain Classes to Implementation Languages

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    30/93

    2008-2009

    B) Copy ALL the attributes and methods of the additionalsuperclasses to ALL the subclassess and removeadditional superclass from the design

    SuperClass1-attribute1

    -attribute2

    SuperClass2-attribute3

    -attribute4

    SuperClass3-attribute7

    -attribute8

    Class1

    -attribute5-attribute6

    Class2

    -attribute5-attribute6

    -attribute3-attribute4

    -attribute3-attribute4

    PotentialInheritanceConflicts

    abstract

    5.2.4. Hot ng thit k i tngMapping Problem Domain Classes to Implementation Languages

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    31/93

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    32/93

    2008-2009

    SuperClass1-attribute1-attribute2

    SuperClass2-attribute3-attribute4

    SuperClass3-attribute7-attribute8

    Class1

    -attribute5-attribute6

    Class2

    -attribute5-attribute6

    -attribute3

    -attribute4

    -attribute3

    -attribute4

    If all superclasses are

    abstract

    -attribute7-attribute8

    -attribute7-attribute8

    -attribute1-attribute2

    -attribute1-attribute2

    PotentialInheritanceConflicts

    5.2.4. Hot ng thit k i tngMapping Problem Domain Classes to Implementation Languages

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    33/93

    2008-2009

    [3] language is of another (less expressive) languageparadigm, e.g. C or Pascal are of the imperative-procedural paradigm

    Book suggests never attempt this! And says Beyond thescope of this text

    In fact, it is possible to convert depending on the non-OO

    language used, e.g. using pointers in C it is possible toemulate object-oriented language constructs

    More difficult, but not impossible, in a modular variant ofan imperative-procedural language e.g. Pascal, but

    inheritance must be factored-out

    5.2.4. Hot ng thit k i tngMapping Problem Domain Classes to Implementation Languages

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    34/93

    2008-2009

    Contract: formalises interactions between client and server objects

    is a set of constraints and guarantees

    Constraints may bepre-conditions,post-conditions orinvariants

    Pre- and post-conditions model constraints on individual

    methods, and contracts establish pre- and post-conditions for a method to execute correctly. Pre-condition is constraint which must be met for a method

    to execute

    Post-condition is constraint that must be met after methodexecutes. Otherwise effect of method execution must beundone

    5.2.5. Cc rng buc v tha thun

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    35/93

    2008-2009

    Invariants model constraints (that must always be true)for all instances of a class, e.g. Domains or types of attributes

    Multiplicity of attributes

    Legal values of attributes including attributes that modelassociation and aggregation and relationships

    e.g. if association relationship is required, invariant is createdto enforce relationship to have valid value for instance to exist

    Invariants usually attached to the class, e.g. CRC cardsor class diagrams as a set of assertions

    5.2.5. Cc rng buc v tha thun

    Attribute Constraint:

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    36/93

    2008-2009

    5.2.5. continued

    Example:-

    Back:

    Order Number (1:1) (unsigned long)Attributes:

    Date (1..1) (Date)

    Sub Total (0..1) (double)Tax (0..1) (double) {Sub Total = sum(Product Order.GetExtension())}Shipping (0..1) (double)Total (0..1) (double)Customer (1..1) (Customer)

    Cust ID (1..1) (unsigned long) (Cust ID = Customer.GetCustID()}State (1..1) (State)StateName (String) {State Name = State.GetState()}

    Relationships:Generalisation(a-kind-of):

    Aggregation (has-parts):

    Other Associations: Product{1..*} Customer(1..1} State{1..1}

    Attribute Constraint:Order Number IS unsigned long

    Attribute Constraint:Customer IS INSTANCE OFCustomer Class

    Attribute Constraint:Cust ID IS unsigned

    LongANDInvariantMUST HAVE one andONLY one value(1..1) =

    GetCustID()Customer.

    Constraint: Instance of Order EXISTSIF (instance of Customer class AND

    instance of State class AND>=1 instance of Product class)

    ASSOCIATED WITH order object

    Product Order

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    37/93

    2008-2009

    5.2.5. continuedInvariants On a Class Diagram(Not recommended Use CRC card for Invariants)

    Order

    -Order Number: unsigned long-Date[1..]: Date-Sub Total[0..1]: double-Tax[0..1]:double-Shipping[0..1]:double

    -Total[0..1]double-Tax[0..1]: double-Customer[1..1]: Customer-Cust ID[1..1]: unsigned long-State[1..1] State-State Name[1..1: String

    Customer

    -Cust ID-Last Name-First Name

    1..1 0..*

    Product

    -Product Number-Product Desc-Price0..* 1..*

    State

    -StateTax RateProduct Desc

    0..* 1..1

    Product Order

    OrderProduct

    QuantityExtension

    {Cust ID=Customer.GetCustID()}

    {State Name=State.GetState()}

    {Tax=State.GetTaxRate()

    *SubTotal}

    {Sub Total=

    Sum(Product Order.GetExtension())}}

    5 2 5 Cc rng buc v tha thun

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    38/93

    2008-2009

    Cc tha thun miu t thng dip trao i gia cc itng, v mi thng ip c to ra cho mi tng tc

    Trn thc t, mi tha thun c to ra cho miphng thc m phng thc nhn cc thng ip tcc i tng khc.

    Tha thun cha ng thng tin cn thit cho lp trnh

    vin hiu phng thc lm g. Cc thng tin bao gm: Method name, class name, ID number, client objects,

    associated use cases, description, arguments received,type of reurn value, pre- and post-conditions

    Tha thun khng cha ng thng tin miu t thut tonca phng thc.

    5.2.5. Cc rng buc v tha thunTha thun

    5 2 5 C ti dUnique Contract ID

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    39/93

    2008-2009

    5.2.5 Continued. Specific Method OF specific Classq

    List of classes and methodsthat send a message to this

    methodList of use cases containingthis method where method isused to realise implementationof the use case (from serverclasss CRC card and associated

    use cases)

    What the method does NOT how it does it

    types of parameters passed

    type of value returned to its clients

    = method signature

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    40/93

    2008-2009

    5.2.6. Method Specification

    Bn ghi chi tit phng thc L mt ti liu vit bao gm cc ch dn tng mnh lm

    th no vit lnh trin khai phng thc. Bn ghi chi tit cn phi r rng v d hiu lp trnh

    vin c th da vo vit m lnh. Mt bn ghi gm 4 thnh phn;

    Thng tin chung: cha ng cc thng tin tng qut v dtn ca phng thc, ten ca lp, Idnumber, Contract ID Cc s kin: Cc s kin kch hot phng thc (nhp

    chut, n phm).

    Thng ip trao i: miu t phng thc ti v i tphng thc. Chi tit thut ton: c vit theo kiu ting Anh c cu

    trc hoc Cc lnh gi lp.

    5.2.6. Continued General Information (for managing programming effort)

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    41/93

    2008-2009

    5 6 Co t uedMethod Name Class Name ID:

    Contract ID Programmer: Date Due:Programming Language: Visual Basic SmallTalk C++ Java

    Triggers/Events

    Arguments Received:Data Type:

    Notes:

    Messages Sent & Arguments PassedClassName.MethodName: Data Type: Notes:

    Argument Returned:Data Type:

    Notes:

    Algorithithm Specification:

    ( g g p g g )

    e.g. user initiated event(mouse event, keystroke event),system event or event initiated by another method

    Message passing to and from method(c.f. sequence and collaboration diagrams)

    Arguments = attributes and data structuresin implemented method

    See next slide

    5 2 6 Continued

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    42/93

    2008-2009

    5.2.6. Continued Structured English will suffice for algorithm specification

    Algorithm Specification:

    Action Statement Examples: Profits = Revenues ExpensesGenerate Inventory-Report

    IF Statement Examples: IF Customer NOT in Customer Object StoreTHENAdd Customer record to Customer Object StoreELSEAdd Current-Sale to Customers Total SalesUpdate Customer record in Customer Object Store

    FOR Statement Examples: FOR all Customers in Customer Object Store DOGenerate a new line in Customer-Report

    Add Customers Total-Sales to Report-Total

    CASE Statement Examples: CASE

    IF Income < 10,000: Marginal-tax-rate = 10 percentIF Income < 20,000: Marginal-tax-rate = 20 percentIF Income < 30,000: Marginal-tax-rate = 31 percentIF Income < 40,000: Marginal-tax-rate = 35 percentELSE Marginal-tax-rate = 38 percent

    ENDCASE

    Notes:

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    43/93

    CD S l ti

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    44/93

    2008-2009

    Revised CD Selections Class Diagram

    CD Selection

    f C C C C

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    45/93

    2008-2009

    Back of CD CRC Card

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    46/93

    2008-2009

    Get Review Method Contract

    R i d P k Di

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    47/93

    2008-2009

    Revised Package Diagram

    Ch 5 Thit k h th

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    48/93

    2008-2009

    Chng 5. Thit k h thng

    5.1 Chuyn sang thit k

    5.2 Thit k lp v phng thc

    5.3 Thit k lp qun l d liu5.4 Thit k lp tng tc ngi-my

    5.5 Thit k lp kin trc vt l

    5 3 Thit k lp q n l d li

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    49/93

    2008-2009

    5.3 Thit k lp qun l d liu

    5.3.1 Gii thiu

    5.3.2 Cc nh dng lu tr d liu

    5.3.3 nh x cc i tng ti cc nh dng lutr i tng.

    5.3.4 Ti u ha lu tr i tng da trn

    RDBMS5.3.5 Thit k lp qun l v truy cp d liu

    5.3.6 CD Selection

    5 3 1 Gii thiu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    50/93

    2008-2009

    5.3.1 Gii thiu

    Design of data management layer: [1] Select format of storage; Alternatives are:

    Files (Sequential and Random) Relational databases Object-relational databases Object-oriented databases

    [2] Map problem domain classes to selected storageformat Problem domain objects drive object storage design

    [3] Optimise storage for efficient performance

    [4] Design appropriate data access classes andmanipulation classes to: Separate problem domain classes from storage format Handle all communication with the database

    5 3 2 Cc nh dng lu tr d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    51/93

    2008-2009

    5.3.2 Cc nh dng lu tr d liu

    C 4 kiu nh dng lu tr i tng: Files (tp tin),CSDL hng i tng, CSDL quan h i tng vCSDL quan h.

    File: tp tin (d liu in t phc v cho cng vic cth). CSDL la tp hp nhm cc thng tin m c quan h vi

    nhau theo mt cch no .

    H thng qun l CSDL (Database Management System:DBMS) l phn mm m n to ra v qun l CSDL

    5.3.2 Cc nh dng lu tr d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    52/93

    2008-2009

    Tp tin truy cp tun t: Ch cho php cc hot ng tp tin tun t c thc

    hin (c, ghi v tm kim), khng hiu qu cho vic tm

    kim v cp nht. Tp tin truy cp tun t ko theo th t.

    Data is in sequence that it was written to the file, i.e. data isorganised serially.New data is added to end of file

    Tp tin truy cp tun t theo th t. Data is in sorted order, e.g. ascending order of some

    component in the dataTp tin truy cp ngu nhin. Cho php cc hot ng ngu nhin hoc trc tip. Hiu qu cho tm kim v cp nht mt i tng c th. Khng hiu qu cho vic vit cc bn bo co.

    g Tp tin

    5.3.2 Cc nh dng lu tr d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    53/93

    2008-2009

    X l ngu nhin v tun t Sometimes need both sequential and random processing

    Can use a sequential file with additional list of keys Key values are data items in which file is to kept in sorted

    order

    Actual data is kept in random access file

    Minimises cost of additions and deletions to sequential file Allows random file to be processed in sequential order

    Also allows fast random processing via random file alone

    More effective if both sequential AND random processingto use a database

    g Tp tin

    5.3.2 Cc nh dng lu tr d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    54/93

    2008-2009

    Cc kiu tp tin ng dng Master Files

    Accumulate data, e.g. data from transaction files Maintained for long time periods and data for new entries is appended to

    end of file Look-up files

    Remain unchanged over time, usually used to validate data fromelsewhere against stored legal values

    Transaction files Update masterfiles with transaction information May or may not be disposed of after masterfile has been updated

    Audit file Contains data relating to changes in other data, e.g. data values before

    and after each change in those values, the identity and authority of the

    changer History file Archive of data from another file no longer needed by systems that

    process the original file

    gTp tin

    5.3.2 Cc nh dng lu tr d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    55/93

    2008-2009

    CSDL phn tn da trn mt b cc bng vi mi bngcha ng mt t kha chnh (trng), mi gi tr ca nch l duy nht ti mi hng.

    Cc bng quan h vi nhau bi vic t cc t khachnh t bng ny ti bng c quan h nh l t khangoi (foreign key).

    Cc h thng qun l CSDL u h tr s ton vn thamchiu (referential integrity), m bo cc gi tr lin ktcc bng vi nhau thng qua t kha chnh v khangoi l ng n v ng b.

    C s d liu phn tn

    5.3.2 Cc nh dng.. - C s d liu phn tnCust ID is p

    rimary key

    Payment Typeis primary

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    56/93

    2008-2009

    Cust ID is primary keyof Customer table

    Order Numberis primarykey of Ordertable

    key of Payment

    Type table

    Order Numberis foreign

    key in Order

    Payment Typeis foreignkey in Order

    5.3.2 Cc nh dng lu tr d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    57/93

    2008-2009

    Tables related through Cust ID

    All Payment Typevalues in Ordermust exist first inPayment Type table

    All Cust ID

    Values inOrder mustexist first inCustomerTable

    Attempt to add order:

    241 11/23/00 1111

    OrderNumber

    Cust ID

    DBMS: Invalid Cust ID value

    C s d liu phn tn

    5.3.2 Cc nh dng lu tr d liuC d li h t

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    58/93

    2008-2009

    Database Management System (DBMS) RDBMS is a DBMS for a Relational Database SQL (Structured Query Language) Ngn ng truy vn c

    cu trc l ngn ng cho vi to, xa bng v truy cpd liu trong cc bng.

    SQL hot ng trn ton b bng ch khng phi tnghng trong bng

    Khi cc truy vn phi cha ng thng tin t nhiu bng,cc bng u tin c kt hp li vi nhau da trn ccmi quan h t kha chnh v t kha ngoi

    Cc i tng cn phi c bin i mt cch ph hpcho vic lu tr trong bng.

    C s d liu phn tn

    5.3.2 Cc nh dng lu tr d liuC d li h t

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    59/93

    2008-2009

    Object-Relational DBMSs ORDBMS: L H thng qun l CSDL quan h cng vi cc

    m rng dnh cho vic x l lu tr i tng trong cu trcbng quan h. iu ny c thc hin thng qua cc kiuc nh ngha bi ngi dng. Example: Create a mapdata type.

    Object-Oriented Database Management Systems(OODBMSs)

    Xy dng c bng cch A) B sung cc m rng ti mt ngn ng lp trnh hng i

    tng.

    B) Xy dng mt h thng qun l d liu hon ton tch bit. The Object Data Management Group (ODMG) is standardising

    process for defining Object Definition Language(ODL), ObjectManipulation Language (OML) and Object Query Language (OQL)

    C s d liu phn tn

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    60/93

    5.3.2 Cc nh dng lu tr d liuLa chn nh dng lu tr d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    61/93

    2008-2009

    Cc tiu ch (Data) types to be supported Types of application systems to be constructed

    (transaction processing, Decision Support System, ) Existing Storage Formats Future Needs

    Other miscellaneous criteria (cost, concurrency control,)

    La chn nh dng lu tr d liu

    5.3.3. nh x cc i tng ti cc nh dng lu tri tng nh x i tng ti nh dng OODBMS

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    62/93

    2008-2009

    i tng nh x i tng ti nh dng OODBMS

    If OODBMS is to be used: Each concrete problem domain class has corresponding object persistence class Each object persistence class has a DAM (Data Access and Management) class that manages

    interaction between object persistence class and problem domain layer

    Unchanged fromanalysis phase

    New Class

    New Class

    Dependent onMust be able to read fromand write to the OODBMS

    5.3.3. nh x cc i tng ti cc nh dng lu tri tng nh x i tng ti nh dng OODBMS

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    63/93

    2008-2009

    Nu s a tha k c s dng trong min bi tonnhng ko c h tr bi OODBMS Cc a tha k phi c phn mnh thnh cc lp

    OODBMS. Lut phn mnh cho mi trng hp a tha k. Lut 1a. Thm thuc tnh ti lp OODBMS biu din

    cc lp cha b sung. C s lin kt ca lin kt mi t lp con n lp cha l 1..1 Thm thuc tnh cha ng danh tnh i tng Object ID ca cc

    th hin c lu tr trong lp OODBMS (biu din lp con) ti cclp OOBMDS biu din cc lp cha.

    Nu lp cha l c th, c s lin kt t lp cha ti lp con l 0..*(trong trng hp khc l 1..1)

    Thm rng buc exclusive OR (Xor) vo gia cc lin kt

    Lut 1b. Lm phng h thng tha k. Copy cc thuc tnh ca cc lp cha ODBMS xung tt c cc lp

    con. Loi b cc lp cha b sung

    i tng nh x i tng ti nh dng OODBMS

    Example:-

    Assume

    Attribute represents subclass that will contain an Object IDof instance stored in OODBMS class that representsadditional superclasses

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    64/93

    2008-2009

    Assume

    Superclass2is concrete

    Add attribute toOODBMS class

    additional superclasses

    Add attribute toOODBMS class

    Add pair of attributesTo represent associationWith Class1ODBMS &

    Class2ODBMS

    Add associations betweenClass2OODBMS and

    Superclass2OODBMS,Class1OODBMS andSuperclass2ODBMSwith correct multiplicities

    XOR

    Shown XOR

    constraintClass 1Inherits fromSuperclass1 andSUperclass2

    By rule 1)b)

    5.3.3. nh x cc i tng ti cc nh dng lu tri tng nh x i tng ti nh dng OODBMS

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    65/93

    2008-2009

    u im ca Lut 1: lu gi c trong CSDL tt c cclp min bi ton c xc nh ti pha phn tch. iuny cho php s linh hot ln nht cho vic qun l (duytr) thit k ca tng qun l d liu. Tuy nhin Lut 1 lm tng s lng thng ip trao i v

    cn phi x l thm quan h XOR (lm gim hiu qu cathit k)

    Khi khi to cc i tng min bi ton t cc itng OODBMS, cc x l khc s cn n. Tin trnh x l b sung s l vic thu thp cc i tng

    OODBM v a cc phn t ca n to i tng min

    bi ton. khi lu tr i tng min bi ton, mt tin trnh b sung

    c s dng chuyn i cc i tng OODBMS

    g g g

    5.3.3. nh x cc i tng ti cc nh dng lu tri tng nh x i tng ti nh dng ORDBMS

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    66/93

    2008-2009

    vic nh x cc i tng min bi ton ti cc itng qun l d liu phc tp hn nhiu khi s dngnh dng lu tr ORDBMS.

    Ph thuc vo mc h tr trong ORDBMS cho OOm cc lut nh x khc nhau s c s dng. Gi thit ORDBMS h tr :

    Object ID.

    Cc thuc tnh a gi tr (Multivalued attributes) Cc th tc lu tr (Stored procedures) NHNG khng h tr cho vic tha k

    Rules for mapping from problem domain objects to

    RDBMS-based data-access and management layertables

    g g g

    5.3.3. nh x cc i tng ti cc nh dng lu tri t

    ng nh x i tng ti nh dng ORDBMS

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    67/93

    2008-2009

    g g g

    5.3.3. nh x cc i tng ti cc nh dng lu tr itng nh x i tng ti nh dng ORDBMS

    6. Repeating groups

    9. Patient Table hasP ib

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    68/93

    2008-2009

    1. Concrete problem domainclasses mapped to tablesin ORDBMS

    n.b. Person class is abstractbut has multiple direct

    subclasses (in fulldiagram) so is also mapped

    2. Single valued attributes mappedto columns in ORDBMS tables

    Amount attribute ofPatient class included

    in Patient Table class

    3. Methods and derivedattributes mapped tostored procedures OR

    program modules

    4. Single-valued aggregationand association relationshipsmapped to a column storing

    an Object ID

    5. Multi-valuedattributesmapped to columns with

    a set of values

    p g g pused to create new table

    7.Map multivaluedaggregation and

    association relationshipsto columns storing setof Object IDs

    Stores multipleObjectIDsTo Patient

    Table objects

    Stores multipleObjectIDsTo SymptomTable objects

    8. Map one-to-many

    and many-to-onerelationships

    Multiple OIDsTo Appt. TableObjects

    Single OIDTo Patient Table

    Object

    Person attribute,

    Person Tablehas SubClassObjects

    Single OIDTo Patient Table

    Object

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    69/93

    5.3.3. nh x cc i tng ti cc nh dng lu tri tng nh x i tng ti nh dng RDBMS

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    70/93

    2008-2009

    Rule 1: Map all concrete problem domain classes to the RDBMStables.

    Rule 2: Map single valued attributes to columns of the tables. Rule 3: Map methods to stored procedures or to program modules.

    Rule 4: Map single-valued aggregation and association relationshipsto a column that can store the key of the related table Rule 5: Map multi-valued attributes and repeating groups to new

    tables and create a one-to-many association from the original table tothe new ones.

    Rule 6: Map multi-valued aggregation and association relationshipsto a new associative table that relates the two original tablestogether. Copy the primary key from both original tables to the newassociative table

    Rule 7: For aggregation and association relationships of mixed type,copy the primary key from the single-valued side (1..1 or 0..1) of therelationship to a new column in the table on the multi-valued side (1..*or 0..*) of the relationship that can store the key of the related table

    Rule 8a: Ensure that the primary key of the subclass instance is thesame as the primary key of the superclass..OR

    Rule 8b: Flatten the inheritance

    2. Map single valued

    5.3.3. nh x cc i tng ti cc nh dng lu tr i tng nh x i tng ti nh dng RDBMS

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    71/93

    2008-2009

    1. Concrete problem domainClasses mapped to tables (also PDclass IF multiple directSubclasses)

    2. Map single valued

    attributes tocolumns

    3. Apply Rule 3

    4. Apply Rule 4

    5. Apply Rule 5 6. etc

    5.3.4. Ti u ha lu tr i tng da trn RDBMSTi u hiu qu lu tr

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    72/93

    2008-2009

    2 chiu c bn cho vic ti u ha CSDL quan h: Hiu qu lu tr

    v tc truy cp.

    Bng d liu tt: Khng c d liu d tha

    Lng ph khng gian b nh

    Tng kh nng gp li.

    t gi tr Null Mt gi tr trng trong bn c th c hiu thnh nhiu

    ngha n s rt kh phin dch

    5.3.4. Ti u ha lu tr i tng da trn RDBMSV d d liu khng chun ha

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    73/93

    2008-2009

    Repeated customer information(name, state) for each order

    Null values in product columnsif order < 3 items

    If customer moves state,need program to update

    ALL instances

    Null = [1] 1 or 2 products only[2] Error: items missing

    [3] Part of order cancelled

    Problem is: Wasted storage space + data integrity threat

    5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    74/93

    2008-2009

    Chun ha d liu l qu trnh p dng mt chui cc nguyn tc ivi cc bng RDBMS nhm xc nh cc bng c cu trc tt ntn.

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    75/93

    Original Model Primary Key

    for Order

    5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu -Create new Product Order Table

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    76/93

    2008-2009

    g

    Order

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long

    -Last Name: String-First name: String-State: String-Tax Rate: float-Product1 Number: unsigned long

    -Product1 Desc.: String-Product1 Price: double-Product1Qty.: unsigned long-Product2 Number: unsigned long-Product2 Desc.: String

    -Product2 Price: double-Product2 Qty.: unsigned long-Product3 Number: unsigned long-Product3 Desc.: String-Product3 Price: double

    -Product3 Qty.: unsigned long

    Revised ModelOrder

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long-Last Name: String-First name: String-State: String-Tax Rate: float

    Primary Key for Order

    Product Order

    -Order Number: unsigned long-Product Number: unsigned long-Product Desc.: String-Product Price: double-Product Qty.: unsigned long

    0..*

    1..*

    Primary Key forProduct Order

    Foreign Key inProduct Order

    = 1NF

    (zero tomany)

    5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    77/93

    2008-2009

    Order TableOrder Date Cust Last First State TaxNumber ID Name Rate239 11/23/00 1035 Black John MD 0.05260 11/24/00 1035 Black John MD 0.05

    273 11/27/00 1035 Black John MD 0.05241 11/23/00 1123 Williams Mary CA 0.08262 11/24/00 1123 Williams Mary CA 0.08287 11/27/00 1123 Williams Mary CA 0.08290 11/30/00 1123 Williams Mary CA 0.08234 11/23/00 2242 DeBerry Ann DC 0.065237 11/23/00 2242 DeBerry Ann DC 0.065

    238 11/23/00 2242 DeBerry Ann DC 0.065245 11/24/00 2242 DeBerry Ann DC 0.065250 11/24/00 2242 DeBerry Ann DC 0.065252 11/24/00 2242 DeBerry Ann DC 0.065253 11/24/00 2242 DeBerry Ann DC 0.065297 11/30/00 2242 DeBerry Ann DC 0.065

    243 11/23/00 4254 Bailey Ryan MD 0.05246 11/23/00 4254 Bailey Ryan MD 0.05248 11/24/00 4254 Bailey Ryan MD 0.05235 11/23/00 9500 Chin April KS 0.05242 11/23/00 9500 Chin April KS 0.05244 11/24/00 9500 Chin April KS 0.05251 11/24/00 9500 Chin April KS 0.05

    ? Products

    5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    78/93

    2008-2009

    Product Order Table Order Product Product Product Product

    Number Number Desc. Price Qty

    339 555 Cheese Tray $45.00 2

    260 444 Wine Gift Pack $60.00 1 273 222 Bottle Opener $12.00 1

    241 444 Wine Gift Pack $60.00 2

    262 222 Bottle Opener $12.00 2

    287 222 Bottle Opener $12.00 2

    290 555 Cheese Tray $45.00 3 234 555 Cheese Tray $45.00 2

    237 111 Wine Guide $15.00 1

    237 444 Wine Gift Pack $60.00 1

    Order 237 has two products

    New Design: [1] Eliminates nulls[2] Unlimited number of products per order

    5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    79/93

    2008-2009

    2NF (Second Normal Form) M hnh d liu nh dng 1NF V Bng d liu cha ng cc trng m ch ph thuc vo mt kha

    chnh hon chnh Gi tr kha chnh ca mi bn ghi c th xc nh gi tr ca tt c cc

    trng khc trong bn ghi. Nu cc trng ph thuc ch mt phn ca kha chnh (ph thuc b phn),

    nhng trng ny nn c t trong mt bng khc

    Product Order Table Order Product Product Product Product

    Number Number Desc. Price Qty

    339 555 Cheese Tray $45.00 2 260 444 Wine Gift Pack $60.00 1

    273 222 Bottle Opener $12.00 1

    241 444 Wine Gift Pack $60.00 2

    262 222 Bottle Opener $12.00 2

    287 222 Bottle Opener $12.00 2

    290 555 Cheese Tray $45.00 3

    234 555 Cheese Tray $45.00 2 237 111 Wine Guide $15.00 1

    237 444 Wine Gift Pack $60.00 1

    248 222 Bottle Opener $12.00 1

    248 333 Jams and Jellies $20.00 2

    248 111 Wine Guide $15.00 1

    Only depend on Product NumberPrimary Key

    Need Order Number

    + Product Number

    To find Qty

    = 2NF5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    80/93

    2008-2009

    Create new table to store product information

    Order

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long-Last Name: String-First name: String-State: String-Tax Rate: float

    Product Order

    -Order Number: unsigned long-Product Number: unsigned long-Product Desc.: String-Product Price: double-Product Qty.: unsigned long

    0..*

    1..*Product

    -Product Number: unsigned long-Product Desc.: String-Price: double

    Order

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long-Last Name: String-First name: String-State: String-Tax Rate: float

    0..*

    1..*

    -Order Number: unsigned long-Product Number: unsigned long-Qty.: unsigned long

    First and Last name

    Only dependOn Cust ID

    NOT whole key

    Customer

    -Cust ID: unsigned long-Last Name: String-First name: String

    1..1

    0..*

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long-State: String-Tax Rate: float

    5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    81/93

    2008-2009

    Create new table to store product information

    Order

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long-Last Name: String-First name: String-State: String-Tax Rate: float

    Product Order

    -Order Number: unsigned long-Product Number: unsigned long-Product Desc.: String-Product Price: double-Product Qty.: unsigned long

    0..*

    1..*Product

    -Product Number: unsigned long-Product Desc.: String-Price: double

    Order

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long-Last Name: String-First name: String-State: String-Tax Rate: float

    0..*

    1..*

    NOT whole key

    = 2NF5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    82/93

    2008-2009

    Create new table to store product information

    Product Order Product

    -Product Number: unsigned long-Product Desc.: String-Price: double

    Order

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long-Last Name: String-First name: String-State: String-Tax Rate: float

    0..*

    1..*

    -Order Number: unsigned long-Product Number: unsigned long-Qty.: unsigned long

    First and Last name

    Only dependOn Cust ID

    NOT whole key

    Customer

    -Cust ID: unsigned long-Last Name: String-First name: String

    1..1

    0..*

    = 2NF5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    83/93

    2008-2009

    Create new table to store product information

    Product Order Product

    -Product Number: unsigned long-Product Desc.: String-Price: double

    Order

    0..*

    1..*

    -Order Number: unsigned long-Product Number: unsigned long-Qty.: unsigned long

    Only dependOn Cust ID

    Customer

    -Cust ID: unsigned long-Last Name: String-First name: String

    1..1

    0..*

    -Order Number: unsigned long-Date: Date-Cust ID: unsigned long-State: String-Tax Rate: float

    5.3.4. Ti u ha lu tr i tng da trn RDBMSChun ha d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    84/93

    2008-2009

    3NF (Third Normal Form) Model IS IN 1NF AND IS IN 2NF AND

    tables in model do not have fields thatare dependent on non-primary key

    fields = No Transitive Dependency

    Order-Order Number: unsigned long-Date: Date

    -Cust ID: unsigned long-State: String-Tax Rate: float

    Depends on

    5.3.4. Ti u ha lu tr i tng da trn RDBMSNhng vn vi RDBMS

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    85/93

    2008-2009

    Creating new tables to optimise for minimum storage requirements= Separate access to tables

    To access data in multiple tables, tables must bejoined This can result in many database operations and lead to very large tables

    and slow processing

    Customer

    -Cust ID: unsigned long-Last Name: String

    -First name: String

    Order Number: unsigned long-Date: Date

    -Cust ID: unsigned long-State: String

    Order

    To print out customers orders need to join CustomerTable with Order Table ON Cust ID field

    Joins can be (computationally) expensive IF tables are large and/or manytables to be joined = slow processing

    10,000 products, 25,000 customers, 100,000 orders X 3 products per order

    Regional differences in music taste:QUERY = products ordered X state of customers = 300,000 row table

    = no. products ordered X total no. columns in tables combined (11)

    5.3.4. Ti u ha lu tr i tng da trn RDBMSTi u tc truy cp

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    86/93

    2008-2009

    Denormalization Adds data from one table to another inorder to speed processing and eliminate a join operation

    Example: Add customer last name to order table to avoidjoining order to customer to get just last name

    5.3.4. Ti u ha lu tr i tng da trn RDBMSTi u tc truy cp -Denormalization Candidates

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    87/93

    2008-2009

    [1] Lookup TablesTables of value, e.g. of product descriptions, payment types

    Codes rarely change include in main table so query does not involve join

    [3] Include a parent entitys attributes in its child entity on thephysical data model

    Payment Type-Payment Type (PK): String-Paymment Description: String

    Order-Order Number(PK): unsigned long-Date: Date-State(FK): String-Cust ID(FK): unsigned long

    -Customer Last Name: String-Payment Type (FK): String-Paymment Description: String

    Payment Description inPayment Type AND Order

    [2] One-to-one relationships

    If values from table often accessed together need join each timeCan combine tables (no join) or include some attributes

    5.3.4. Ti u ha lu tr i tng da trn RDBMSTi u tc truy cp -Clustering and Indexing

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    88/93

    2008-2009

    Clustering Interfile clustering

    Arrange records on storage media so that similar recordsare stored close together

    Indexing An indexin data storage is like an index in the back of a

    textbook;

    Index is minimised table containing values from one ormore columns in a table + location of the values withintable.

    A query can use an index to find the locations of only those

    records that are included in the query answerand A table can have an unlimited number of indexes (but too

    many can add overhead)

    5.3.4. Ti u ha lu tr i tng da trn RDBMSTi u tc truy cp Indexing example

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    89/93

    2008-2009

    S th l f th idth f h l

    5.3.4. Ti u ha lu tr i tng da trn RDBMSc lng kch thc khng gian b nh

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    90/93

    2008-2009

    Sum the values of the average width of each column(field) to find total record size

    Add overhead (vendor may provide an estimate)

    Estimate the number of records you plan to have inthe database

    5.3.4. Ti u ha lu tr i tng da trn RDBMSc lng kch thc khng gian b nh

    V d

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    91/93

    2008-2009

    V d

    D i d t d i l ti l

    5.3.5. Thit k cc lp qun l v truy cp d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    92/93

    2008-2009

    Design data access and manipulation classes

    Prevent data management functionality from becomingpart of problem domain classes

    Four Problem Domain Classes three are concrete

    5.3.5. Thit k cc lp qun l v truy cp d liu

  • 8/9/2019 OOD-2009-TDD-Chuong 10

    93/93

    2008-2009

    Four Problem Domain Classes, three are concrete = three DAM classes supporting 1:1 translation between

    concrete Problem Domain classes and ORDBMS tables