SlideShare a Scribd company logo
1 of 61
Download to read offline
20++ Years of
Design PatternsDesign Patterns
Improve the Design of your Code
Dr Dimitris Dranidis
Dr Dimitris Dranidis
Senior Lecturer in Computer Science Department
Programme director of the
MSc in Software Engineering & Telecommunications
MSc in Business, Management, Technology and Innovation
2
MSc in Business, Management, Technology and Innovation
Leader of the "Software Engineering & Service-Oriented
Technologies" research group, r
Research interests:
service-oriented computing, Cloud computing
model-based testing, formal methods,
software engineering education
http://www.city.academic.gr/csd/dranidis/
Experience in Java/Design Patterns
Teaching Design Patterns for >15 years
Advanced Software Development Techniques
MSc in Software Engineering and Telecommunications
Principal Developer of JSXM
Model-based testing tool
http:www.jsxm.org
14 KLOC (JSXM core)
Plenty of Design Patterns applied
3
Outline
Quiz problem
Bad smells in design
Open/Closed principle
4
Open/Closed principle
Design Patterns
Examples of Design Patterns
Quiz solution
Key points
A PROBLEM TO KEEP YOU
BUSY!
5
Current Design
(http://bit.ly/1zwNf4R)
Your current implementation of a class DocumentPrinter is as follows:
class DocumentPrinter {
public void printDocument(Document doc) {
if (doc.getType().equals("PDF")) {
PDFPrinter pdfprinter = new PDFPrinter();PDFPrinter pdfprinter = new PDFPrinter();
pdfprinter.printPDF(doc.getContent());
}
else if (doc.getType().equals("HTML")) {
HTMLPrinter htmlprinter = new HTMLPrinter();
htmlprinter.printHTML(doc.getContent());
}
else
throw new RuntimeException("Wrong type of document");
}
}
6
Constraints
Library classes (These classes cannot be modified).
Document, PDFPrinter and HTMLPrinter
You are expecting other types of documents to beYou are expecting other types of documents to be
handled in the future and you would not like to change
your DocumentPrinter class continuously.
Change DocumentPrinter once and for all!
Make it open for extensions and closed to modifications!
7
What we wish to avoid:
class DocumentPrinter {
public void printDocument(Document doc) {
if (doc.getType().equals("PDF")) {
PDFPrinter pdfprinter = new PDFPrinter();
pdfprinter.printPDF(doc.getContent());
}}
else if (doc.getType().equals("HTML")) {
HTMLPrinter htmlprinter = new HTMLPrinter();
htmlprinter.printHTML(doc.getContent());
}
else if (doc.getType().equals(“XML")) {
XMLPrinter xmlprinter = new XMLPrinter();
xmlprinter.printXML(doc.getContent());
}
else
throw new RuntimeException("Wrong type of document");
}
}
8
DESIGN SMELLS
9
Design smells
Rigidity: Hard to change
A change forces many other unforeseen changes
“Changing it was a lot more complicated than I
thought”
Fragility: Easy to break
A change forces the application to break in oftenA change forces the application to break in often
unforeseen ways
Fragile modules: The more you fix them the worst
they get
Immobility: Hard to reuse
Useful parts are hard to separate
Viscosity: Hard to do the right thing
Many ways to deal with change; hacks are easier than
design-preserving ways
Caused by the software or the environment
10
Design smells
Needless complexity: Overdesign
Not useful (even unused) design
elements
Dealing with anticipated requirements
Needless repetition: Cut & Paste abuseNeedless repetition: Cut & Paste abuse
Redundant code
Bugs require repetitive and different fixes
Opacity: Difficult to understand
Easy when you write it, difficult when you
read it
11
OPEN-CLOSED PRINCIPLE
12
OCP: Open-Closed Principle
A module should be
Open for extension
Closed for modification
How can we extend the client so that it works with
other servers (OPEN) without having to modify the
client (CLOSED)?
Client Server
13
OCP: Abstraction is the key!
Client
«interface»
ClientInterface
Bertrand Meyer 97
Server
14
DESIGN PATTERNS
15
What are Design patterns?
Well-known best design
practices
Let’s apply a Factory
Named solutions to design
problems by experienced
developers
Vocabulary of principles to
design software
Let’s apply a Factory
and a Strategy!
16
A Pattern Language: Towns,
Buildings, Construction (1977)
Book on architecture, urban
design, and community
livability.
Patterns describe a problem
and then offer a solution.and then offer a solution.
Still one of the best-selling
books on architecture
17
Gang of Four (GoF) Design
Patterns (1994)
23 patterns categorized in
Creational
Structural
Behavioral
18
Quotes from the Book
Program to an interface, not an
implementation
Favor object composition over class
inheritanceinheritance
Inheritance breaks encapsulation
Implement abstract classes,
do not inherit concrete classes!
19
22 PLoP conferences (1994-today)
Pattern Languages of Programs
Annual conferences
*PLoP*PLoP
20
Patterns in core Java
All patterns have been implemented
in Java’s core libraries:
http://stackoverflow.com/questions/167
3841/examples-of-gof-design-patterns-3841/examples-of-gof-design-patterns-
in-javas-core-libraries
or google:
Examples of GoF Design Patterns in
Java
21
DESIGN PATTERNS
EXAMPLES
22
SHAPES EXAMPLE
23
Shapes example
public class Rectangle {
private double width;
private double height;
public Rectangle ( double w, double h ) {
width = w ;
height = h ;
}
public double getArea() { return width * height ; }
}
24
Shapes example
public class AreaPrinter {
private Rectangle shape;
public AreaPrinter (Rectangle s ) { shape = s ; }
public void printArea () {
System.out.println( "Shape area : " + shape.getArea() ) ;System.out.println( "Shape area : " + shape.getArea() ) ;
}
}
public class Main {
public static void main( String arg[] ) {
Rectangle rectangle = new Rectangle(2.0, 5.0);
AreaPrinter ap = new AreaPrinter( rectangle ) ;
ap.printArea () ;
}
}
25
Desired extensions
We want our application to work with other types of
shapes: Squares, Triangles, Circles, Polygons, etc.
public class Square {
private double width;private double width;
public Square ( double w ) {
width = w ;
}
public double getArea() { return width * width ; }
}
Do we need another AreaPrinter for Square?
Can we make AreaPrinter work for both Rectangle
and Square?
26
Abstraction is the key!
public interface Shape {
public double getArea();
}
public class AreaPrinter {public class AreaPrinter {
private Shape shape;
public AreaPrinter ( Shape s ) { shape = s ; }
public void printArea () {
System.out.println( "Shape area : " + shape.getArea() ) ;
}
}
AreaPrinter is now open to extensions, closed to modifications!
27
Concrete classes implement the
desired interface
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle ( double w, double h ) {
width = w ;
height = h ;height = h ;
}
public double getArea() { return width * height ; }
}
public class Square implements Shape {
private double width;
public Square ( double w ) {
width = w ;
}
public double getArea() { return width * width ; }
}
28
Polygons?
Polygons’ area is hard to calculate! We would not like to write
the code ourselves.
Luckily we find a class Polygon which offers the calculation of
its objects’ area.
Source code not available OR
We don’t wish to change the implementation of PolygonWe don’t wish to change the implementation of Polygon
public class Polygon {
......
public Polygon ( List points ) { .... }
public double computeArea () { .... }
......
}
How can we integrate polygons into our Shapes application?
29
Adapter Design Pattern
public class PolygonAdapter implements Shape {
private Polygon polygon; // composition
public PolygonAdapter (Polygon c ) {
polygon = c ;
}
public double getArea () {
return polygon.computeArea() ; // indirection
}
}
30
Main client
public class Main {
public static void main( String arg[] ) {
Rectangle rectangle = new Rectangle(2.0, 5.0);
AreaPrinter ap = new AreaPrinter( rectangle ) ;
ap.printArea () ;ap.printArea () ;
Square square = new Square (3.0);
AreaPrinter sp = new AreaPrinter( square ) ;
sp.printArea () ;
Polygon polygon = new Polygon (......);
AreaPrinter pp = new AreaPrinter (
new PolygonAdapter(polygon));
pp.printArea () ;
}
}
31
Adapter (GoF, Structural Pattern)
Intent
convert the interface of an existing class into another interface
used by clients
Motivation:
want to use an existing class but ...want to use an existing class but ...
interface of class is not compatible to the one you use and
modification of class is out of question because
source code of class is not available or
it is a general-purpose class (you do not want to modify)
resolve incompatible interfaces
provide a stable interface to similar classes with different
interfaces
32
Problem
Adaptee
+specificRequest()
<<interface>>
Target
+request()
Client
+specificRequest()+request()
incompatible
interfaces
33
Adaptee
+specificRequest()
<<interface>>
Target
+request()
Client
Adapter Solution
+specificRequest()
specificRequest()
+request()
Adapter
+request()
34
Adapter Solution
:Adaptee:Adapter:Client
Target
specificRequest()2:
request()1:
35
EMPLOYEE EXAMPLE
36
Current design
public class Employee {
private double salary ;
public Employee (double s) {
salary = s ;
}}
public double getPayAmount () {
return salary ;
}
}
Assume that some privileged employees receive double salary
Assume that employees can be promoted
How can we extend Employee to protect it from changes?
37
2 possible solutions
Sub-typing (Inheritance)
Include a type attribute
38
Sub-typing solution (??)
public class Manager extends Employee {
public double getPayAmount () {
return 2 * salary ;return 2 * salary ;
}
}
39
Sub-typing solution does not work!
Objects cannot dynamically change their type
An Employee object cannot be converted to a
Manager object
More points of variation create class explosion!More points of variation create class explosion!
Different types of employees could have different
promotion eligibility criteria
Leading to a huge
inheritance hierarchy tree.
40
Type attribute solution (??)
(Too smelly, violates OCP)
public class Employee {
private int type ;
private double salary ;
public static final int ENGINEER = 1;
public static final int MANAGER = 2;public static final int MANAGER = 2;
public Employee (double s, int t) { salary = s ; type = t ;}
public double getPayAmount () {
if (type == Employee.ENGINEER)
return salary;
if (type == Employee.MANAGER)
return salary * 2;
return salary ;
}
} 41
Strategy Pattern solution
public interface PaymentStrategy {
public double getPayAmount (Employee context);
}
public class EngineerPaymentStrategy implements PaymentStrategy {
public double getPayAmount (Employee empl) {
return empl.getSalary() ;
}
}
public class ManagerPaymentStrategy implements PaymentStrategy {
public double getPayAmount (Employee empl) {
return empl.getSalary() * 2 ;
}
} 42
Strategy Pattern solution
public class Employee {
private PaymentStrategy paymentStrategy ; // composition
private double salary ;
public Employee (double s, PaymentStrategy strategy) {
salary = s ; paymentStrategy = strategy ;
}
public double getSalary () {return salary ;}
public double getPayAmount () {
return paymentStrategy.getPayAmount(this) ; // indirection
}
}
43
Strategy (GoF: Behavioral)
Intent
define a family of related algorithms, encapsulate each one, and
make them interchangeable by providing a common interface
algorithm may vary by object and also may vary over time
Motivation:Motivation:
Many different algorithms exist for fulfilling a responsibility
You need different variants of an algorithm
An algorithm uses data that clients shouldn't know about
A class defines many behaviors, and these appear as multiple
switch statements in the classes operations
Many related classes differ only in their behavior
44
Strategy (GoF)
Motivation (cont):
Separate the algorithms from the object which
is using them becauseis using them because
an object may decide to change the algorithm it uses
an object will get complex if you include all the code of the
algorithms
an object will have to use conditionals to decide which algorithm it
will use
each algorithm uses its own algorithm-specific data
it is difficult to add new algorithms if they are hard coded in the
object.
45
Strategy Solution
<<interface>>
Strategy
Context
46
Strategy
+algorithmInterface( context : Context )
strategy.algorithmInterface(this)
ConcreteStrategyA
+algorithmInterface( context )
ConcreteStrategyB
+algorithmInterface( context )
+contextInterface()
KEY POINTS
47
When should you use patterns
You have to add some new
functionality and you find out
that:
You need to copy and paste codeYou need to copy and paste code
You need to hack the solution
Current design does not allow
easy fit of the new functionality
48
Protect your code from changes
Identify the variation points
Isolate the variation points,
separate them from what is stable.
Encapsulate variations in a
method/class
method encapsulation
Template method, Factory method
class encapsulation
Strategy, Abstract Factory
49
When NOT to use Design Patterns
Patterns introduce complexity
Simple problems require
simple solutions
Over-conformance to goodOver-conformance to good
design principles leads to
“Needless Complexity”
Apply Design Patterns only
when it is necessary!
Remember:
Not everything is a nail!
50
SOLUTION TO PROBLEM
51
Current Design
(http://bit.ly/1zwNf4R)
class DocumentPrinter {
public void printDocument(Document doc) {
if (doc.getType().equals("PDF")) {
PDFPrinter pdfprinter = new PDFPrinter();
pdfprinter.printPDF(doc.getContent());pdfprinter.printPDF(doc.getContent());
}
else if (doc.getType().equals("HTML")) {
HTMLPrinter htmlprinter = new HTMLPrinter();
htmlprinter.printHTML(doc.getContent());
}
else
throw new RuntimeException("Wrong type of document");
}
}
52
Analysis of the problem
Variation points:
Objects created:
PDFPrinter pdfprinter = new PDFPrinter();PDFPrinter pdfprinter = new PDFPrinter();
HTMLPrinter htmlprinter = new HTMLPrinter();
Interface of objects:
pdfprinter.printPDF(doc.getContent());
htmlprinter.printHTML(doc.getContent());
53
Adapter
(Providing a stable interface)
public interface Printer {
public void print (String content);
}
public class PDFPrinterAdapter implements Printer {public class PDFPrinterAdapter implements Printer {
public void print (String content) {
PDFPrinter pdfprinter = new PDFPrinter();
pdfprinter.printPDF(content);
}
}
… similar for HTMLPrinterAdapter
54
Factory (GoF Creational)
(Provides a stable interface for object creation)
public class PrinterFactory {
Printer makePrinter (String doctype) {
if (doctype.equals("PDF")) {
return new PDFPrinterAdapter();
}
else if (doctype.equals("HTML")) {
return new HTMLPrinterAdapter()
}
else throw new RuntimeException("……");
}
}
55
Singleton (GoF Creational)
public class PrinterFactory {
static PrinterFactory printerFactory = new PrinterFactory();
protected PrinterFactory() {}
static PrinterFactory getInstance() { return printerFactory; }
Printer makePrinter(String doctype) {
….
}
}
56
Final DocumentPrinter
(Open for extensions, closed for modifications)
class DocumentPrinter {
public void printDocument(Document doc) {
PrinterFactory printerFactory = PrinterFactory.getInstance();
Printer printer = printerFactory.makePrinter (doc.getType());
printer.print (doc.getContent());
}
}
57
… or as one-liner!
class DocumentPrinter {
public void printDocument(Document doc) {
PrinterFactory.getInstance().makePrinter (doc.getType()).
print (doc.getContent());print (doc.getContent());
}
}
58
How to learn Design Patterns
Pick a pattern and then find, and read as many
examples as possible
Focus on the problems the patterns solves,
NOT the solution!NOT the solution!
Invent your own
examples/problems
Apply the Design Pattern
… and enjoy!
59
How to apply Design Patterns
Follow a problem-directed approach:
Identification of the pattern needed:
understand the problem
find a pattern that addresses the problem (or a similarfind a pattern that addresses the problem (or a similar
problem)
Study the Pattern solution
UML diagrams
code example (C++, Java, …)
Apply the solution to the problem
embed pattern name in class/method names
60
Recommended Readings
Design Patterns, GoF
Refactoring, Martin Fowler
Agile Software Development,
Robert Martin
61

More Related Content

Similar to Design patters java_meetup_slideshare [compatibility mode]

Webinar on Design Patterns titled 'Dive into Design Patterns'
Webinar on Design Patterns titled 'Dive into Design Patterns'Webinar on Design Patterns titled 'Dive into Design Patterns'
Webinar on Design Patterns titled 'Dive into Design Patterns'Edureka!
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General IntroductionAsma CHERIF
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patternsOlivier Bacs
 
P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Design Patterns : The Ultimate Blueprint for Software
Design Patterns : The Ultimate Blueprint for SoftwareDesign Patterns : The Ultimate Blueprint for Software
Design Patterns : The Ultimate Blueprint for SoftwareEdureka!
 
Object-Oriented Application Frameworks
Object-Oriented Application FrameworksObject-Oriented Application Frameworks
Object-Oriented Application Frameworkskim.mens
 
Lessons learned teaching a groovy grails course
Lessons learned teaching a groovy grails courseLessons learned teaching a groovy grails course
Lessons learned teaching a groovy grails courseJacobAae
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfAnassElHousni
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Groupbrada
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your codevmandrychenko
 
Software Abstractions for Parallel Hardware
Software Abstractions for Parallel HardwareSoftware Abstractions for Parallel Hardware
Software Abstractions for Parallel HardwareJoel Falcou
 
Software development effort reduction with Co-op
Software development effort reduction with Co-opSoftware development effort reduction with Co-op
Software development effort reduction with Co-oplbergmans
 
CS 354 Introduction
CS 354 IntroductionCS 354 Introduction
CS 354 IntroductionMark Kilgard
 

Similar to Design patters java_meetup_slideshare [compatibility mode] (20)

Webinar on Design Patterns titled 'Dive into Design Patterns'
Webinar on Design Patterns titled 'Dive into Design Patterns'Webinar on Design Patterns titled 'Dive into Design Patterns'
Webinar on Design Patterns titled 'Dive into Design Patterns'
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Design Patterns : The Ultimate Blueprint for Software
Design Patterns : The Ultimate Blueprint for SoftwareDesign Patterns : The Ultimate Blueprint for Software
Design Patterns : The Ultimate Blueprint for Software
 
Object-Oriented Application Frameworks
Object-Oriented Application FrameworksObject-Oriented Application Frameworks
Object-Oriented Application Frameworks
 
Lessons learned teaching a groovy grails course
Lessons learned teaching a groovy grails courseLessons learned teaching a groovy grails course
Lessons learned teaching a groovy grails course
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
Design pattern
Design patternDesign pattern
Design pattern
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your code
 
MDE in Practice
MDE in PracticeMDE in Practice
MDE in Practice
 
Need 4 Speed FI
Need 4 Speed FINeed 4 Speed FI
Need 4 Speed FI
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Software Abstractions for Parallel Hardware
Software Abstractions for Parallel HardwareSoftware Abstractions for Parallel Hardware
Software Abstractions for Parallel Hardware
 
Software development effort reduction with Co-op
Software development effort reduction with Co-opSoftware development effort reduction with Co-op
Software development effort reduction with Co-op
 
CS 354 Introduction
CS 354 IntroductionCS 354 Introduction
CS 354 Introduction
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 

Recently uploaded

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 

Recently uploaded (20)

Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 

Design patters java_meetup_slideshare [compatibility mode]

  • 1. 20++ Years of Design PatternsDesign Patterns Improve the Design of your Code Dr Dimitris Dranidis
  • 2. Dr Dimitris Dranidis Senior Lecturer in Computer Science Department Programme director of the MSc in Software Engineering & Telecommunications MSc in Business, Management, Technology and Innovation 2 MSc in Business, Management, Technology and Innovation Leader of the "Software Engineering & Service-Oriented Technologies" research group, r Research interests: service-oriented computing, Cloud computing model-based testing, formal methods, software engineering education http://www.city.academic.gr/csd/dranidis/
  • 3. Experience in Java/Design Patterns Teaching Design Patterns for >15 years Advanced Software Development Techniques MSc in Software Engineering and Telecommunications Principal Developer of JSXM Model-based testing tool http:www.jsxm.org 14 KLOC (JSXM core) Plenty of Design Patterns applied 3
  • 4. Outline Quiz problem Bad smells in design Open/Closed principle 4 Open/Closed principle Design Patterns Examples of Design Patterns Quiz solution Key points
  • 5. A PROBLEM TO KEEP YOU BUSY! 5
  • 6. Current Design (http://bit.ly/1zwNf4R) Your current implementation of a class DocumentPrinter is as follows: class DocumentPrinter { public void printDocument(Document doc) { if (doc.getType().equals("PDF")) { PDFPrinter pdfprinter = new PDFPrinter();PDFPrinter pdfprinter = new PDFPrinter(); pdfprinter.printPDF(doc.getContent()); } else if (doc.getType().equals("HTML")) { HTMLPrinter htmlprinter = new HTMLPrinter(); htmlprinter.printHTML(doc.getContent()); } else throw new RuntimeException("Wrong type of document"); } } 6
  • 7. Constraints Library classes (These classes cannot be modified). Document, PDFPrinter and HTMLPrinter You are expecting other types of documents to beYou are expecting other types of documents to be handled in the future and you would not like to change your DocumentPrinter class continuously. Change DocumentPrinter once and for all! Make it open for extensions and closed to modifications! 7
  • 8. What we wish to avoid: class DocumentPrinter { public void printDocument(Document doc) { if (doc.getType().equals("PDF")) { PDFPrinter pdfprinter = new PDFPrinter(); pdfprinter.printPDF(doc.getContent()); }} else if (doc.getType().equals("HTML")) { HTMLPrinter htmlprinter = new HTMLPrinter(); htmlprinter.printHTML(doc.getContent()); } else if (doc.getType().equals(“XML")) { XMLPrinter xmlprinter = new XMLPrinter(); xmlprinter.printXML(doc.getContent()); } else throw new RuntimeException("Wrong type of document"); } } 8
  • 10. Design smells Rigidity: Hard to change A change forces many other unforeseen changes “Changing it was a lot more complicated than I thought” Fragility: Easy to break A change forces the application to break in oftenA change forces the application to break in often unforeseen ways Fragile modules: The more you fix them the worst they get Immobility: Hard to reuse Useful parts are hard to separate Viscosity: Hard to do the right thing Many ways to deal with change; hacks are easier than design-preserving ways Caused by the software or the environment 10
  • 11. Design smells Needless complexity: Overdesign Not useful (even unused) design elements Dealing with anticipated requirements Needless repetition: Cut & Paste abuseNeedless repetition: Cut & Paste abuse Redundant code Bugs require repetitive and different fixes Opacity: Difficult to understand Easy when you write it, difficult when you read it 11
  • 13. OCP: Open-Closed Principle A module should be Open for extension Closed for modification How can we extend the client so that it works with other servers (OPEN) without having to modify the client (CLOSED)? Client Server 13
  • 14. OCP: Abstraction is the key! Client «interface» ClientInterface Bertrand Meyer 97 Server 14
  • 16. What are Design patterns? Well-known best design practices Let’s apply a Factory Named solutions to design problems by experienced developers Vocabulary of principles to design software Let’s apply a Factory and a Strategy! 16
  • 17. A Pattern Language: Towns, Buildings, Construction (1977) Book on architecture, urban design, and community livability. Patterns describe a problem and then offer a solution.and then offer a solution. Still one of the best-selling books on architecture 17
  • 18. Gang of Four (GoF) Design Patterns (1994) 23 patterns categorized in Creational Structural Behavioral 18
  • 19. Quotes from the Book Program to an interface, not an implementation Favor object composition over class inheritanceinheritance Inheritance breaks encapsulation Implement abstract classes, do not inherit concrete classes! 19
  • 20. 22 PLoP conferences (1994-today) Pattern Languages of Programs Annual conferences *PLoP*PLoP 20
  • 21. Patterns in core Java All patterns have been implemented in Java’s core libraries: http://stackoverflow.com/questions/167 3841/examples-of-gof-design-patterns-3841/examples-of-gof-design-patterns- in-javas-core-libraries or google: Examples of GoF Design Patterns in Java 21
  • 24. Shapes example public class Rectangle { private double width; private double height; public Rectangle ( double w, double h ) { width = w ; height = h ; } public double getArea() { return width * height ; } } 24
  • 25. Shapes example public class AreaPrinter { private Rectangle shape; public AreaPrinter (Rectangle s ) { shape = s ; } public void printArea () { System.out.println( "Shape area : " + shape.getArea() ) ;System.out.println( "Shape area : " + shape.getArea() ) ; } } public class Main { public static void main( String arg[] ) { Rectangle rectangle = new Rectangle(2.0, 5.0); AreaPrinter ap = new AreaPrinter( rectangle ) ; ap.printArea () ; } } 25
  • 26. Desired extensions We want our application to work with other types of shapes: Squares, Triangles, Circles, Polygons, etc. public class Square { private double width;private double width; public Square ( double w ) { width = w ; } public double getArea() { return width * width ; } } Do we need another AreaPrinter for Square? Can we make AreaPrinter work for both Rectangle and Square? 26
  • 27. Abstraction is the key! public interface Shape { public double getArea(); } public class AreaPrinter {public class AreaPrinter { private Shape shape; public AreaPrinter ( Shape s ) { shape = s ; } public void printArea () { System.out.println( "Shape area : " + shape.getArea() ) ; } } AreaPrinter is now open to extensions, closed to modifications! 27
  • 28. Concrete classes implement the desired interface public class Rectangle implements Shape { private double width; private double height; public Rectangle ( double w, double h ) { width = w ; height = h ;height = h ; } public double getArea() { return width * height ; } } public class Square implements Shape { private double width; public Square ( double w ) { width = w ; } public double getArea() { return width * width ; } } 28
  • 29. Polygons? Polygons’ area is hard to calculate! We would not like to write the code ourselves. Luckily we find a class Polygon which offers the calculation of its objects’ area. Source code not available OR We don’t wish to change the implementation of PolygonWe don’t wish to change the implementation of Polygon public class Polygon { ...... public Polygon ( List points ) { .... } public double computeArea () { .... } ...... } How can we integrate polygons into our Shapes application? 29
  • 30. Adapter Design Pattern public class PolygonAdapter implements Shape { private Polygon polygon; // composition public PolygonAdapter (Polygon c ) { polygon = c ; } public double getArea () { return polygon.computeArea() ; // indirection } } 30
  • 31. Main client public class Main { public static void main( String arg[] ) { Rectangle rectangle = new Rectangle(2.0, 5.0); AreaPrinter ap = new AreaPrinter( rectangle ) ; ap.printArea () ;ap.printArea () ; Square square = new Square (3.0); AreaPrinter sp = new AreaPrinter( square ) ; sp.printArea () ; Polygon polygon = new Polygon (......); AreaPrinter pp = new AreaPrinter ( new PolygonAdapter(polygon)); pp.printArea () ; } } 31
  • 32. Adapter (GoF, Structural Pattern) Intent convert the interface of an existing class into another interface used by clients Motivation: want to use an existing class but ...want to use an existing class but ... interface of class is not compatible to the one you use and modification of class is out of question because source code of class is not available or it is a general-purpose class (you do not want to modify) resolve incompatible interfaces provide a stable interface to similar classes with different interfaces 32
  • 37. Current design public class Employee { private double salary ; public Employee (double s) { salary = s ; }} public double getPayAmount () { return salary ; } } Assume that some privileged employees receive double salary Assume that employees can be promoted How can we extend Employee to protect it from changes? 37
  • 38. 2 possible solutions Sub-typing (Inheritance) Include a type attribute 38
  • 39. Sub-typing solution (??) public class Manager extends Employee { public double getPayAmount () { return 2 * salary ;return 2 * salary ; } } 39
  • 40. Sub-typing solution does not work! Objects cannot dynamically change their type An Employee object cannot be converted to a Manager object More points of variation create class explosion!More points of variation create class explosion! Different types of employees could have different promotion eligibility criteria Leading to a huge inheritance hierarchy tree. 40
  • 41. Type attribute solution (??) (Too smelly, violates OCP) public class Employee { private int type ; private double salary ; public static final int ENGINEER = 1; public static final int MANAGER = 2;public static final int MANAGER = 2; public Employee (double s, int t) { salary = s ; type = t ;} public double getPayAmount () { if (type == Employee.ENGINEER) return salary; if (type == Employee.MANAGER) return salary * 2; return salary ; } } 41
  • 42. Strategy Pattern solution public interface PaymentStrategy { public double getPayAmount (Employee context); } public class EngineerPaymentStrategy implements PaymentStrategy { public double getPayAmount (Employee empl) { return empl.getSalary() ; } } public class ManagerPaymentStrategy implements PaymentStrategy { public double getPayAmount (Employee empl) { return empl.getSalary() * 2 ; } } 42
  • 43. Strategy Pattern solution public class Employee { private PaymentStrategy paymentStrategy ; // composition private double salary ; public Employee (double s, PaymentStrategy strategy) { salary = s ; paymentStrategy = strategy ; } public double getSalary () {return salary ;} public double getPayAmount () { return paymentStrategy.getPayAmount(this) ; // indirection } } 43
  • 44. Strategy (GoF: Behavioral) Intent define a family of related algorithms, encapsulate each one, and make them interchangeable by providing a common interface algorithm may vary by object and also may vary over time Motivation:Motivation: Many different algorithms exist for fulfilling a responsibility You need different variants of an algorithm An algorithm uses data that clients shouldn't know about A class defines many behaviors, and these appear as multiple switch statements in the classes operations Many related classes differ only in their behavior 44
  • 45. Strategy (GoF) Motivation (cont): Separate the algorithms from the object which is using them becauseis using them because an object may decide to change the algorithm it uses an object will get complex if you include all the code of the algorithms an object will have to use conditionals to decide which algorithm it will use each algorithm uses its own algorithm-specific data it is difficult to add new algorithms if they are hard coded in the object. 45
  • 46. Strategy Solution <<interface>> Strategy Context 46 Strategy +algorithmInterface( context : Context ) strategy.algorithmInterface(this) ConcreteStrategyA +algorithmInterface( context ) ConcreteStrategyB +algorithmInterface( context ) +contextInterface()
  • 48. When should you use patterns You have to add some new functionality and you find out that: You need to copy and paste codeYou need to copy and paste code You need to hack the solution Current design does not allow easy fit of the new functionality 48
  • 49. Protect your code from changes Identify the variation points Isolate the variation points, separate them from what is stable. Encapsulate variations in a method/class method encapsulation Template method, Factory method class encapsulation Strategy, Abstract Factory 49
  • 50. When NOT to use Design Patterns Patterns introduce complexity Simple problems require simple solutions Over-conformance to goodOver-conformance to good design principles leads to “Needless Complexity” Apply Design Patterns only when it is necessary! Remember: Not everything is a nail! 50
  • 52. Current Design (http://bit.ly/1zwNf4R) class DocumentPrinter { public void printDocument(Document doc) { if (doc.getType().equals("PDF")) { PDFPrinter pdfprinter = new PDFPrinter(); pdfprinter.printPDF(doc.getContent());pdfprinter.printPDF(doc.getContent()); } else if (doc.getType().equals("HTML")) { HTMLPrinter htmlprinter = new HTMLPrinter(); htmlprinter.printHTML(doc.getContent()); } else throw new RuntimeException("Wrong type of document"); } } 52
  • 53. Analysis of the problem Variation points: Objects created: PDFPrinter pdfprinter = new PDFPrinter();PDFPrinter pdfprinter = new PDFPrinter(); HTMLPrinter htmlprinter = new HTMLPrinter(); Interface of objects: pdfprinter.printPDF(doc.getContent()); htmlprinter.printHTML(doc.getContent()); 53
  • 54. Adapter (Providing a stable interface) public interface Printer { public void print (String content); } public class PDFPrinterAdapter implements Printer {public class PDFPrinterAdapter implements Printer { public void print (String content) { PDFPrinter pdfprinter = new PDFPrinter(); pdfprinter.printPDF(content); } } … similar for HTMLPrinterAdapter 54
  • 55. Factory (GoF Creational) (Provides a stable interface for object creation) public class PrinterFactory { Printer makePrinter (String doctype) { if (doctype.equals("PDF")) { return new PDFPrinterAdapter(); } else if (doctype.equals("HTML")) { return new HTMLPrinterAdapter() } else throw new RuntimeException("……"); } } 55
  • 56. Singleton (GoF Creational) public class PrinterFactory { static PrinterFactory printerFactory = new PrinterFactory(); protected PrinterFactory() {} static PrinterFactory getInstance() { return printerFactory; } Printer makePrinter(String doctype) { …. } } 56
  • 57. Final DocumentPrinter (Open for extensions, closed for modifications) class DocumentPrinter { public void printDocument(Document doc) { PrinterFactory printerFactory = PrinterFactory.getInstance(); Printer printer = printerFactory.makePrinter (doc.getType()); printer.print (doc.getContent()); } } 57
  • 58. … or as one-liner! class DocumentPrinter { public void printDocument(Document doc) { PrinterFactory.getInstance().makePrinter (doc.getType()). print (doc.getContent());print (doc.getContent()); } } 58
  • 59. How to learn Design Patterns Pick a pattern and then find, and read as many examples as possible Focus on the problems the patterns solves, NOT the solution!NOT the solution! Invent your own examples/problems Apply the Design Pattern … and enjoy! 59
  • 60. How to apply Design Patterns Follow a problem-directed approach: Identification of the pattern needed: understand the problem find a pattern that addresses the problem (or a similarfind a pattern that addresses the problem (or a similar problem) Study the Pattern solution UML diagrams code example (C++, Java, …) Apply the solution to the problem embed pattern name in class/method names 60
  • 61. Recommended Readings Design Patterns, GoF Refactoring, Martin Fowler Agile Software Development, Robert Martin 61