Design Patterns: Engr. Abdul-Rahman Mahmood
Design Patterns: Engr. Abdul-Rahman Mahmood
Design Patterns: Engr. Abdul-Rahman Mahmood
armahmood786@yahoo.com alphasecure@gmail.com
alphapeeler.sf.net/pubkeys/pkey.htm http://alphapeeler.sourceforge.net
pk.linkedin.com/in/armahmood http://alphapeeler.tumblr.com
www.twitter.com/alphapeeler armahmood786@jabber.org
www.facebook.com/alphapeeler alphapeeler@aim.com
abdulmahmood-sss alphasecure mahmood_cubix 48660186
armahmood786@hotmail.com alphapeeler@icloud.com
http://alphapeeler.sf.net/me http://alphapeeler.sf.net/acms/
Classes
A class is a description of a set of
objects that share the same attributes,
ClassName operations, relationships, and semantics.
attributes
operations
Class Attributes
Person
Person
Person
name : String
address : Address
birthdate : Date
ssn : Id
PhoneBook
You can specify an operation by stating its signature: listing the name, type, and
default value of all parameters, and, in the case of functions, a return type.
Class Responsibilities
A class may also include its responsibilities in a class diagram.
SmokeAlarm
Responsibilities
• dependencies
• generalizations
• associations
Dependency Relationships
A dependency indicates a semantic relationship between two or
more elements. The dependency from CourseSchedule to Course exists
because Course is used in both the add and remove operations of
CourseSchedule.
CourseSchedule
Course
add(c : Course)
remove(c : Course)
Generalization Relationships
Person
A generalization connects a subclass
to its superclass. It denotes an
inheritance of attributes and behavior
from the superclass to the subclass and
indicates a specialization in the subclass
of the more general superclass.
Student
Generalization Relationships
UML permits a class to inherit from multiple superclasses, although some
programming languages (e.g., Java) do not permit multiple inheritance.
Student Employee
TeachingAssistant
Association Relationships
If two classes in a model need to communicate with each other, there must be
link between them.
Student Instructor
Association Relationships
We can indicate the multiplicity of an association by adding multiplicity
adornments to the line denoting the association.
Student Instructor
1..*
Association Relationships
The example indicates that every Instructor has one or more Students:
Student Instructor
1..*
Association Relationships
We can also indicate the behavior of an object in an association (i.e., the role
of an object)
membership
Student Team
1..* 1..*
Association Relationships
We can specify dual associations.
member of
1 president of 1..*
Association Relationships
We can constrain the association relationship by defining the navigability of
the association. Here, a Router object requests services from a DNS object by
sending messages to (invoking the operations of) the server. The direction of
the association indicates that the server has no knowledge of the Router.
Router DomainNameServer
Association Relationships
Associations can also be objects themselves, called link classes or an
association classes.
Registration
modelNumber
serialNumber
warrentyCode
Product Warranty
Association Relationships
A class can have a self association.
Class
Scrollbar
1 1
Window Titlebar
1 1
Menu
1 1 ..
*
Interfaces
An interface is a named set of operations that
specifies the behavior of objects without
showing their inner structure. It can be
rendered in the model by a one- or two-
<<interface>>
compartment rectangle, with the stereotype
ControlPanel
<<interface>> above the interface name.
Interface Services
<<interface>> <<interface>>
Paser Paser
implementation
Print Setup
1public class A {
2
3 public void doSomething(B b) {
Aggregation
Now, if class A stored the reference to class B for later
use we would have a different relationship called
Aggregation. A more common and more obvious
example of Aggregation would be via setter injection:
1public class A {
2
3 private B _b;
4
5 public void setB(B b) { _b = b; }
Composition
Aggregation is the weaker form of object containment
(one object contains other objects). The stronger form
is called Composition. In Composition the
containing object is responsible for the creation and
life cycle of the contained object (either directly or
indirectly). Following are a few examples of
Composition. First, via member initialization:
1public class A {
1public class A {
2
2
3 private B _b;
3 private B _b = new B();
4
5 public A() {
6 _b = new B();
7 } // default constructor
Inheritance
1public class A {
2
3 ...
4
5} // class A
6
7public class B extends A {
8
9 ....
10
11} // class B
Realization
1public interface A {
2
3 ...
4
5} // interface A
6
7public class B implements A {
8
9 ...
10
11} // class B
Parameterized Class
A parameterized class or template
T
defines a family of potential elements.
LinkedList
To use it, the parameter must be bound.
T
1 .. *
A template is rendered by a small dashed
rectangle superimposed on the upper-right
corner of the class rectangle. The dashed
rectangle contains a list of formal
parameters for the class.
Example : Parameterized Class
<<bind>>(Name)
StudentList
Enumeration
<<exception>> <<exception>>
InterfaceException SQLException