University of California, San Diego. La Jolla, CA 92093-0114, USA. CSE 70:
Unified Modeling Language – Class Diagrams. Design Patterns – Strategy.
CSE 70: Unified Modeling Language – Class Diagrams Design Patterns – Strategy Ingolf Krueger
Department of Computer Science & Engineering University of California, San Diego La Jolla, CA 92093-0114, USA
California Institute for Telecommunications and Information Technologies La Jolla, CA 92093-0405, USA
Learning Goals for Today
© Ingolf Krueger, 2010
CSE
2
Learning Goals
• Know the use of the Unified Modeling Language (UML) for requirements capture and design.
• Be able to draw and understand basic UML class diagrams.
• Know and be able to apply the Strategy Pattern
© Ingolf Krueger, 2010
CSE
3
Unified Modeling Language (UML)
source: http://commons.wikimedia.org/wiki/File:UML_Diagrams.jpg © Ingolf Krueger, 2010
CSE
4
UML • A visual language for specifying, constructing, and documenting system artifacts. • Provides different language elements (diagrams/text) to model systems from various viewpoints – Structure – Behavior
• OMG standard since 1997, now at version UML 2.2 • Especially useful for modeling/documenting objectoriented systems: requirements, architecture, design – but not limited to that © Ingolf Krueger, 2010
CSE
5
Basics Covered in CSE 70
Behavior State Machine
Structure Use case
Class Object
Interaction Overview
Sequence Activity
Component Composite Structure Deployment
Communication © Ingolf Krueger, 2010
Package CSE
6
UML Class Diagrams
© Ingolf Krueger, 2010
CSE
7
Example: ChatServer Class in Java ChatServer.java !
public class ChatServer extends BaseChatServer {! ... ! private String[] clients; ! private int client_index;! ... ! public void sign_on(String client_id) {! clients[client_index++] = client_id;! }! ...! }!!
© Ingolf Krueger, 2010
CSE
8
Example: ChatServer Class in Java ChatServer.java !
inheritance relationship public class ChatServer extends BaseChatServer {! ... ! private String[] clients; ! private int client_index;! ... ! public void sign_on(String client_id) {! clients[client_index++] = client_id;! }! ...! }!!
© Ingolf Krueger, 2010
CSE
9
Example: ChatServer Class in Java ChatServer.java !
public class ChatServer extends BaseChatServer {! ... ! private String[] clients; ! fields private int client_index;! ... ! public void sign_on(String client_id) {! clients[client_index++] = client_id;! }! ...! }!!
© Ingolf Krueger, 2010
CSE
10
Example: ChatServer Class in Java ChatServer.java !
public class ChatServer extends BaseChatServer {! ... ! private String[] clients; ! private int client_index;! methods ... ! public void sign_on(String client_id) {! clients[client_index++] = client_id;! }! ...! }!!
© Ingolf Krueger, 2010
CSE
11
UML Class Diagrams: Basics Attributes/ Fields
Superclass
Subclass Inheritance (extends)
Operations/ Methods
© Ingolf Krueger, 2010
CSE
12
UML Class Diagrams: Compare to Java Code public class ChatServer ! extends BaseChatServer {! ... ! private String[] clients; ! private int client_index;! ... ! public void ! sign_on(String client_id) {! clients[client_index++] = ! client_id;! }! ...! }!! © Ingolf Krueger, 2010
CSE
13
Class Diagrams: Inheritance public class ChatServer ! extends BaseChatServer {! ... ! private String[] clients; ! private int client_index;! ... ! public void ! sign_on(String client_id) {! clients[client_index++] =! client_id;! }! ...! }!! © Ingolf Krueger, 2010
CSE
14
Class Diagrams: Attributes/Fields public class ChatServer ! extends BaseChatServer {! ... ! private String[] clients; ! private int client_index;! ... ! public void ! sign_on(String client_id) {! clients[client_index++] = ! client_id;! }! ...! }!! © Ingolf Krueger, 2010
CSE
15
Class Diagrams: Methods public class ChatServer ! extends BaseChatServer {! ... ! private String[] clients; ! private int client_index;! ... ! public void ! sign_on(String client_id) {! clients[client_index++] = ! client_id;! }! ...! }!! © Ingolf Krueger, 2010
CSE
16
Class Diagrams: Basics
read: ChatServer is-a BaseChatServer
© Ingolf Krueger, 2010
CSE
17
Classes and Interfaces
© Ingolf Krueger, 2010
CSE
18
Class Diagrams: Interfaces & Classes public interface IDispatchTarget { !public void close();! }! …! public interface IController extends IDispatchTarget {! !public void ! parseCommand(String cmd);! }! …! public class Client ! implements IController {! … ! }!! © Ingolf Krueger, 2010
CSE
19
Class Diagrams: Interfaces & Classes public interface IDispatchTarget { !public void close();! }! …! public interface IController extends IDispatchTarget {! !public void ! parseCommand(String cmd);! }! …! public class Client ! implements IController {! … ! }!! © Ingolf Krueger, 2010
CSE
20
Class Diagrams: Interfaces & Classes public interface IDispatchTarget { !public void close();! }! …! public interface IController extends IDispatchTarget {! !public void ! parseCommand(String cmd);! }! …! public class Client ! implements IController {! … ! }!! © Ingolf Krueger, 2010
CSE
21
Class Diagrams: Interfaces & Classes public interface IDispatchTarget { !public void close();! }! …! public interface IController extends IDispatchTarget {! !public void ! parseCommand(String cmd);! }! …! public class Client ! implements IController {! … ! }!! © Ingolf Krueger, 2010
CSE
22
Associations, Aggregation
© Ingolf Krueger, 2010
CSE
23
Example: ChatClient package simplechat.client;! import simplechat.server.*;! ...! public class ChatClientImp {!
ChatClientImp.java !
!private String name;! private ChatServer server;! public void setName(String name) {! this.name = name! }! ...! © Ingolf Krueger, 2010
CSE
24
Example: ChatClient !public ChatClientImp(String client_id, ! String server_id) {! ! !try {! ! ! !Registry registry = ! LocateRegistry.getRegistry();! ! ! !server = ! (ChatServer) ! registry.lookup(server_id);! ! ! !! ! ! !server.sign_on(client_id);! ...! ! ! !! © Ingolf Krueger, 2010
CSE
25
Undirected Association any number of
exactly one
Association (undirected)
© Ingolf Krueger, 2010
CSE
26
Undirected Association
role/field name
© Ingolf Krueger, 2010
CSE
27
Undirected Association
read: every object of class ChatClientImp has exactly one ChatServer every object of class ChatServer has many ChatClientImps © Ingolf Krueger, 2010
CSE
28
Undirected Association
public class ChatClientImp {! ...! private ChatServer server;! ...! } ! © Ingolf Krueger, 2010
CSE
29
Undirected Association
public class ChatClientImp {! ...! private ChatServer server;! ...! } ! © Ingolf Krueger, 2010
CSE
30
Undirected Association
public class ChatServer {! ...! private ChatClientImp[] clients;! ...! } ! © Ingolf Krueger, 2010
CSE
31
Frequently Used Multiplicities
Symbol
Meaning
1
exactly one
n
exactly n
n..m
at least n and at most m
*
any number of
© Ingolf Krueger, 2010
CSE
32
Design Patterns: Proven solutions for recurring design problems in specific contexts © Ingolf Krueger, 2010
CSE
33
Why bother? Let’s Design a Chat Server that can Handle Messages © Ingolf Krueger, 2010
CSE
34
First Design
© Ingolf Krueger, 2010
CSE
35
Customer wants: Server with Message Logging Capability
© Ingolf Krueger, 2010
CSE
36
Second Design: Message Logging Server
© Ingolf Krueger, 2010
CSE
37
Customer also wants: Server with Message Encryption Capability
© Ingolf Krueger, 2010
CSE
38
Third Design: Logging OR Encryption
© Ingolf Krueger, 2010
CSE
39
Customer really wants: Logging Server with Encryption Capability
© Ingolf Krueger, 2010
CSE
40
Fourth Design: Logging with Encryption
© Ingolf Krueger, 2010
CSE
41
Pros and Cons of Fourth Design • Pro: – Customer requirement can be fulfilled
• Cons: – Unclear design decomposition strategy: • why should message handling dominate logging? • why should logging dominate encryption?
– Tight coupling between message handling, logging and encryption capability! • Addition of further capabilities leads to exponential spread of the class hierarchy
– What if we want to replace the logging/encryption algorithm at runtime: requires restart of entire server!
© Ingolf Krueger, 2010
CSE
42
Separation of Concerns
Let me try to explain to you, what to my taste is characteristic for all intelligent thinking. It is, that one is willing to study in depth an aspect of one's subject matter in isolation for the sake of its own consistency, all the time knowing that one is occupying oneself only with one of the aspects. […] But nothing is gained –on the contrary!– by tackling these various aspects simultaneously. It is what I sometimes have called "the separation of concerns", which, even if not perfectly possible, is yet the only available technique for effective ordering of one's thoughts, that I know of.1 1E.
W. Dijkstra: "On the role of scientific thought", in E.W. Dijkstra, Selected Writings on Computing: A Personal Perspective, New York, NY, USA: Springer-Verlag New York, Inc., pp. 60–66 © Ingolf Krueger, 2010
CSE
43
What are the separate concerns of the Server design? Message Handling, Logging, Encryption © Ingolf Krueger, 2010
CSE
44
How can we separate them out?
© Ingolf Krueger, 2010
CSE
45
Chat Server Interfaces: MessageHandler
© Ingolf Krueger, 2010
CSE
46
Chat Server Interfaces: Encryptor and Logger
© Ingolf Krueger, 2010
CSE
47
How can we bring them back together?
© Ingolf Krueger, 2010
CSE
48
Chat Server with Composition
© Ingolf Krueger, 2010
CSE
49
Example: Server with Composition public class Server {!
ChatServer.java !
!private MessageHandler handler;! !private MessageLogger logger;! !private MessageEncryptor encryptor;! public Server() {! handler = null;! logger = null;! encryptor = null;! }
...! © Ingolf Krueger, 2010
CSE
50
Example: Server with Composition Server.java ! ...! public void setHandler(MessageHandler handler){ ! this.handler = handler;! }!
public void setLogger(MessageLogger logger) {! this.logger = logger;! }! public void setEncryptor(MessageEncryptor en) { ! this.encryptor = en;! }! ...! © Ingolf Krueger, 2010
CSE
51
Example: Server with Composition Server.java ! ...! public void doHandleMessage(Message msg) {! this.handler.handleMessage(msg);! }!
public void doLogMessage(Message msg) {! this.logger.handleMessage(msg);! }! public void doEncryptMessage(Message msg) {! this.encryptor.handleMessage(msg);! }! }! © Ingolf Krueger, 2010
CSE
52
Example: RSAFileServer
public class RSAFileServer {!
RSAFileServer.java !
public RSAFileServer() {! super();! setHandler(new BasicHandler());! setLogger(new FileLogger());! setEncryptor(new RSAEncryptor()); }
...! }!
© Ingolf Krueger, 2010
!
CSE
53
Key OO Design Principles
Develop Against Interfaces rather than Implementations Favor Composition over Inheritance © Ingolf Krueger, 2010
CSE
54
Separation of Concerns: Variability vs. Stability
Separate what varies from what doesn’t. OO Mechanisms: • Encapsulation (Classes/Interfaces) • Inheritance • Association/Aggregation/Composition
How to do it? Design Patterns to the Rescue © Ingolf Krueger, 2010
CSE
55
What have you learned today?
© Ingolf Krueger, 2010
CSE
56
Learning Goals
• Know the use of the Unified Modeling Language (UML) for requirements capture and design.
• Be able to draw and understand basic UML class diagrams.
• Know and be able to apply the Strategy Pattern
© Ingolf Krueger, 2010
CSE
57