Selected Design Patterns
Prof. Dr. Ralf Lämmel
Universität Koblenz-Landau
Software Languages Team
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Reusable solutions to
recurrent problems
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Selected patterns
(Pattern names with intents)
Composite -- provide uniform interface on part-whole hierarchies
Command -- encapsulate the execution of functionality; enable undo
Visitor -- represent operations on object structure as objects
Observer -- provide change notifications to objects depending on state
MVC -- decouple model, view, control in for interactive applications
Proxy -- refine or replace behavior of a given object
Object Adapter -- provide a different interface for an existing object
Template Method -- capture general structure of an algorithm
...
Intent = short combo
of problem + solution
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Observer -- Problem
The observer pattern (aka publish/subscribe) involves
an object, called the subject, which maintains a list of
its dependents, called observers, and notifies them
automatically of any state changes.
Do you know the problem?
Do you know a solution?
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Observer -- Solution
http://en.wikipedia.org/wiki/File:Observer.svg
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Essential elements of a
design pattern
Name
The problem describes when to apply the pattern.
The solution describes elements of the design, their
relationships, responsibilities, and collaborations.
The consequences are the results and trade-offs of
applying the pattern.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Describing design patterns
Pattern Name
Classification
Intent
Also Known As
Motivation
Applicability
Structure
Participants
Collaborations
Consequences
Implementation
Sample Code
Known Uses
Related Patterns
We do
not go into all
these details in this
course.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Classification of
design patterns
Aspects
Creational patterns
Structural patterns
Behavioral patterns
Scope
Class
Object
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Design patterns -- Overview
[Gamma et al., 1995, S. 10]
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Design patterns
of this slide deck
Template Method
Abstract Factory
Object Adapter
Observer
Proxy
You are definitely
expected to expand on
your knowledge of
design patterns.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Abstract Factory -- Problem
Object models often give rise to variation. For instance,
there may be multiple GUI libraries subject to different
widget hierarchies. Whenever components want to
abstract from the specific choice, then substantial efforts
are required. For instance, the construction of objects
must be tunneled through a factory.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Abstract Factory -- Solution
http://sourcemaking.com/design_patterns/abstract_factory
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Abstract Factory
DEMO
Two alternative object models for companies.
POJOs
Some sort of beans
Make the choice of the object model configurable.
Contribution:javaExorcism
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Proxy -- Problem
Additional access behavior for objects
Access management for remote objects
Access management for expensive objects
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Proxy -- Solution
http://en.wikipedia.org/wiki/File:Proxy_pattern_diagram.svg
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Proxy -- Scenarios
Additional access behavior for objects
Access management for remote objects
Access management for expensive objects
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Proxy
DEMO
Use security proxy to protect salary access.
Interesting issue of proxy deployment.
Contribution:javaExorcism
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Observer -- Problem
The observer pattern (aka publish/subscribe) involves
an object, called the subject, which maintains a list of
its dependents, called observers, and notifies them
automatically of any state changes.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Observer -- Solution
http://en.wikipedia.org/wiki/File:Observer.svg
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Observer
DEMO
Use observer to providing logging.
Use observer to enforce data constraint.
Contribution:javaExorcism
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Object Adapter -- Problem
A given object often has about the right capabilities for a
specific client but not necessarily the precise interface
expected. Accordingly, an interface adaptation can be
achieved by an extra wrapper around the given object.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Adapter -- Solution
(object adapters)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Adapter -- Solution
(class adapters)
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Adapter
DEMO
Class adapter: add observability of list interface.
Object adapter: downgrade java.util.List to simple list.
Contribution:javaExorcism
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Template Method -- Problem
Some algorithms or strategies in a system may be very
similar and hence it may be desirable to capture their
commonalities in a sort of template so that specifics can
be expressed through refinement giving rise to a high
degree of reuse.
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Template Method -- Solution
http://stephane.ducasse.free.fr/FreeBooks/SmalltalkDesignPatternCompanion/templateMethod.pdf
Intent
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Temp
Method lets subclasses redefine certain steps of an algorithm without changing the algorith
structure.
Structure
Description
Template Method may be #1 on the Design Patterns Pop Charts. Its structure and use are at the h
of much of object-oriented programming. Template Method turns on the fundamental object-orie
templateMethod
AbstractClass
ConcreteClassA
primitiveMethod2
primitiveMethod1
primitiveMethod1
primitiveMethod2
...
self primitiveMethod1.
...
self primitiveMethod2.
...
ConcreteClassB
primitiveMethod1
primitiveMethod2
ConcreteClassC
primitiveMethod2
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Template
DEMO
Two general class of algorithms can identified:
Queries such as 101feature:Total
Transformations such as 101feature:Cut
Contribution:javaTemplate
Contribution:javaExorcism
(C) 2010-2013 Prof. Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable)
Summary
The use of design patterns is a basic element of software
development. If you wouldn’t use them, your designs
and implementations are unnecessarily unstandardized,
idiosyncratic, incomprehensible, unmaintainable, etc.
That is, if you are interested in a software development
career, or you plan to talk to software developers at an
interesting level of detail, you have to aggregate
substantial knowledge of design patterns.

