The document outlines an introductory course on design patterns that covers creational, structural and behavioral patterns like strategy, observer, decorator, factory, singleton, command, adapter, facade, iterator, composite and state patterns as well as object-oriented design principles. The course is presented over 5 sessions and discusses how design patterns are used to solve common software design problems and how they allow code reuse.
5. What is so special about
design patterns?
At any given moment, somewhere in the world,
someone struggles with the same software design
problems you have. But better yet, someone has
already solved your software design problems.
Tried-and-true, road-tested, successfully used by
developers to create functional, elegant, reusable, and
flexible software.
6. Use inheritance
Duck
The Duck Game
MallardDuck
RedhedDuck
Quack()
Swim()
Display()
Someone wants fly()
Use Interfaces
Quackable
Swimmable
Flyable
Let’s Design it together Code reuse?
Waiting for a design
pattern to save the
day? No :P
Design Priciple 1
Identify the aspects of
your application that
vary and separate them
from what stays the
same.
9. Adding a spice of flexibility
Design Priciple 2
Program to an interface,
not an implementation
10. OO Design Principles
Indentify the aspects of your application that vary and
separate them from what stays the same.
Program to an interface, not an implementation.
Favor composition over inheritance
11. Strategy pattern
Defines a family of algorithms, encapsulates each one,
and make them interchangeable.
Strategy lets the algorithm vary independently from
clients that use it.
12. So?
How do I use design patterns?
If design patterns are so great, why can’t somebody
build a library of them so I don’t have to?
Aren’t libraries and frameworks also design patterns?
Knowing OO basics is enough? What makes me a
good OO designer?
14. Observer Pattern
The most heavily used pattern in the JDK
Publisher + subscriber = Observer Pattern
Defines a one-to-many relationship.
State monitor.
Definition: defines one-to-many dependency between
objects so that when one object changes state, all of
its dependencies are notified and updated
automatically.
16. DP Categories
Creational
Concern the ways and means of object instantiation
Structural
Deal with the mutual composition of classes or
objects.
Behavioral
Analyze the ways in which classes or objects interact
and distribute responsibilities among them.
18. OO Design Principles
Classes should be open for for extension, but closed
for modification. (Open-Closed Principle)
19. Decorator Pattern
Attach additional responsibilities to an object
dynamically.
Decorators provide a flexible alternative to subclassing
for extending functionality.
21. Factory Method Pattern
Defines an interface for creating an object, but lets
subclasses decide which class to instantiate. Factory
Method lets a class defer instantiation to subclasses.
23. Singleton Pattern
One of a kind objects.
Static initializers?
Private constructors? Useful?
Ensures a class has only one instance, and provide a
global point of access to it.
24. Command Pattern
Encapsulates a request as an object, thereby letting
you parameterize other objects with different requests,
queue or log requests, and support undoable
operations.
25. Adapter and Facade
Patterns
Adapter pattern: converts the interface of a class into
another interface the clients expect. Adapters lets
classes work together that couldn’t otherwise because
of incompatible interfaces.
Facade pattern: provides a unified interface to a set of
interfaces in a subsystem. Facade defines a higher-
level interface that makes the subsystem easier to use.
26. OO Design Principle
Least Knowledge: talk only to your immediate friends.
How java violates it?
How facade use it?
28. Iterator Pattern
Provides a way to access the elements of an aggregate
object (collection) sequentially without exposing its
underlying representation.
29. OO Design Principle
Single Responsibility: A class should have only one
reason to change.
Cohesion: a term measures how closely a class or a
module supports a single purpose or responsibility.
High Cohesion -> if a class is designed around a set of
related functions.
30. Composite Pattern
Allows you to compose objects into tree structures to
represent part-whole hierarchies, Composite lets clients
treat individual objects and compositions of objects
uniformly.
32. State pattern
A little known fact: the strategy and state patterns were
twins separated at birth.
Allows an object to alter its behavior when its internal
state changes. The object will appear to change its
class.
33. Proxy Pattern
Remote Proxy.
Virtual proxy.
Protection proxy.
Definition: Provides a surrogate or placeholder for
another object to control access to it.
34. Compound Patterns
Patterns are often used together and combined within
the same design solution.
A compound pattern combines two or more patterns
into a solution that solves a recurring or general
problem.