Upload
bc103058
View
218
Download
0
Embed Size (px)
Citation preview
7/28/2019 CSE 70 UML Overview Classes
1/57
California Institute for Telecommunicationsand Information TechnologiesLa Jolla, CA 92093-0405, USA
Department of Computer Science & EngineeringUniversity of California, San DiegoLa Jolla, CA 92093-0114, USA
CSE 70: Unified Modeling Language Class Diagrams
Design Patterns Strategy
Ingolf Krueger
7/28/2019 CSE 70 UML Overview Classes
2/57
Ingolf Krueger, 2010 2CSE
Learning Goals for Today
7/28/2019 CSE 70 UML Overview Classes
3/57
Ingolf Krueger, 2010 3CSE
Learning Goals
Know the use of the Unified Modeling Language (UML) forrequirements capture and design.
Be able to draw and understand basic UML class diagrams.
Know and be able to apply the Strategy Pattern
7/28/2019 CSE 70 UML Overview Classes
4/57
Ingolf Krueger, 2010 4CSE
Unified Modeling Language (UML)
source: http://commons.wikimedia.org/wiki/File:UML_Diagrams.jpg
7/28/2019 CSE 70 UML Overview Classes
5/57
Ingolf Krueger, 2010 5CSE
UML
A visual language for specifying, constructing, anddocumenting 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 object-
oriented systems: requirements, architecture, design but not limited to that
7/28/2019 CSE 70 UML Overview Classes
6/57
Ingolf Krueger, 2010 6CSE
Basics Covered in CSE 70
StructureBehavior
State Machine
InteractionOverview
ClassObject
Deployment
Package
CompositeStructure
ComponentSequence
Activity
Use case
Communication
7/28/2019 CSE 70 UML Overview Classes
7/57
Ingolf Krueger, 2010 7CSE
UML Class Diagrams
7/28/2019 CSE 70 UML Overview Classes
8/57
Ingolf Krueger, 2010 8CSE
Example: ChatServer Class in 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;!
}!...!
}!!
ChatServer.java
7/28/2019 CSE 70 UML Overview Classes
9/57
Ingolf Krueger, 2010 9CSE
Example: ChatServer Class in 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;!
}!...!
}!!
ChatServer.java
inheritance relationship
7/28/2019 CSE 70 UML Overview Classes
10/57
Ingolf Krueger, 2010 10CSE
Example: ChatServer Class in 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;!
}!...!
}!!
ChatServer.java
fields
7/28/2019 CSE 70 UML Overview Classes
11/57
Ingolf Krueger, 2010 11CSE
Example: ChatServer Class in 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;!
}!...!
}!!
ChatServer.java
methods
7/28/2019 CSE 70 UML Overview Classes
12/57
Ingolf Krueger, 2010 12CSE
UML Class Diagrams: Basics
SuperclassAttributes/Fields
Inheritance
(extends)Operations/
Methods
Subclass
7/28/2019 CSE 70 UML Overview Classes
13/57
Ingolf Krueger, 2010 13CSE
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;!
}!...!
}!!
7/28/2019 CSE 70 UML Overview Classes
14/57
Ingolf Krueger, 2010 14CSE
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;!
}!...!
}!!
7/28/2019 CSE 70 UML Overview Classes
15/57
Ingolf Krueger, 2010 15CSE
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;!
}!...!
}!!
7/28/2019 CSE 70 UML Overview Classes
16/57
Ingolf Krueger, 2010 16CSE
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;!
}!...!
}!!
7/28/2019 CSE 70 UML Overview Classes
17/57
Ingolf Krueger, 2010 17CSE
Class Diagrams: Basics
read:
ChatServeris-a
BaseChatServer
7/28/2019 CSE 70 UML Overview Classes
18/57
Ingolf Krueger, 2010 18CSE
Classes and Interfaces
7/28/2019 CSE 70 UML Overview Classes
19/57
Ingolf Krueger, 2010 19CSE
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 {!
!}!!
7/28/2019 CSE 70 UML Overview Classes
20/57
Ingolf Krueger, 2010 20CSE
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 {!
!}!!
7/28/2019 CSE 70 UML Overview Classes
21/57
7/28/2019 CSE 70 UML Overview Classes
22/57
Ingolf Krueger, 2010 22CSE
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 {! !}!!
7/28/2019 CSE 70 UML Overview Classes
23/57
Ingolf Krueger, 2010 23CSE
Associations, Aggregation
7/28/2019 CSE 70 UML Overview Classes
24/57
Ingolf Krueger, 2010 24CSE
Example: ChatClient
package simplechat.client;!import simplechat.server.*;!...!public class ChatClientImp {!!private String name;! private ChatServer server;! public void setName(String name) {!
this.name = name!}!...!
ChatClientImp.java
7/28/2019 CSE 70 UML Overview Classes
25/57
Ingolf Krueger, 2010 25CSE
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);! ...!! ! !!
7/28/2019 CSE 70 UML Overview Classes
26/57
Ingolf Krueger, 2010 26CSE
Undirected Association
Association
(undirected)
any numberof
exactlyone
7/28/2019 CSE 70 UML Overview Classes
27/57
Ingolf Krueger, 2010 27CSE
Undirected Association
role/field
name
7/28/2019 CSE 70 UML Overview Classes
28/57
Ingolf Krueger, 2010 28CSE
Undirected Association
read:
every object of class ChatClientImphas exactly one ChatServer
every object of class ChatServerhas many ChatClientImps
7/28/2019 CSE 70 UML Overview Classes
29/57
Ingolf Krueger, 2010 29CSE
public class ChatClientImp {!...!private ChatServer server;!
...!}!
Undirected Association
7/28/2019 CSE 70 UML Overview Classes
30/57
Ingolf Krueger, 2010 30CSE
public class ChatClientImp {!...!private ChatServer server;!
...!}!
Undirected Association
7/28/2019 CSE 70 UML Overview Classes
31/57
Ingolf Krueger, 2010 31CSE
public class ChatServer {!...!private ChatClientImp[] clients;!
...!}!
Undirected Association
7/28/2019 CSE 70 UML Overview Classes
32/57
Ingolf Krueger, 2010 32CSE
Frequently Used Multiplicities
Symbol Meaning
1 exactly one
n exactly n
n..m at least n and at most m* any number of
7/28/2019 CSE 70 UML Overview Classes
33/57
Ingolf Krueger, 2010 33CSE
Design Patterns:
Proven solutions forrecurring design problems
in specific contexts
7/28/2019 CSE 70 UML Overview Classes
34/57
Ingolf Krueger, 2010 34CSE
Why bother?
Lets Design a Chat Server
that can Handle Messages
7/28/2019 CSE 70 UML Overview Classes
35/57
Ingolf Krueger, 2010 35CSE
First Design
7/28/2019 CSE 70 UML Overview Classes
36/57
Ingolf Krueger, 2010 36CSE
Customer wants: Serverwith Message Logging
Capability
7/28/2019 CSE 70 UML Overview Classes
37/57
Ingolf Krueger, 2010 37CSE
Second Design: Message Logging Server
7/28/2019 CSE 70 UML Overview Classes
38/57
Ingolf Krueger, 2010 38CSE
Customer also wants:Server with MessageEncryption Capability
7/28/2019 CSE 70 UML Overview Classes
39/57
Ingolf Krueger, 2010 39CSE
Third Design: Logging OR Encryption
7/28/2019 CSE 70 UML Overview Classes
40/57
Ingolf Krueger, 2010 40CSE
Customer reallywants:Logging Server withEncryption Capability
7/28/2019 CSE 70 UML Overview Classes
41/57
Ingolf Krueger, 2010 41CSE
Fourth Design: Logging with Encryption
7/28/2019 CSE 70 UML Overview Classes
42/57
Ingolf Krueger, 2010 42CSE
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 andencryption capability!
Addition of further capabilities leads to exponential spread ofthe class hierarchy
What if we want to replace the logging/encryption algorithmat runtime: requires restart of entire server!
7/28/2019 CSE 70 UML Overview Classes
43/57
Ingolf Krueger, 2010 43CSE
Separation of Concerns
Let me try to explain to you, what to my taste is characteristic forall intelligent thinking. It is, that one is willing to study in depth anaspect of one's subject matter in isolation for the sake of its ownconsistency, all the time knowing that one is occupying oneselfonly with one of the aspects. [] But nothing is gained on thecontrary! 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 foreffective 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: APersonal Perspective, New York, NY, USA: Springer-Verlag New York, Inc., pp. 6066
7/28/2019 CSE 70 UML Overview Classes
44/57
Ingolf Krueger, 2010 44CSE
What are the separateconcerns of the Server
design?
Message Handling,Logging, Encryption
7/28/2019 CSE 70 UML Overview Classes
45/57
Ingolf Krueger, 2010 45CSE
How can we separate themout?
7/28/2019 CSE 70 UML Overview Classes
46/57
Ingolf Krueger, 2010 46CSE
Chat Server Interfaces: MessageHandler
7/28/2019 CSE 70 UML Overview Classes
47/57
Ingolf Krueger, 2010 47CSE
Chat Server Interfaces: Encryptor and Logger
7/28/2019 CSE 70 UML Overview Classes
48/57
Ingolf Krueger, 2010 48CSE
How can we bring themback together?
7/28/2019 CSE 70 UML Overview Classes
49/57
Ingolf Krueger, 2010 49CSE
Chat Server with Composition
7/28/2019 CSE 70 UML Overview Classes
50/57
Ingolf Krueger, 2010 50CSE
Example: Server with Composition
public class Server {!!private MessageHandler handler;!!private MessageLogger logger;!!private MessageEncryptor encryptor;!public Server() {!handler = null;!logger = null;!encryptor = null;!
} ...!
ChatServer.java
7/28/2019 CSE 70 UML Overview Classes
51/57
Ingolf Krueger, 2010 51CSE
Example: Server with Composition
...!public void setHandler(MessageHandler handler){this.handler = handler;!
}!
public void setLogger(MessageLogger logger) {!this.logger = logger;!
}!
public void setEncryptor(MessageEncryptor en) {this.encryptor = en;!
}!...!
Server.java
7/28/2019 CSE 70 UML Overview Classes
52/57
Ingolf Krueger, 2010 52CSE
Example: Server with Composition
...!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);!}!
}!
Server.java
7/28/2019 CSE 70 UML Overview Classes
53/57
Ingolf Krueger, 2010 53CSE
Example: RSAFileServer
public class RSAFileServer {!public RSAFileServer() {!super();!setHandler(new BasicHandler());!setLogger(new FileLogger());!setEncryptor(new RSAEncryptor()); !
} ...!
}!
RSAFileServer.java
7/28/2019 CSE 70 UML Overview Classes
54/57
Ingolf Krueger, 2010 54CSE
Key OO Design Principles
Develop Against Interfacesrather than
Implementations
Favor Composition overInheritance
7/28/2019 CSE 70 UML Overview Classes
55/57
Ingolf Krueger, 2010 55CSE
Separation of Concerns: Variability vs. Stability
Separate what varies from what doesnt.
OO Mechanisms:
Encapsulation (Classes/Interfaces)InheritanceAssociation/Aggregation/Composition
How to do it?Design Patterns to the Rescue
7/28/2019 CSE 70 UML Overview Classes
56/57
Ingolf Krueger, 2010 56CSE
What have you learnedtoday?
7/28/2019 CSE 70 UML Overview Classes
57/57
Ingolf Krueger, 2010 57CSE
Learning Goals
Know the use of the Unified Modeling Language (UML) forrequirements capture and design.
Be able to draw and understand basic UML class diagrams.
Know and be able to apply the Strategy Pattern