Professional Documents
Culture Documents
Implement Ac I On
Implement Ac I On
Implement Ac I On
18/06/12
Design Patterns:
q q q q q q q q
18/06/12
Class Association:
n n n n
18/06/12
Give a formal/informal description of the functionality of an operation. Useful in reducing misunderstanding and allows splitting the workload. Which class/subsystem to start.
18/06/12
During design:
q
Detailed information about the method header are defined: n Returned Type, Parameter Type; n Accessibility. What should the method do? What are acceptable values for the parameter? Etc
18/06/12
Operation Contract
n n
Writing the operation contract is one possible way to define the functionality. Define the precondition:
q
Express the constraint regarding the attributes of the class and the actual parameter. Express the effect of an operation:
n n n
18/06/12
Precondition: - amt > 0 - amt <= balance Postcondition: - New balance = Old balance - amt
18/06/12
Order of Implementation
n
Starts with high-level components. Then continue to implement the dependent components. Starts with the lower level classes, i.e., classes with least dependency. Then proceed to code classes that make use of the implemented classes.
Bottom-Up:
n
18/06/12
Example
A
methodA() myB
B
methodB()
{ myB.methodB(); }
may be an attribute in class A of class B; The navigability is one possible indicator of the dependency.
myB
PAW de Diseo a Implementacin 9
18/06/12
Class A represents a higher level component as it makes use of other classes. Starting with high level components enables early validation of the overall system. Need temporary implementation (known as stub) for lower classes. Example: n A temporary implementation of methodB() is needed for the implementation of methodA().
PAW de Diseo a Implementacin 10
Disadvantages:
q
18/06/12
Bottom Up Implementation
n n
Low level classes can usually stand alone. No need for stub. Have to postpone the generation of a complete executable program.
Disadvantages:
q
18/06/12
11
18/06/12
12
Implementing Classes
n
Basic mapping:
q
q q
A UML Class is implemented by a class in Java, C+ +, etc; Attributes are implemented by fields; Operations are implemented by methods. E.g., the implementation of abstract classes, interfaces, and generalization.
18/06/12
13
Example: Generalization
public class WalkIn extends Booking { public WalkIn(...) { ... } } public class Reservation extends Booking { Time arrivalTime ; public Reservation(...) { ... } public void setArrivalTime(Time t) { arrivalTime = t ; } }
18/06/12
15
Implementing Associations
n
Account :Account
n n n n
Guarantor :Guarantor
A link gives one object access to another and enables message passing. Java: References (C++: Object Pointer) share these properties, so associations can be implemented with references. References support only one direction of navigation.
q
18/06/12
18/06/12
17
Implementation
n
The association is defined by a field in the Account Java class holding a reference to an instance of the DebitCard Java class:
public class Account { private DebitCard theCard ; ... } Good match for simple object reference.
Account
0..1
DebitCard
18/06/12
18
As only Account is aware of this association, it should also maintain the association:
q q
Setup/Removal of the link; Appropriate methods for accessing the linked object.
public DebitCard getCard(){ return theCard; } public void setCard(DebitCard card){ theCard = card; }
e.g.
18/06/12
19
The last example assumes that the DebitCard association is mutable: q An Account object can be linked to a different DebitCard object during its lifetime. Hence, it makes sense to have the following methods for setup/removal the link:
public void setCard(DebitCard card){ theCard = card; } public void removeCard( ) { theCard = null; }
18/06/12
20
Immutable Association
n n
18/06/12
21
Compulsory Association
n n
Multiplicity with lowerbound larger than 0 is compulsory, i.e., At all times, the link(s) cannot be null . Example:
The above means An Account object must have exactly one link to a Guarantor object at all times . Programmers have to check explicitly to ensure this is true.
18/06/12
22
Implementation
n
The earliest possible opportunity to setup the link is in the constructor method:
public Account(Guarantor g) { if (g == null) { // throw NullLinkError } theGuarantor = g ; }
If the association is mutable, similar checks must be performed in methods that changes the link, e.g.:
public setGuarantor(Guarantor g) { if (g == null) { // throw NullLinkError } theGuarantor = g ; }
18/06/12
23
Data structures, such as arrays and vectors, can be used to store the references.
q
}
18/06/12 PAW de Diseo a Implementacin 24
Bidirectional Association
n
because references are unidirectional, it will take two references to implement a bidirectional link; the association can be implemented by including a suitable field in each of the associated classes.
Account DebitCard
Bidirectional Association stated in Class Diagram
Account
theCard: DebitCard
theCard theAccount
DebitCard
theAccount: Account
18/06/12
n n
Question: Why is that the order of associations creation to satisfy the conditions? 18/06/12 PAW de Diseo a Implementacin 26
Referential Integrity
n
The bidirectional association is implemented as two separate references, it is important to keep them consistent.
q q
The links should be inversed one to each other. Known as referential integrity.
aCard: DebitCard
theAccount
18/06/12
27
creation and reference setup (setCard() method) are two separate operations. Responsibility of programmer to make sure the two operations are performed in a consistent fashion.
DebitCard
To minimize the human error, delegate one of the classes to maintain the referential integrity instead. Example:
public class Account { private DebitCard theCard ; public void addCard() { theCard = new DebitCard(this); } // other methods as well } Account class handles both the DebitCard creation and reference setting.
18/06/12
28
Suppose an association is intended to be immutable and also needs to be traversed in both directions. Constructors of both classes should take in an instance of the other to satisfy the constraint specified by the class diagram.
q q
Java and C++ do not allow simultaneous object creation. Must create one of the objects with the default constructor, i.e., to violate the constraint for a short time.
PAW de Diseo a Implementacin 29
18/06/12
Code Example
n
g is not initialized. a is not initialized. g is initialized by default constructor, no Account reference. Setup the reference after Account a is properly initialized.
18/06/12
30
Class Diagram
myG
Account
myG: Guarantor
getMyG( ): Guarantor setMyG( Guarantor )
myA
Guarantor
myA: Account
getMyA ( ): Account setMyA ( Account ) Possible Implementation
18/06/12
31
Methods that change the reference should maintain referential integrity. Take setMyG() for example: q Have to set the reversed link too. q If Account has already a Guarantor:
n n
Either do not allow the link change. OR, remove the link with previous Guarantor.
oldG: Guarantor newG: Guarantor
a.SetMyG(newG)
a :Account
18/06/12
32
Code Example
//Class Account public void setMyG(Guarantor newG) { if (newG == null) //throw NullLinkError if (myG != null && myG != newG) { //previous Guarantor exists myG.removeMyA(this); } if (myG == newG) return; myG = newG; //set the reverse link myG.setMyA(this); }
//Class Guarantor void removeMyA(Account a) { //only allows holder of //the reverse link call //this method if (a == myA) myA = null; }
18/06/12
33
//Class Account void RemoveMyG(Guarantor g) { //only allows holder of //the reverse link call //this method if (g == myG) myG = null; }
18/06/12
34
Observations
n
Even with the complicated code, there are still some problems:
q q
The old guarantor will be without an Account object. Mutual recursive functions:
n
Should avoid compulsory bidirectional association if possible. If bidirectional association must be used, immutability is recommended to curb the coding complexity.
PAW de Diseo a Implementacin 35
18/06/12
One-to-Many Association
q
18/06/12
It provides a method like addAccount() that creates a new account and setup the reference. Similar to the addCard() method on slide 30.
PAW de Diseo a Implementacin 36
Many-to-Many Association
Allows only one of the classes to modify the links. E.g., It makes sense to allow only the Customer to change links to Accounts and maintain the reversed link also. Simplify the implementation as the coding logic can be considered from one direction only.
18/06/12
37
Many-to-Many Association
The new class (e.g., Signatory in the example above) is in a good position to maintain referential integrity.
:Account :Signatory :Customer
18/06/12
38
Qualified Association
For example, to access accounts given only the account number; Also to avoid a linear search through all accounts.
18/06/12
39
Implementing Qualifier
n
18/06/12
40
Code Example
n
public class Bank{ private HashTable theAccounts; public void addAccount (Account a) { theAccounts.put(new Integer(a.getNumber()), a); } put (Object Key, Object Value) public void removeAccount(int number) { theAccounts.remove(new Integer(number)); } public Account lookupAccount(int number) { return (Account) theAccounts.get(new Integer(number)); } get(Object Key): Object
18/06/12
41
It means a data structure that associates keys with values. The primary operation it supports efficiently is a lookup: given a key (e.g., a person's name), find the corresponding value (e.g., that person's telephone number). It works by transforming the key using a hash function into a hash, a number that is used to index into an array to locate the desired location ("bucket") where the values should be.
18/06/12
42
Like arrays, hash tables provide constant-time O(1) lookup on average, regardless of the number of items in the table.
PAW de Diseo a Implementacin 43
18/06/12
Association Class
Common strategy:
q
Transform the association class into a simple class; replacing it by two associations.
18/06/12
44
Student
Module *
Class Diagram
Student
1
Registration
Mark: Integer
Mark: Integer
:Module
:Registration
Mark = 76
:Student
:Module
:Registration
Mark = 76
:Student
:Student
:Registration
Object Diagram
:Student
:Registration
Object Diagram
Mark = 89
Mark = 89
18/06/12
45
Code Example
public class Module { private Vector registrations; public void enrol(Student st) { registrations.addElement( new Registration(st) ); } Maintain the link ... Pass the to Registration } Student to Registration. class Registration { private Student student; private int mark; Registration(Student st) { student = st; mark = 0; } ... }
18/06/12 PAW de Diseo a Implementacin
46
Summary
n n
Class Association:
n n n n
18/06/12
47
Reading Suggestions
n n
18/06/12
48