SlideShare a Scribd company logo
1 of 13
Download to read offline
GoF
Gang of Four (Design Patterns) in Java
Mina Tafreshi
Software Engineer @ Bol.com
What is a design pattern?
● Design pattern is a general repeatable solution to common problems in
software design.
● A design pattern is not a finished design.
● These patterns are mostly “evolved” rather than “discovered”.
[Note]: Code implementation is your responsibility!
Gang of Four Design Patterns
● Name?
Element of Reusable
Object-Oriented Software.
● By whom?
Erich Gamma, Richard Helm,
Ralph Johnson and John Vlissides
● When?
1994
GoF Design Patterns categories
1. Creational design patterns:
It deal with object creation. They look at ways to solve design issues arising out of creation of objects.
2. Structural design patterns:
Ease the design by identifying a simple way to realize relationships between entities.
3. Behavioral design patterns:
Identify common communication patterns between objects and realize these patterns.
Singleton Design Pattern Ensures only one instance of an object is created.
Factory Design Pattern Creates objects without specifying the exact class
to create.
Abstract Factory Design
Pattern
Allows for the creation of objects without
specifying their concrete type.
Builder Design Pattern Separate the construction of a complex object
from its representation so that the same
construction process can create different
representations.
Prototype Design Pattern Creates a new object from an existing object.
Adapter Design Pattern Convert the interface of a class into another
interface the clients expect.
Bridge Design Pattern Decouple an abstraction from its implementation.
Composite Design Pattern Compose objects into tree structures to represent
part-whole hierarchies.
Decorator Design Pattern Attach additional responsibilities to an object
dynamically.
Facade Design Pattern Facade defines a higher-level interface that makes
the subsystem easier to use.
Flyweight Design Pattern Use sharing to support large numbers of
fine-grained objects efficiently.
Proxy Design Pattern Provide a surrogate or placeholder for another
object to control access to it.
Command Design Pattern Encapsulate a request as an object.
Iterator Design Pattern Accesses the elements of an object sequentially
without exposing its underlying representation
Mediator Design Pattern Allows loose coupling between classes by being the
only class that has detailed knowledge of their
methods.
Memento Design Pattern Provides the ability to restore an object to its previous
state.
Observer Design Pattern publish/subscribe pattern which allows a number of
observer objects to see an event.
State Design Pattern Allow an object to alter its behaviour when its internal
state changes.
Strategy Design Pattern Define a family of algorithms, encapsulate each one,
and make them interchangeable.
Template Method Design Pattern lets subclasses redefine certain steps of an algorithm
without changing the algorithm's structure.
Delegation Pattern where an object expresses certain behavior to the
outside but in reality delegates responsibility for
implementing that behaviour to an associated object.
Singleton Design Pattern:
Ensures only one instance of an object is created
When:
A class in your program should have just a single instance available to all clients.
Why:
The most common reason for this is to control access to some shared resource.
Examples in Java core libraries:
● java.lang.Runtime#getRuntime()
● java.awt.Desktop#getDesktop()
● java.lang.System#getSecurityManager()
Factory Design Pattern:
Creates objects without specifying the exact class to create
When:
You don’t know beforehand the exact types and dependencies of the objects your code should work with.
Why:
The most common reason for this is to control access to some shared resource.
Examples in Java core libraries:
● java.util.Calendar#getInstance()
● java.util.ResourceBundle#getBundle()
● java.text.NumberFormat#getInstance()
● java.nio.charset.Charset#forName()
● java.net.URLStreamHandlerFactory#createURLStreamHandler(String)
● javax.xml.bind.JAXBContext#createMarshaller()
Builder Design Pattern:
Separate the construction of a complex object from its representation
When:
You want your code to be able to create different representations of some objects.
Why:
It lets you build objects step by step, using only those steps that you really need.
Examples in Java core libraries:
● java.lang.StringBuilder#append() (unsynchronized)
● java.lang.StringBuffer#append() (synchronized)
● java.nio.ByteBuffer#put()
( in CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer)
● javax.swing.GroupLayout.Group#addComponent()
● All implementations java.lang.Appendable
Prototype Design Pattern:
Creates a new object from an existing object
When:
Your code shouldn’t depend on the concrete classes of objects that you need to copy
Why:
you want to reduce the number of subclasses that only differ in the way they initialize their respective objects
Examples in Java core libraries:
The Prototype pattern is available in Java out of the box with
a Cloneable interface.
Any class can implement this interface to become cloneable.
● java.lang.Object#clone()
(class should implement the java.lang.Cloneable interface)
Resources:
1. GoF (https://en.wikipedia.org/wiki/Design_Patterns)
2. https://refactoring.guru/design-patterns/
3. Head First Design Patterns (https://www.oreilly.com/library/view/head-first-design/0596007124/)
Email:
mina@tafreshi.me

More Related Content

What's hot

Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cycle
Slideshare
 
Object Design - Part 1
Object Design - Part 1Object Design - Part 1
Object Design - Part 1
Dhaval Dalal
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
11prasoon
 

What's hot (20)

Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Factory Design Pattern
Factory Design PatternFactory Design Pattern
Factory Design Pattern
 
Software Development Life Cycle
Software Development Life CycleSoftware Development Life Cycle
Software Development Life Cycle
 
Html 5 tags
Html  5 tagsHtml  5 tags
Html 5 tags
 
Object Design - Part 1
Object Design - Part 1Object Design - Part 1
Object Design - Part 1
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Bridge pattern
Bridge patternBridge pattern
Bridge pattern
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
Introduction to SOFTWARE ARCHITECTURE
Introduction to SOFTWARE ARCHITECTUREIntroduction to SOFTWARE ARCHITECTURE
Introduction to SOFTWARE ARCHITECTURE
 
Software architecture
Software architectureSoftware architecture
Software architecture
 
Master pages
Master pagesMaster pages
Master pages
 
SOLID Design Principles
SOLID Design PrinciplesSOLID Design Principles
SOLID Design Principles
 
Learn react-js
Learn react-jsLearn react-js
Learn react-js
 
Data Designs (Software Engg.)
Data Designs (Software Engg.)Data Designs (Software Engg.)
Data Designs (Software Engg.)
 
Design Pattern
Design PatternDesign Pattern
Design Pattern
 
Overview of UML Diagrams
Overview of UML DiagramsOverview of UML Diagrams
Overview of UML Diagrams
 
Singleton design pattern
Singleton design patternSingleton design pattern
Singleton design pattern
 
Organizing Technical Teams
Organizing Technical TeamsOrganizing Technical Teams
Organizing Technical Teams
 

Similar to Gang of Four in Java

Bartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design PatternsBartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design Patterns
Jason Townsend, MBA
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
Simplilearn
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi
 

Similar to Gang of Four in Java (20)

Design patterns
Design patternsDesign patterns
Design patterns
 
Software design and Architecture.pptx
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptx
 
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
 
dotnet stuff.com tutorials-design-patterns_exploring-net-design-patterns-in-s...
dotnet stuff.com tutorials-design-patterns_exploring-net-design-patterns-in-s...dotnet stuff.com tutorials-design-patterns_exploring-net-design-patterns-in-s...
dotnet stuff.com tutorials-design-patterns_exploring-net-design-patterns-in-s...
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Bartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design PatternsBartlesville Dot Net User Group Design Patterns
Bartlesville Dot Net User Group Design Patterns
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
GOF Design pattern with java
GOF Design pattern with javaGOF Design pattern with java
GOF Design pattern with java
 
OOP design patterns
OOP design patternsOOP design patterns
OOP design patterns
 
Prophecy Of Design Patterns
Prophecy Of Design PatternsProphecy Of Design Patterns
Prophecy Of Design Patterns
 
Oops design pattern intro
Oops design pattern intro Oops design pattern intro
Oops design pattern intro
 
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
C# Design Patterns | Design Pattern Tutorial For Beginners | C# Programming T...
 
Basic design pattern interview questions
Basic design pattern interview questionsBasic design pattern interview questions
Basic design pattern interview questions
 
Javascript Design Patterns
Javascript Design PatternsJavascript Design Patterns
Javascript Design Patterns
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 

Recently uploaded

%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 

Recently uploaded (20)

WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 

Gang of Four in Java

  • 1. GoF Gang of Four (Design Patterns) in Java Mina Tafreshi Software Engineer @ Bol.com
  • 2. What is a design pattern? ● Design pattern is a general repeatable solution to common problems in software design. ● A design pattern is not a finished design. ● These patterns are mostly “evolved” rather than “discovered”. [Note]: Code implementation is your responsibility!
  • 3. Gang of Four Design Patterns ● Name? Element of Reusable Object-Oriented Software. ● By whom? Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides ● When? 1994
  • 4. GoF Design Patterns categories 1. Creational design patterns: It deal with object creation. They look at ways to solve design issues arising out of creation of objects. 2. Structural design patterns: Ease the design by identifying a simple way to realize relationships between entities. 3. Behavioral design patterns: Identify common communication patterns between objects and realize these patterns.
  • 5. Singleton Design Pattern Ensures only one instance of an object is created. Factory Design Pattern Creates objects without specifying the exact class to create. Abstract Factory Design Pattern Allows for the creation of objects without specifying their concrete type. Builder Design Pattern Separate the construction of a complex object from its representation so that the same construction process can create different representations. Prototype Design Pattern Creates a new object from an existing object.
  • 6. Adapter Design Pattern Convert the interface of a class into another interface the clients expect. Bridge Design Pattern Decouple an abstraction from its implementation. Composite Design Pattern Compose objects into tree structures to represent part-whole hierarchies. Decorator Design Pattern Attach additional responsibilities to an object dynamically. Facade Design Pattern Facade defines a higher-level interface that makes the subsystem easier to use. Flyweight Design Pattern Use sharing to support large numbers of fine-grained objects efficiently. Proxy Design Pattern Provide a surrogate or placeholder for another object to control access to it.
  • 7. Command Design Pattern Encapsulate a request as an object. Iterator Design Pattern Accesses the elements of an object sequentially without exposing its underlying representation Mediator Design Pattern Allows loose coupling between classes by being the only class that has detailed knowledge of their methods. Memento Design Pattern Provides the ability to restore an object to its previous state. Observer Design Pattern publish/subscribe pattern which allows a number of observer objects to see an event. State Design Pattern Allow an object to alter its behaviour when its internal state changes. Strategy Design Pattern Define a family of algorithms, encapsulate each one, and make them interchangeable. Template Method Design Pattern lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Delegation Pattern where an object expresses certain behavior to the outside but in reality delegates responsibility for implementing that behaviour to an associated object.
  • 8. Singleton Design Pattern: Ensures only one instance of an object is created When: A class in your program should have just a single instance available to all clients. Why: The most common reason for this is to control access to some shared resource. Examples in Java core libraries: ● java.lang.Runtime#getRuntime() ● java.awt.Desktop#getDesktop() ● java.lang.System#getSecurityManager()
  • 9. Factory Design Pattern: Creates objects without specifying the exact class to create When: You don’t know beforehand the exact types and dependencies of the objects your code should work with. Why: The most common reason for this is to control access to some shared resource. Examples in Java core libraries: ● java.util.Calendar#getInstance() ● java.util.ResourceBundle#getBundle() ● java.text.NumberFormat#getInstance() ● java.nio.charset.Charset#forName() ● java.net.URLStreamHandlerFactory#createURLStreamHandler(String) ● javax.xml.bind.JAXBContext#createMarshaller()
  • 10. Builder Design Pattern: Separate the construction of a complex object from its representation When: You want your code to be able to create different representations of some objects. Why: It lets you build objects step by step, using only those steps that you really need. Examples in Java core libraries: ● java.lang.StringBuilder#append() (unsynchronized) ● java.lang.StringBuffer#append() (synchronized) ● java.nio.ByteBuffer#put() ( in CharBuffer, ShortBuffer, IntBuffer, LongBuffer, FloatBuffer and DoubleBuffer) ● javax.swing.GroupLayout.Group#addComponent() ● All implementations java.lang.Appendable
  • 11. Prototype Design Pattern: Creates a new object from an existing object When: Your code shouldn’t depend on the concrete classes of objects that you need to copy Why: you want to reduce the number of subclasses that only differ in the way they initialize their respective objects Examples in Java core libraries: The Prototype pattern is available in Java out of the box with a Cloneable interface. Any class can implement this interface to become cloneable. ● java.lang.Object#clone() (class should implement the java.lang.Cloneable interface)
  • 12. Resources: 1. GoF (https://en.wikipedia.org/wiki/Design_Patterns) 2. https://refactoring.guru/design-patterns/ 3. Head First Design Patterns (https://www.oreilly.com/library/view/head-first-design/0596007124/)