1
15-214
School of Computer Science
Objects, Design, and Concurrency (Part 2: Designing (Sub-)Systems) - - PowerPoint PPT Presentation
Principles of Software Construction: Objects, Design, and Concurrency (Part 2: Designing (Sub-)Systems) Assigning Responsibilities Christian Kstner Bogdan Vasilescu School of Computer Science 15-214 1 2 15-214 2 Learning Goals Apply
1
15-214
School of Computer Science
2
15-214
2
3
15-214
3
4
15-214
5
15-214
5
Considering the Library problem, which class should know which items have been borrowed by a user? Which should compute late fees?
6
15-214
– knowing – doing
– doing something itself, such as creating an object or doing a calculation – initiating action in other objects – controlling and coordinating activities in other objects
– knowing about private encapsulated data – knowing about related objects – knowing about things it can derive or calculate
7
15-214
7
8
15-214
– Desired quality attributes of software – Driven by cost/benefit economics – Examples: design for change, understanding, reuse, …
– Guidelines for designing software – Support one or more design goals – Examples: Information hiding, low repr. gap, low coupling, high cohesion, …
– Rules of thumb for low-level design decisions – Promote design principles, and ultimately design goals – Example: Creator, Expert, Controller
– General solutions to recurring design problems – Promote design goals, but may add complexity or involve tradeoffs – Examples: Decorator, Strategy, Template Method
– Use high-level goals of project to resolve
Goals Heuristics Patterns Principles
9
15-214
10
15-214
10
11
15-214
11
Domain Model
Object Model
12
15-214
12
13
15-214
13
14
15-214
15
15-214
16
15-214
– “few" is context-dependent
– Changes in related classes force local changes; changes in local class forces changes in related classes (brittle, rippling effects) – Harder to understand in isolation. – Harder to reuse because requires additional presence of other dependent classes – Difficult to extend – changes in many places
17
15-214
17
class Shipment { private List<Box> boxes; int getWeight() { int w=0; for (Box box: boxes) for (Item item: box.getItems()) w += item.weight; return w; } class Box { private List<Item> items; Iterable<Item> getItems() { return items;} } class Item { Box containedIn; int weight; }
18
15-214
18
class Box { private List<Item> items; private Map<Item,Integer> weights; Iterable<Item> getItems() { return items;} int getWeight(Item item) { return weights.get(item);} } class Item { private Box containedIn; int getWeight() { return containedIn.getWeight(this);} }
19
15-214
20
15-214
21
15-214
22
15-214
23
15-214
24
15-214
25
15-214
26
15-214
– protected fields and methods are visible – subclass is fragile to many superclass changes, e.g. change in method signatures, added abstract methods – Guideline: prefer composition to inheritance, to reduce coupling
– A stable interface is unlikely to change, and likely well- understood – Prefer coupling to interfaces over coupling to implementations
– Consider cohesion, low repr. gap, and other principles
27
15-214
W3C- compliant DOM standard
28
15-214
28
29
15-214
29
30
15-214
30
31
15-214
32
15-214
32
: Student : System
login(id) checkout(bookid) due date logout() receipt
33
15-214
33
: Student : System
login(id) checkout(bookid) due date logout() receipt
CheckoutController login(id: Int) checkout(bid: Int) logout()
34
15-214
34
: Student : System
login(id) checkout(bookid) due date logout() receipt
35
15-214
– does not do much work itself – delegates to other objects
– -> one overall controller for the system
– -> several smaller controllers for specific tasks
36
15-214
– User interface and domain logic are decoupled from each other
– Both are coupled to the controller, which serves as a mediator, but this coupling is less harmful
– Controller serves as an interface to the domain logic – Smaller, explicit interfaces support evolvability
37
15-214
(OR SINGLE RESPONSIBILITY PRINCIPLE)
37
38
15-214
39
15-214
40
15-214
Simulation responsibilities
environment stimulus
41
15-214
41
class DatabaseApplication public void authorizeOrder(Data data, User currentUser, ...){ // check authorization // lock objects for synchronization // validate buffer // log start of operation // perform operation // log end of operation // release lock on objects } public void startShipping(OtherData data, User currentUser, ...){ // check authorization // lock objects for synchronization // validate buffer // log start of operation // perform operation // log end of operation // release lock on objects } }
42
15-214
class Chat { List<String> channels; Map<String, List<Msg>> messages; Map<String, String> accounts; Set<String> bannedUsers; File logFile; File bannedWords; URL serverAddress; Map<String, Int> globalSettings; Map<String, Int> userSettings; Map<String, Graphic> smileys; CryptStrategy encryption; Widget sendButton, messageList; }
42
43
15-214
class Chat { List<String> channels; Map<String, List<Msg>> messages; Map<String, String> accounts; Set<String> bannedUsers; File logFile; File bannedWords; URL serverAddress; Map<String, Int> globalSettings; Map<String, Int> userSettings; Map<String, Graphic> smileys; CryptStrategy encryption; Widget sendButton, messageList; }
43
class Chat { Content content; AccountMgr accounts; File logFile; ConnectionMgr conns; } class ChatUI { Chat chat; Widget sendButton, …; } class AccountMgr { … acounts, bannedUsr… }
44
15-214
44
45
15-214
Graph is tasked with not just data, but also algorithmic responsibilities
46
15-214
class Player { Board board; /* in code somewhere… */ getSquare(n); Square getSquare(String name) { for (Square s: board.getSquares()) if (s.getName().equals(name)) return s; return null; }} class Player { Board board; /* in code somewhere… */ board.getSquare(n); } class Board{ List<Square> squares; Square getSquare(String name) { for (Square s: squares) if (s.getName().equals(name)) return s; return null; }}
Which design has higher cohesion?
47
15-214
47
48
15-214
48
49
15-214
very different functional areas
functional area and collaborates with classes to fulfil tasks
– Classes are easier to maintain – Easier to understand – Often support low coupling – Supports reuse because of fine grained responsibility
methods of highly related functionality; does not do too much work
50
15-214
50
class Graph { Node[] nodes; boolean[] isVisited; } class Algorithm { int shortestPath(Graph g, Node n, Node m) { for (int i; …) if (!g.isVisited[i]) { … g.isVisited[i] = true; } } return v; } }
51
15-214
51
52
15-214
52
53
15-214
54
15-214
54
class Shipment { private List<Box> boxes; int getWeight() { int w=0; for (Box box: boxes) for (Item item: box.getItems()) w += item.weight; return w; } class Box { private List<Item> items; Iterable<Item> getItems() { return items;} } class Item { Box containedIn; int weight; }
55
15-214
56
15-214
Sale time Sales LineItem quantity Product Description description price itemID Described-by
Contains 1..* 1 1 Register id Captured-on Customer name Paid by
57
15-214
Sale time Sales LineItem quantity Product Description description price itemID Described-by
Contains 1..* 1 1 Register id Captured-on Customer name Paid by
58
15-214
S a le tim e ... g e tT o ta l() S a le s L in e Ite m q u a n tity g e tS u b to ta l() P ro d u c t D e s c rip tio n d e s c rip tio n p ric e ite m ID g e tP ric e () N e w m e th o d :P ro d u c t D e s c rip tio n 1 .1 : p := g e tP ric e () 1 * : s t = g e tS u b to ta l : S a le t = g e tT o ta l lin e Ite m s [ i ] : S a le s L in e Ite m
59
15-214
60
15-214
60
61
15-214
62
15-214
63
15-214
64
15-214
65
15-214
– class responsible for creating objects it needs to reference – creating the objects themselves avoids depending on another class to create the object
– Object creation is hidden, can be replaced locally
– complex initialization – instantiate different classes in different circumstances – then cohesion suggests putting creation in a different object
66
15-214
* old midterm question
67
15-214
67
Considering the Library problem, which class should know which items have been borrowed by a user? Which should compute late fees?
68
15-214