SWEonline Design Patterns
SWEonline Design Patterns
1
UML Revisited
Generalization
Dependency: “uses”
2
Creational Patterns to be Covered
ꟷFactory method
ꟷAbstract factory
ꟷBuilder
ꟷSingleton
3
Factory Method
4
Solution
5
Example: Drawing
<<interface>> Client
Shape
+ draw()
6
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
Similar implementation for Square and Circle
7
Example: Kiosk
<<interface>> Kiosk
PaymentProcessor
+ processPayment()
GetPayment
Cash CreditCard Coupon +getProcessor(type):
PaymentProcessor
+ prcessPayment() + prcessPayment() + prcessPayment()
8
Factory
9
10
11
Consequences
• Factory design pattern provides approach to code for interface rather
than implementation.
• Factory pattern removes the instantiation of actual implementation
classes from client code. Factory pattern makes our code more
robust, less coupled and easy to extend. For example, we can easily
change lower class implementation because client program is
unaware of this.
• Factory pattern provides abstraction between implementation and
client classes through inheritance.
12
Abstract Factory
13
Solution
14
Example: Car Factory
<<interface>> Client
HondaFactory
+ createEngine()
+ createTrans()
Engine
CivicFactory AccordFactory
+ createEngine() + createEngine()
+ createTrans() + createTrans()
CivicEngine AccordEngine
Transmission
CivicTrans AccordTrans
15
https://www.tutorialspoint.com/design_pattern/design_pattern
_quick_guide.htm
16
17
18
Abstract Factory
19
20
21
22
23
24
Consequences
• Isolates the concrete class
• Makes exchanging product family easy
• Promotes consistency among products
• Abstract Factory design pattern provides approach to code for
interface rather than implementation.
• Abstract Factory pattern is “factory of factories” and can be easily
extended to accommodate more products.
• Abstract Factory pattern is robust and avoid conditional logic of
Factory pattern.
• Supporting new types of products is difficult
25
Singleton
26
Solution
SingletonClass
- instance: SingletonClass
- SingletonClass()
+ getInstance(): SingletonClass
27
Example: DB Connection Manger
public class DbConnection{
private DbConnection() {
connection = connectToDatabase(dbUser,dbPassword,dbName);
}
return instance;
}
}
DbConnection connection=DbConnection.getDbConnection();
28
Consequences
• Controlled access to sole instance
• Reduced namespace
29
Builder
30
Solution
31
Examples
To create a computer,
different parts are assembled
depending upon the order
received by the customer
(e.g., a customer can demand
a 500 GB hard disk with an
Intel processor; another
customer can choose a 250
GB hard disk with an AMD
processor).
32
Consequences
• Lets you vary a product’s internal representation
33
34
35
36
Builder
37
38
39
40
Structural Patterns
1
Motivation: Adapter
2
Adapter
3
Solution
4
Example: Car Factory
Client
<<interface>>
PowerAdapter
+connectPower()
5
6
7
8
Adapter
9
10
11
Consequences
• Allows pre-existing classes to be used in your code.
12
Motivation: Decorator
13
Decorator
14
Solution
15
16
17
18
Decorator
19
20
21
22
23
Consequences
• More flexibility than static inheritance
• Pay-as-you-go approach
24
Composite
25
Components
Composite Pattern consists of following
objects:
• Base Component – Base component is
the interface for all objects in the
composition, client program uses base
component to work with the objects in
the composition. It can be an interface or
an abstract class with some methods
common to all the objects.
• Leaf – Defines the behaviour for the
elements in the composition. It is the
building block for the composition and
implements base component. It doesn’t
have references to other Components.
• Composite – It consists of leaf elements
and implements the operations in base
component.
26
Example
27
28
29
30
31
32
33
34
35
36
Consequences
• Makes the client simple
37
State
1
Solution
2
Example
3
4
5
6
7
Consequences
• Localizes the state specific behavior
8
Motivation: Strategy
• Quick sort
• Merge sort
• Insertion sort
• Bubble sort
• Radix sort
• Heap sort
• Bucket sort
• ..
9
Strategy
10
Solution
11
12
13
14
15
16
17
Consequences
• Families of related algorithms
18
Motivation: Observer
19
Observer
20
Solution
21
22
23
Consequences
• Decoupling subject and observer
24
Many observers, many subjects
25
26
27
28
Mediator
29
30
31
32
33
34
35
36
37
38
Template
39
40
41
42
43
44
45
Command
46
47
48
49
50
51
52
53