Selected design patterns (as part of the the PTT lecture)

  • 1.
    Selected Design Patterns Prof.Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team
  • 2.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Reusable solutions to recurrent problems
  • 3.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Selected patterns (Pattern names with intents) Composite -- provide uniform interface on part-whole hierarchies Command -- encapsulate the execution of functionality; enable undo Visitor -- represent operations on object structure as objects Observer -- provide change notifications to objects depending on state MVC -- decouple model, view, control in for interactive applications Proxy -- refine or replace behavior of a given object Object Adapter -- provide a different interface for an existing object Template Method -- capture general structure of an algorithm ... Intent = short combo of problem + solution
  • 4.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Observer -- Problem The observer pattern (aka publish/subscribe) involves an object, called the subject, which maintains a list of its dependents, called observers, and notifies them automatically of any state changes. Do you know the problem? Do you know a solution?
  • 5.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Observer -- Solution http://en.wikipedia.org/wiki/File:Observer.svg
  • 6.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Essential elements of a design pattern Name The problem describes when to apply the pattern. The solution describes elements of the design, their relationships, responsibilities, and collaborations. The consequences are the results and trade-offs of applying the pattern.
  • 7.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Describing design patterns Pattern Name Classification Intent Also Known As Motivation Applicability Structure Participants Collaborations Consequences Implementation Sample Code Known Uses Related Patterns We do not go into all these details in this course.
  • 8.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Classification of design patterns Aspects Creational patterns Structural patterns Behavioral patterns Scope Class Object
  • 9.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Design patterns -- Overview [Gamma et al., 1995, S. 10]
  • 10.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Design patterns of this slide deck Template Method Abstract Factory Object Adapter Observer Proxy You are definitely expected to expand on your knowledge of design patterns.
  • 11.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Abstract Factory -- Problem Object models often give rise to variation. For instance, there may be multiple GUI libraries subject to different widget hierarchies. Whenever components want to abstract from the specific choice, then substantial efforts are required. For instance, the construction of objects must be tunneled through a factory.
  • 12.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Abstract Factory -- Solution http://sourcemaking.com/design_patterns/abstract_factory
  • 13.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Abstract Factory DEMO Two alternative object models for companies. POJOs Some sort of beans Make the choice of the object model configurable. Contribution:javaExorcism
  • 14.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Proxy -- Problem Additional access behavior for objects Access management for remote objects Access management for expensive objects
  • 15.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Proxy -- Solution http://en.wikipedia.org/wiki/File:Proxy_pattern_diagram.svg
  • 16.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Proxy -- Scenarios Additional access behavior for objects Access management for remote objects Access management for expensive objects
  • 17.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Proxy DEMO Use security proxy to protect salary access. Interesting issue of proxy deployment. Contribution:javaExorcism
  • 18.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Observer -- Problem The observer pattern (aka publish/subscribe) involves an object, called the subject, which maintains a list of its dependents, called observers, and notifies them automatically of any state changes.
  • 19.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Observer -- Solution http://en.wikipedia.org/wiki/File:Observer.svg
  • 20.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Observer DEMO Use observer to providing logging. Use observer to enforce data constraint. Contribution:javaExorcism
  • 21.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Object Adapter -- Problem A given object often has about the right capabilities for a specific client but not necessarily the precise interface expected. Accordingly, an interface adaptation can be achieved by an extra wrapper around the given object.
  • 22.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Adapter -- Solution (object adapters)
  • 23.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Adapter -- Solution (class adapters)
  • 24.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Adapter DEMO Class adapter: add observability of list interface. Object adapter: downgrade java.util.List to simple list. Contribution:javaExorcism
  • 25.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Template Method -- Problem Some algorithms or strategies in a system may be very similar and hence it may be desirable to capture their commonalities in a sort of template so that specifics can be expressed through refinement giving rise to a high degree of reuse.
  • 26.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Template Method -- Solution http://stephane.ducasse.free.fr/FreeBooks/SmalltalkDesignPatternCompanion/templateMethod.pdf Intent Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Temp Method lets subclasses redefine certain steps of an algorithm without changing the algorith structure. Structure Description Template Method may be #1 on the Design Patterns Pop Charts. Its structure and use are at the h of much of object-oriented programming. Template Method turns on the fundamental object-orie templateMethod AbstractClass ConcreteClassA primitiveMethod2 primitiveMethod1 primitiveMethod1 primitiveMethod2 ... self primitiveMethod1. ... self primitiveMethod2. ... ConcreteClassB primitiveMethod1 primitiveMethod2 ConcreteClassC primitiveMethod2
  • 27.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Template DEMO Two general class of algorithms can identified: Queries such as 101feature:Total Transformations such as 101feature:Cut Contribution:javaTemplate Contribution:javaExorcism
  • 28.
    (C) 2010-2013 Prof.Dr. Ralf Lämmel, Universität Koblenz-Landau (where applicable) Summary The use of design patterns is a basic element of software development. If you wouldn’t use them, your designs and implementations are unnecessarily unstandardized, idiosyncratic, incomprehensible, unmaintainable, etc. That is, if you are interested in a software development career, or you plan to talk to software developers at an interesting level of detail, you have to aggregate substantial knowledge of design patterns.