Chapter 02 of the lecture Style & Design Principles taught at SAE Institute Hamburg.
Introduction to advanced concepts of object-oriented design, such as delegation, polymorphism, cohesion and coupling, and to behavioral, creational and structural design patterns.
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
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
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
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
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());
}
}
}
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