1
15‐214
School of Computer Science School of Computer Science
Principles of Software Construction: Objects, Design, and Concurrency - - PowerPoint PPT Presentation
Principles of Software Construction: Objects, Design, and Concurrency (Part 2: Designing (Sub )Systems) Assigning Responsibilities Jonathan Aldrich Charlie Garrod School of School of Computer Science Computer Science 15 214 1 Learning Goals
1
15‐214
School of Computer Science School of Computer Science
2
15‐214
2
3
15‐214
4
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
5
15‐214
5
6
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
7
15‐214
8
15‐214
8
9
15‐214
9
Domain Model
Object Model
1 0
15‐214
1 0
1 1
15‐214
1 1
1 2
15‐214
1 3
15‐214
1 4
15‐214
1 5
15‐214
1 5
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; }
1 6
15‐214
1 6
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); } }
1 7
15‐214
1 8
15‐214
1 9
15‐214
2 0
15‐214
2 1
15‐214
2 2
15‐214
2 3
15‐214
2 4
15‐214
2 5
15‐214
W 3 C- com pliant DOM standard
2 6
15‐214
2 6
2 7
15‐214
2 7
2 8
15‐214
2 8
2 9
15‐214
3 0
15‐214
3 0
: Student : System
login(id) checkout(bookid) due date logout() receipt
3 1
15‐214
3 1
: Student : System
login(id) checkout(bookid) due date logout() receipt
CheckoutController login(id: Int) checkout(bid: Int) logout()
3 2
15‐214
3 2
: Student : System
login(id) checkout(bookid) due date logout() receipt
3 3
15‐214
3 4
15‐214
3 5
15‐214
3 5
3 6
15‐214
3 7
15‐214
3 8
15‐214
Register responsibilities
environment stimulus
3 9
15‐214
3 9
class DatabaseApplication //... database fields //... Logging Stream //... Cache Status 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 } }
4 0
15‐214
Graph is tasked with not just data, but also algorithmic responsibilities
4 1
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?
4 2
15‐214
4 2
4 3
15‐214
4 3
4 4
15‐214
– Classes are easier to maintain – Easier to understand – Often support low coupling – Supports reuse because of fine grained responsibility
4 5
15‐214
4 5
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; } }
4 6
15‐214
4 6
4 7
15‐214
4 7
4 8
15‐214
4 9
15‐214
4 9
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; }
5 0
15‐214
5 1
15‐214
5 1
5 2
15‐214
5 3
15‐214
5 4
15‐214
5 5
15‐214
5 6
15‐214
5 7
15‐214
5 8
15‐214
* old midterm question