SlideShare une entreprise Scribd logo
1  sur  43
Télécharger pour lire hors ligne
Style & Design Principles
Chapter 02: Design Patterns
Nick Prühs
5 Minute Review Session
• Name a few characteristics of good code!
• How can you achieve good code?
• Tabs or spaces?
• When should you use a struct instead of a class?
• When should you use a method instead of a property?
• Name the three common interfaces and base classes that
can be used for collections in .NET!
• What is the main purpose of the interface IEquatable?
• What is the main purpose of the interface IComparable?
• How are Equals and GetHashCode related to each other?
2 / 58
Assignment Solution #1
DEMO
3 / 58
Objectives
• To learn advances concepts of object-oriented
design
• To understand the motivation behind design
patterns
• To get an idea of the different types of design
patterns and their application
4 / 78
Design Patterns
• General reusable solution to a commonly occurring
problem within a given context
• Formalized best practices that the programmer
must implement themselves in the application
• Not a finished design that can be transformed directly
into source code
• Gained popularity in computer science after the
book Design Patterns: Elements of Reusable Object-
Oriented Software was published in 1994 by the so-
called "Gang of Four" (Gamma et al.)
5 / 78
Advantages of
Design Patterns
• Speed up the development process by providing
tested, proven development paradigms
• Improve code readability for coders and architects
who are familiar with the patterns
6 / 78
Design Pattern Types
• Creational (object creation)
• Structural (relationships between objects)
• Behavioral (communication between objects)
7 / 78
Object-Oriented Design 101
• Aggregation
• Combine simple objects or data types into more
complex ones
• Usually expressed by means of references from one
object to another
• Inheritance
• Adding detail to a general data type to create a more
specific data type
8 / 78
Object-Oriented Design 101
• Delegation
• Handing a task over to another part of the program
• Polymorphism
• Ad hoc polymorphism (function overloading)
• Parametric polymorphism (generic programming)
• Subtyping (subclassing)
9 / 78
Object-Oriented Design 101
• Cohesion
• Degree to which the elements of a module belong
together
• How much functionalities embedded in a class have in
common
• Coupling
• Degree to which each program module relies on the
other modules
10 / 78
Object-Oriented Design 101
• Cohesion
• Degree to which the elements of a module belong
together
• How much functionalities embedded in a class have in
common
• Coupling
• Degree to which each program module relies on the
other modules
11 / 78
Why getters and setters
are evil
“Don’t ask for the information you need to do the
work; ask the object that has the information to do
the work for you.”
- Allen Holub
12 / 78
Why getters and setters
are evil
• Getter and setter methods are dangerous for the
same reason that public fields are dangerous
• They’re okay if
• They return interface references
• You don’t know in advance how your class will be used
13 / 78
Behavioral Design Patterns
Communication Between Objects:
• Iterator
• Observer
• Command
• Memento
• Strategy
14 / 78
Iterator Pattern
Provides a way to access the elements of an
aggregate object sequentially without exposing
its underlying representation.
15 / 78
Examples:
• Contains
• Where
• Count
Iterator Pattern
Provides a way to access the elements of an
aggregate object sequentially without exposing
its underlying representation.
16 / 78
Observer Pattern
Subject maintains a list of its dependents,
called observers, and notifies them
automatically of any state changes, usually by
calling one of their methods.
17 / 78
Examples:
• Event Handling
Observer Pattern
Subject maintains a list of its dependents,
called observers, and notifies them
automatically of any state changes, usually by
calling one of their methods.
18 / 78
Command Pattern
Encapsulates all the information needed to
call a method at a later time in an object.
19 / 78
Examples:
• Networking
• Replays
• Undo
Command Pattern
Encapsulates all the information needed to
call a method at a later time in an object.
20 / 78
Command Pattern
Encapsulates all the information needed to
call a method at a later time in an object.
21 / 78
Memento Pattern
Provides the ability to restore an object to its
previous state.
22 / 78
Examples:
• Undo
Memento Pattern
Provides the ability to restore an object to its
previous state.
23 / 78
Strategy Pattern
Defines a family of algorithms, encapsulates
each one, and makes them interchangeable.
24 / 78
Examples:
• Calculator
• Sorting
• AI
Strategy Pattern
Defines a family of algorithms, encapsulates
each one, and makes them interchangeable.
25 / 78
Creational Design Patterns
Object Creation:
• Prototype
• Factory
• Object Pool
• Singleton
26 / 78
Prototype Pattern
Objects are created using a prototypical instance,
which is cloned to produce new objects.
27 / 78
C#
public Map(Map map)
{
// Set width and height.
this.Width = map.Width;
this.Height = map.Height;
// Deep copy map tiles.
this.Tiles = new MapTile[this.Width,this.Height];
for (var x = 0; x < this.Width; x++)
{
for (var y = 0; y < this.Height; y++)
{
this.Tiles[x, y] = new MapTile(map[x, y]);
}
}
}
Factory Method Pattern
Defines an interface for creating an object, but
let the classes that implement the interface
decide which class to instantiate.
28 / 78
Examples:
• Frameworks
Factory Method Pattern
Defines an interface for creating an object, but
let the classes that implement the interface
decide which class to instantiate.
29 / 78
Factory Method Pattern
Defines an interface for creating an object, but
let the classes that implement the interface
decide which class to instantiate.
30 / 78
Object Pool Pattern
Uses a set of initialized objects kept ready to
use, rather than allocating and destroying
them on demand.
31 / 78
Examples:
• Unity3D Game Objects
• Database Connections
• Threads
Object Pool Pattern
Uses a set of initialized objects kept ready to
use, rather than allocating and destroying
them on demand.
32 / 78
Singleton (Anti-)Pattern
Restricts the instantiation of a class to one object.
Disadvantages:
• Introduces unnecessary restrictions in situations
where a sole instance of a class is not actually
required
• Introduces global state into an application
• Needs to be thread-safe!
33 / 78
Singleton (Anti-)Pattern
Restricts the instantiation of a class to one object.
34 / 78
C#
public class Singleton
{
private static Singleton instance;
private Singleton() { }
public static Singleton Instance
{
get
{
return instance ?? (instance = new Singleton());
}
}
}
Structural Design Patterns
Relationships Between Objects:
• Composite
• Decorator
35 / 78
Composite Pattern
Treats a group of objects in the same way as a
single instance of an object.
36 / 78
Examples:
• Files and Directories
Composite Pattern
Treats a group of objects in the same way as a
single instance of an object.
37 / 78
Decorator Pattern
Allows behavior to be added to an individual
object, either statically or dynamically,
without affecting the behavior of other
objects from the same class.
38 / 78
Examples:
• Streams
Decorator Pattern
Allows behavior to be added to an individual
object, either statically or dynamically,
without affecting the behavior of other
objects from the same class.
39 / 78
Assignment #2
1. Event Manager
Implement an event manager based on the Observer
pattern!
1. Provide a method for adding a new listener.
2. Provide a method for removing a listener.
3. Provide a method for queuing a new event.
4. Provide a method for passing all events to the
listeners.
40 / 78
Assignment #2
2. Object Pool
Implement an object pool based on the Object Pool
pattern!
1. Define an IPoolable interface for resetting pooled
objects.
2. Create an ObjectPool class with Alloc and Free
methods for allocating and returning pooled objects.
3. Decide what to do if no object can be allocated!
41 / 78
References
• Wikipedia. Software design pattern.
http://en.wikipedia.org/wiki/Software_design_patt
ern, October 29, 2013.
• Holub, Allen. Why getter and setter methods are
evil.
http://www.javaworld.com/article/2073723/core-
java/why-getter-and-setter-methods-are-evil.html,
September 5, 2003.
42 / 78
Thank you for your attention!
Contact
Mail
dev@npruehs.de
Blog
http://www.npruehs.de
Twitter
@npruehs
Github
https://github.com/npruehs
43 / 78

