Basic Implementation Steps
3 ุฎุทูุงุช ูุชุญููู ุงูู design ูู code ุดุบูุงู.
๐ฅ Inputs
- Detailed UML Class Diagram
- UML Sequence Diagram
- UML State Diagram (ูู ู ุญุชุงุฌ)
๐ค Output
Code โ implementation ููู model ูู ุง ูู ู ูุตูู.
Other aspects: data structures, algorithms, database, UI.
ุงูู method interface = name + parameters + return + visibility. ุงูู functionality ุจุชุชุญุฏุฏ ุนู ุทุฑูู contract:
- Precondition โ ูููุฏ ุนูู
attributesูุงููactual parametersูุจู ุงููcall. - Postcondition โ
effectsุจุนุฏ ุงููcall:instance creation/deletionยทattribute modificationยทassociation formed/broken.
// Interface
public void withdraw(float amt) { ... }
// Contract
// Precondition: amt > 0, amt <= balance
// Postcondition: new balance = old balance - amt
Order of Implementation
Top-Down vs Bottom-Up.
ุงุจุฏุฃ ุจุงูู high-level components (e.g., OrderManager) โ ุจุนุฏูู ุงูู dependent components.
โ Advantages
- Class A = higher level component ู makes use of others.
- Early validation of overall system.
โ Disadvantages
- ู ุญุชุงุฌ stubs (temporary implementations) ููู lower classes.
ุงุจุฏุฃ ุจุงูู lower-level classes (e.g., Item, Product) โ ุจุนุฏูู ุงูู higher-level.
โ Advantages
- Low-level classes stand alone.
- ู ููุด stubs ู ุญุชุงุฌุฉ.
โ Disadvantages
- ุงูู complete executable ูุชุฃุฎุฑ ููููุงูุฉ.
UML Class โ Java Class
Basic mappings.
- UML Class โ Java
class - UML Attributes โ Java
fields - UML Operations โ Java
methods
๐ UML
// Abstract
Account {abstract}
// Interface
<<interface>> Payable
// Enum
<<enum>> OrderStatus
CREATED
SHIPPED
DELIVEREDโ Java
abstract class Account { ... }
interface Payable { ... }
public enum OrderStatus {
CREATED, SHIPPED, DELIVERED
}// SavingsAccount extends Account
class Account { ... }
class SavingsAccount extends Account { ... }
// Invoice implements Payable
interface Payable { ... }
class Invoice implements Payable { ... }
UML Types โ Java Types
Direct type translations.
| UML Type | Java Type |
|---|---|
| Boolean | boolean |
| Integer | int |
| Real | float or double |
| String | String |
Default ุจูุนุชู
ุฏ ุนูู ุงูู modeling tool. ุจุนุถ ุงูุฃุฏูุงุช ุจุชุณุชุฎุฏู
int ูู default ูู ุงูู return value ู
ุด ู
ุชุญุฏุฏ.
Unidirectional Associations
ุงูู Associations ูู UML ุจุชุชุญูู ูู references ูู Java. ุงูู default multiplicity = 0..1 (reference can be null).
UML: Account [1] โโ [0..1] DebitCard
public class Account {
private DebitCard theCard; // 0..1 = good match for ref
public DebitCard getCard() { return theCard; }
public void setCard(DebitCard card) { theCard = card; }
public void removeCard() { theCard = null; }
}
{Immutable}ุงูู link ุจูุชุนู ูู set ู ุฑุฉ ูุงุญุฏุฉ ูู ุด ุจูุชุบูุฑ. ูุงุฒู explicit check:
public void setCard(DebitCard card) {
if (theCard != null) {
// throw ImmutableAssociationException
}
theCard = card;
}
ุงูู link ูุงุฒู
ู
ุงูุจูุงุด null ุฃุจุฏูุง. ุงุนู
ู enforce ูู ุงูู constructor:
public Account(Guarantor g) {
if (g == null) {
// throw NullLinkError
}
theGuarantor = g; // must have a guarantor
}
Bidirectional Associations โญ
ุฏู ุจุชูุฌู ูู Question 1.07 ู ู ุงู ุชุญุงูุงุช Fall 2021-22 / Winter 2021.
ุงูู References ูู Java ุจุทุจูุนุชูุง unidirectional. ุนูุดุงู ุชุนู ู bidirectional ู ุญุชุงุฌ 2 references โ ูุงุญุฏ ูู ูู class.
ุงูู key concept: Referential Integrity โ ุงูู links ูุงุฒู ุชูุถู consistent.
โ ๏ธ Referential Integrity Violation
Account acc1 = new Account();
Account acc2 = new Account();
DebitCard aCard = new DebitCard(acc1); // card โ acc1
acc2.setCard(aCard); // acc2 โ card
// PROBLEM: card.theAccount = acc1, but acc2 thinks it owns card!
// Links inconsistent โ violates integrity
โ Solution โ Delegate One Class to Maintain Integrity
public class Account {
private DebitCard theCard;
public void addCard() {
// Account handles BOTH creation and reference setting
theCard = new DebitCard(this);
}
}
1-to-Many & Many-to-Many
ArrayList / collection ููู multiple references. ุฏู ุดูู Code (a) ูู exam Q1.07.
(check in constructor)
= new ArrayList<>();
(check size โฅ 1)
ุงูู UML: Advertiser [1] โโ [*] Account
ุงูููุฏ ุงูุตุญ (ูู Code A ูู ุงูู exam):
public class Advertiser {
private Set accounts;
public Advertiser() {
accounts = new HashSet();
}
public void addAccount(Account a) {
if (a != null) {
accounts.add(a);
a.setOwner(this); // Maintain bidirectional
}
}
}
public class Account {
private Advertiser owner; // single ref โ many accounts share owner
public void setOwner(Advertiser newOwner) {
if (newOwner != null)
owner.addAccount(this);
}
}
- Code B:
AdvertiserุนูุฏูAccount accountูุงุญุฏ โ ุบูุทุ ุงูู ูุฑูุถSet. - Code C:
Accountุนูุฏู Set of Advertiser owners โ ุบูุทุ ูุฏู ุชุจูู many-to-many ู ุด 1-to-many.
- Direct โ ูู class ูุญุชูุธ ุจู collection. ุงุฎุชุงุฑ ูุงุญุฏ ูุญุงูุธ ุนูู ุงูู referential integrity.
- Reify the association โ ุงุนู
ู intermediate class ู
ุซู
Signatoryูู ุณู ุงูุนูุงูุฉ. ุบุงูุจุงู ุฃูุถู.
Qualified Associations & Association Classes
HashMap ููู qualifiers. ู Reification ููู association classes.
UML: Bank [accno] โ Account [0..1] โ efficient lookup by accno.
public class Bank {
private HashMap<Integer, Account> accounts;
public Account lookupAccount(int number) {
return accounts.get(number);
}
public void addAccount(Account a) {
accounts.put(a.getNumber(), a);
}
}
UML: Module *โโ*โโ Student + attached Registration {mark}
Implementation: ุญููููุง ูุนูุงูุชูู ู ู ุฎูุงู new class.
class Registration {
private Student student;
private int mark;
Registration(Student st) {
student = st;
mark = 0;
}
}
public class Module {
private List<Registration> registrations;
public void enrol(Student st) {
registrations.add(new Registration(st));
}
}
State Diagram โ Code
3 ุฎุทูุงุช: define states ยท track current state ยท switch on state per operation.
- Define States โ ุงุนู ููุง constants (ุฃู ุงุณุชุฎุฏู State pattern).
- Track Current State โ ุญุฏุฏ ุงูู initial state ูู ุงูู constructor.
- Operation-by-Operation โ ุงุณุชุฎุฏู
switchูููู case ููู state.
States: InCredit, OverDrawn, Suspended. Operations: deposit, withdraw, suspend, unsuspend.
public class Account {
// Step 1: define states
private final int InCredit = 0;
private final int Overdrawn = 1;
private final int Suspended = 2;
private int state;
private int historyState;
private double bal;
// Step 2: initial state in constructor
public Account() {
state = InCredit;
}
// Step 3: switch on state per operation
public void withdraw(double amt) {
switch (state) {
case InCredit:
if (amt > bal) state = Overdrawn;
bal -= amt;
break;
case Overdrawn:
case Suspended:
break;
}
}
}
ุงูู Entry action ุจุชุชููุฐ ูู ุง ุชุฏุฎู state. ูุงูู Exit action ูู ุง ุชุฎุฑุฌ.
public void eventF() { // transition INTO state S
switch (state) {
case T:
doEntry(); // state T โ S: trigger S's entry action
state = S;
break;
}
}
public void eventG() { // transition OUT of state S
switch (state) {
case S:
doExit(); // state S โ U: trigger S's exit action
state = U;
break;
}
}
Exam Bank โ Lecture 10
ุฃุณุฆูุฉ Code โ Class Diagram ู ู Fall 2021-22 ู Winter 2021. ูุงู ูู ูู ุงู ุชุญุงู final.
Advertiser [1] โโ [*] Account. Which code properly implements it?
(a) Advertiser has Set<Account> accounts. Account has single Advertiser owner. addAccount checks null + sets owner.
(b) Advertiser has single
Account account field.
(c) Advertiser has Set; Account has Set<Advertiser> owners.
multiplicity 1:* ุจุชููู: Advertiser ูุงุญุฏ โ Accounts ูุชูุฑุฉ. Code (a) ุตุญ:
- Advertiser โ
Set<Account> accounts(collection). - Account โ
Advertiser owner(single reference). addAccount+setOwnerููุญูุงุธ ุนูู bidirectional integrity.
Advertiser ุงูู
ูุฑูุถ ูุญู
ู multiple. ยท
C โ Account has Set of Advertisers = many-to-manyุ ู
ุด 1-to-*.
class Order { OrderStatus status; ArrayList<OrderDetail> details; ... }
class OrderDetail { Product product; }
class Product { String code, name; float unitPrice; }
enum OrderStatus { OPEN, CLOSED; }
ArrayList<OrderDetail> = Order has many OrderDetails (multiplicity *). ู Order owns OrderDetailsุ ูุงูุนูุงูุฉ composition โ. ูู
ุงู OrderDetail has 1 Productุ ู Order has OrderStatus enum.
Manager [1] โโ [*] Account. Best Java implementation?private Account account;private Manager mgr;private ArrayList<Account> accounts;private HashMap<String, Account> index;HashMap ููู qualified associations ููุท.
Bank [accno] โ Account) is best implemented with:HashMap ุจูุฏู O(1) lookup ุจุงูู key.
switch ุญุณุจ ุงูู state ูู ูู operation.
(C) ู ู ูู ููุทุจู ุจู State Patternุ ุฃูุธู ููู ุฃุนูุฏ. ุงูุฏูุชูุฑ ุจููุตู ุจู (D) ููุจุณุงุทุฉ.
default Guarantorุ ุจุนุฏูู Account(g)ุ ุจุนุฏูู g.setAccount(a) โ ูุฏู ุจุชูู
ู ุงูู bidirectional link.Cheat Sheet
UML โ Code quick reference.
๐ UML โ Java Basics
class X { }abstract class Xinterface Xenum X { A, B }extends / implements๐ข Multiplicity โ Code
๐ Special Patterns
๐ State Diagram โ Code
Rapid Revision
Flashcards ยท Common Mistakes.
๐จ Common Mistakes
1:* ุจู many-to-many1:* = ุทุฑู ุนูุฏู collection ูุงูุทุฑู ุงูุชุงูู ุนูุฏู single ref. ุฃู
ุง m:n = ุงูุทุฑููู ุนูุฏูู
collections.HashMap ููู * associationHashMap ููู qualified ุจุณ. ุงูู * ุงูุนุงุฏู ูุณุชุฎุฏู
ArrayList/Set.