Contenu connexe

Similaire à Style & Design Principles 02 - Design Patterns

Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptxSachin Patidar
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2Julie Iskander
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
How to design an application correctly ?
How to design an application correctly ?How to design an application correctly ?
How to design an application correctly ?Guillaume AGIS
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)stanbridge
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane2
 
P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their applicationHiệp Tiến
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patternsamitarcade
 
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.docxdanhaley45372
 
Nodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternNodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternTalentica Software
 

Similaire à Style & Design Principles 02 - Design Patterns (20)

Design_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.pptDesign_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.ppt
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
 
Design Patterns - GOF
Design Patterns - GOFDesign Patterns - GOF
Design Patterns - GOF
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
How to design an application correctly ?
How to design an application correctly ?How to design an application correctly ?
How to design an application correctly ?
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Design pattern and their application
Design pattern and their applicationDesign pattern and their application
Design pattern and their application
 
Introduction to design_patterns
Introduction to design_patternsIntroduction to design_patterns
Introduction to design_patterns
 
Design Patterns
Design PatternsDesign Patterns
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
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Nodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design PatternNodejs Chapter 3 - Design Pattern
Nodejs Chapter 3 - Design Pattern
 

Plus de Nick Pruehs

Unreal Engine Basics 06 - Animation, Audio, Visual Effects
Unreal Engine Basics 06 - Animation, Audio, Visual EffectsUnreal Engine Basics 06 - Animation, Audio, Visual Effects
Unreal Engine Basics 06 - Animation, Audio, Visual EffectsNick Pruehs
 
Unreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceUnreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceNick Pruehs
 
Unreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesUnreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesNick Pruehs
 
Unreal Engine Basics 03 - Gameplay
Unreal Engine Basics 03 - GameplayUnreal Engine Basics 03 - Gameplay
Unreal Engine Basics 03 - GameplayNick Pruehs
 
Unreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal EditorUnreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal EditorNick Pruehs
 
Unreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkUnreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkNick Pruehs
 
Game Programming - Cloud Development
Game Programming - Cloud DevelopmentGame Programming - Cloud Development
Game Programming - Cloud DevelopmentNick Pruehs
 
Game Programming - Git
Game Programming - GitGame Programming - Git
Game Programming - GitNick Pruehs
 
Eight Rules for Making Your First Great Game
Eight Rules for Making Your First Great GameEight Rules for Making Your First Great Game
Eight Rules for Making Your First Great GameNick Pruehs
 
Designing an actor model game architecture with Pony
Designing an actor model game architecture with PonyDesigning an actor model game architecture with Pony
Designing an actor model game architecture with PonyNick Pruehs
 
Game Programming 13 - Debugging & Performance Optimization
Game Programming 13 - Debugging & Performance OptimizationGame Programming 13 - Debugging & Performance Optimization
Game Programming 13 - Debugging & Performance OptimizationNick Pruehs
 
Scrum - but... Agile Game Development in Small Teams
Scrum - but... Agile Game Development in Small TeamsScrum - but... Agile Game Development in Small Teams
Scrum - but... Agile Game Development in Small TeamsNick Pruehs
 
Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)Nick Pruehs
 
What Would Blizzard Do
What Would Blizzard DoWhat Would Blizzard Do
What Would Blizzard DoNick Pruehs
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsNick Pruehs
 
Tool Development A - Git
Tool Development A - GitTool Development A - Git
Tool Development A - GitNick Pruehs
 
Game Programming 12 - Shaders
Game Programming 12 - ShadersGame Programming 12 - Shaders
Game Programming 12 - ShadersNick Pruehs
 
Game Programming 11 - Game Physics
Game Programming 11 - Game PhysicsGame Programming 11 - Game Physics
Game Programming 11 - Game PhysicsNick Pruehs
 
Game Programming 10 - Localization
Game Programming 10 - LocalizationGame Programming 10 - Localization
Game Programming 10 - LocalizationNick Pruehs
 
Game Programming 09 - AI
Game Programming 09 - AIGame Programming 09 - AI
Game Programming 09 - AINick Pruehs
 

Plus de Nick Pruehs (20)

Unreal Engine Basics 06 - Animation, Audio, Visual Effects
Unreal Engine Basics 06 - Animation, Audio, Visual EffectsUnreal Engine Basics 06 - Animation, Audio, Visual Effects
Unreal Engine Basics 06 - Animation, Audio, Visual Effects
 
Unreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceUnreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User Interface
 
Unreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesUnreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior Trees
 
Unreal Engine Basics 03 - Gameplay
Unreal Engine Basics 03 - GameplayUnreal Engine Basics 03 - Gameplay
Unreal Engine Basics 03 - Gameplay
 
Unreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal EditorUnreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal Editor
 
Unreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkUnreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game Framework
 
Game Programming - Cloud Development
Game Programming - Cloud DevelopmentGame Programming - Cloud Development
Game Programming - Cloud Development
 
Game Programming - Git
Game Programming - GitGame Programming - Git
Game Programming - Git
 
Eight Rules for Making Your First Great Game
Eight Rules for Making Your First Great GameEight Rules for Making Your First Great Game
Eight Rules for Making Your First Great Game
 
Designing an actor model game architecture with Pony
Designing an actor model game architecture with PonyDesigning an actor model game architecture with Pony
Designing an actor model game architecture with Pony
 
Game Programming 13 - Debugging & Performance Optimization
Game Programming 13 - Debugging & Performance OptimizationGame Programming 13 - Debugging & Performance Optimization
Game Programming 13 - Debugging & Performance Optimization
 
Scrum - but... Agile Game Development in Small Teams
Scrum - but... Agile Game Development in Small TeamsScrum - but... Agile Game Development in Small Teams
Scrum - but... Agile Game Development in Small Teams
 
Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)
 
What Would Blizzard Do
What Would Blizzard DoWhat Would Blizzard Do
What Would Blizzard Do
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine Basics
 
Tool Development A - Git
Tool Development A - GitTool Development A - Git
Tool Development A - Git
 
Game Programming 12 - Shaders
Game Programming 12 - ShadersGame Programming 12 - Shaders
Game Programming 12 - Shaders
 
Game Programming 11 - Game Physics
Game Programming 11 - Game PhysicsGame Programming 11 - Game Physics
Game Programming 11 - Game Physics
 
Game Programming 10 - Localization
Game Programming 10 - LocalizationGame Programming 10 - Localization
Game Programming 10 - Localization
 
Game Programming 09 - AI
Game Programming 09 - AIGame Programming 09 - AI
Game Programming 09 - AI
 

Dernier

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Dernier (20)

Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Style & Design Principles 02 - Design Patterns

  • 1. Style & Design Principles Chapter 02: Design Patterns Nick Prühs
  • 2. 5 Minute Review Session • Name a few characteristics of good code! • How can you achieve good code? • Tabs or spaces? • When should you use a struct instead of a class? • When should you use a method instead of a property? • Name the three common interfaces and base classes that can be used for collections in .NET! • What is the main purpose of the interface IEquatable? • What is the main purpose of the interface IComparable? • How are Equals and GetHashCode related to each other? 2 / 58
  • 4. Objectives • To learn advances concepts of object-oriented design • To understand the motivation behind design patterns • To get an idea of the different types of design patterns and their application 4 / 78
  • 5. Design Patterns • General reusable solution to a commonly occurring problem within a given context • Formalized best practices that the programmer must implement themselves in the application • Not a finished design that can be transformed directly into source code • Gained popularity in computer science after the book Design Patterns: Elements of Reusable Object- Oriented Software was published in 1994 by the so- called "Gang of Four" (Gamma et al.) 5 / 78
  • 6. Advantages of Design Patterns • Speed up the development process by providing tested, proven development paradigms • Improve code readability for coders and architects who are familiar with the patterns 6 / 78
  • 7. Design Pattern Types • Creational (object creation) • Structural (relationships between objects) • Behavioral (communication between objects) 7 / 78
  • 8. Object-Oriented Design 101 • Aggregation • Combine simple objects or data types into more complex ones • Usually expressed by means of references from one object to another • Inheritance • Adding detail to a general data type to create a more specific data type 8 / 78
  • 9. Object-Oriented Design 101 • Delegation • Handing a task over to another part of the program • Polymorphism • Ad hoc polymorphism (function overloading) • Parametric polymorphism (generic programming) • Subtyping (subclassing) 9 / 78
  • 10. Object-Oriented Design 101 • Cohesion • Degree to which the elements of a module belong together • How much functionalities embedded in a class have in common • Coupling • Degree to which each program module relies on the other modules 10 / 78
  • 11. Object-Oriented Design 101 • Cohesion • Degree to which the elements of a module belong together • How much functionalities embedded in a class have in common • Coupling • Degree to which each program module relies on the other modules 11 / 78
  • 12. Why getters and setters are evil “Don’t ask for the information you need to do the work; ask the object that has the information to do the work for you.” - Allen Holub 12 / 78
  • 13. Why getters and setters are evil • Getter and setter methods are dangerous for the same reason that public fields are dangerous • They’re okay if • They return interface references • You don’t know in advance how your class will be used 13 / 78
  • 14. Behavioral Design Patterns Communication Between Objects: • Iterator • Observer • Command • Memento • Strategy 14 / 78
  • 15. Iterator Pattern Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 15 / 78 Examples: • Contains • Where • Count
  • 16. Iterator Pattern Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 16 / 78
  • 17. Observer Pattern Subject maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. 17 / 78 Examples: • Event Handling
  • 18. Observer Pattern Subject maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. 18 / 78
  • 19. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. 19 / 78 Examples: • Networking • Replays • Undo
  • 20. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. 20 / 78
  • 21. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. 21 / 78
  • 22. Memento Pattern Provides the ability to restore an object to its previous state. 22 / 78 Examples: • Undo
  • 23. Memento Pattern Provides the ability to restore an object to its previous state. 23 / 78
  • 24. Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. 24 / 78 Examples: • Calculator • Sorting • AI
  • 25. Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. 25 / 78
  • 26. Creational Design Patterns Object Creation: • Prototype • Factory • Object Pool • Singleton 26 / 78
  • 27. Prototype Pattern Objects are created using a prototypical instance, which is cloned to produce new objects. 27 / 78 C# public Map(Map map) { // Set width and height. this.Width = map.Width; this.Height = map.Height; // Deep copy map tiles. this.Tiles = new MapTile[this.Width,this.Height]; for (var x = 0; x < this.Width; x++) { for (var y = 0; y < this.Height; y++) { this.Tiles[x, y] = new MapTile(map[x, y]); } } }
  • 28. Factory Method Pattern Defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. 28 / 78 Examples: • Frameworks
  • 29. Factory Method Pattern Defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. 29 / 78
  • 30. Factory Method Pattern Defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. 30 / 78
  • 31. Object Pool Pattern Uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand. 31 / 78 Examples: • Unity3D Game Objects • Database Connections • Threads
  • 32. Object Pool Pattern Uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand. 32 / 78
  • 33. Singleton (Anti-)Pattern Restricts the instantiation of a class to one object. Disadvantages: • Introduces unnecessary restrictions in situations where a sole instance of a class is not actually required • Introduces global state into an application • Needs to be thread-safe! 33 / 78
  • 34. Singleton (Anti-)Pattern Restricts the instantiation of a class to one object. 34 / 78 C# public class Singleton { private static Singleton instance; private Singleton() { } public static Singleton Instance { get { return instance ?? (instance = new Singleton()); } } }
  • 35. Structural Design Patterns Relationships Between Objects: • Composite • Decorator 35 / 78
  • 36. Composite Pattern Treats a group of objects in the same way as a single instance of an object. 36 / 78 Examples: • Files and Directories
  • 37. Composite Pattern Treats a group of objects in the same way as a single instance of an object. 37 / 78
  • 38. Decorator Pattern Allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. 38 / 78 Examples: • Streams
  • 39. Decorator Pattern Allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. 39 / 78
  • 40. Assignment #2 1. Event Manager Implement an event manager based on the Observer pattern! 1. Provide a method for adding a new listener. 2. Provide a method for removing a listener. 3. Provide a method for queuing a new event. 4. Provide a method for passing all events to the listeners. 40 / 78
  • 41. Assignment #2 2. Object Pool Implement an object pool based on the Object Pool pattern! 1. Define an IPoolable interface for resetting pooled objects. 2. Create an ObjectPool class with Alloc and Free methods for allocating and returning pooled objects. 3. Decide what to do if no object can be allocated! 41 / 78
  • 42. References • Wikipedia. Software design pattern. http://en.wikipedia.org/wiki/Software_design_patt ern, October 29, 2013. • Holub, Allen. Why getter and setter methods are evil. http://www.javaworld.com/article/2073723/core- java/why-getter-and-setter-methods-are-evil.html, September 5, 2003. 42 / 78
  • 43. Thank you for your attention! Contact Mail dev@npruehs.de Blog http://www.npruehs.de Twitter @npruehs Github https://github.com/npruehs 43 / 78