Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Software Engineering II

Introduction to OOD &


Design Patterns

Dr. Abeer Hamdy


Lecture Objectives:

▪ From OO analysis to OO design


▪ Introduce the concept of design patterns.
▪ Introduce the following patterns :
▪ Singleton
▪ Immutable
▪ Delegator
Where Are We?

✓Requirements
Eelicitation (Ch.4)

✓Functional Model
Nonfunctional ✓(Use Cases)
Requirements

➢Analysis (Ch.5)

✓Analysis Object Model

✓Dynamic Model
System design (later)
(Ch. 6 & 7)

Class design
(Ch. 8 & Ch 9)

✓Dynamic Model Design Patterns


✓(Sequence diagrams)

Object Design Model


(Design class diagram)

Mapping Models
Source Code (Ch. 10)

Testing (Ch. 11)

Ddeliverable
System
The Design Process

▪ Design is the creative process of figuring out how to


implement all of the customer’s requirements.
▪ Early design decisions address the system’s architecture.
▪ Later design decisions address how to implement the
individual units.
▪ We can improve our design by studying examples of good
designs.
Class Design Activities

1. Service (Interface) Specification


▪ Identify missing attributes.
▪ Assign operations to appropriate classes
—Apply GRASP
▪ Add visibility information.
▪ Add signature information.
—Additional objects could be identified
Class Design Activities

▪ GRASP (General Responsibility Assignment Software


Patterns)
▪ Information Expert
—Assign the responsibilities to the class that includes the required
information.
▪ Low coupling
—Assign the responsibilities in a way that lead to low coupling
between the classes.
▪ High Cohesion
—Avoid having God classes , reduce the number of responsibilities
of each class, try to have cohesive classes.
Class Design Activities

2. Reuse
▪ Use Class libraries
▪ Use Design patterns.
▪ Choose algorithms that minimize the cost of implementing the operations
And select data structures appropriate to the algorithms.
▪ Apply Design principles.
Class Design Activities
3. Refactoring and Restructuring
▪ Includes:
—Merging two similar classes from two different subsystems into a single
class.
—Collapsing classes with no significant behavior into attributes.
— Splitting complex classes into simpler ones.
—Rearranging classes and operations for packaging.

4. Optimization
▪ Includes:
—Address performance criteria such as response time and memory
utilization.
—Changing algorithms to respond to speed or memory requirements.
—Adding redundant associations for efficiency.
—Adding derived attributes to improve the access time to objects.
—Reduce the dynamic binding.
Introduction to Patterns

▪ A pattern is the outline of a reusable solution to a recurring


problem encountered in a particular context.

▪ Many of them have been systematically documented for all


software developers to use.

▪ A good pattern should


▪Contain a solution that has been proven to effectively solve
the problem in the indicated context.

▪ Studying patterns is an effective way to learn from the


experience of others
Software Patterns

▪ Popular Software Patterns

▪ Architectural Patterns
▪ Design Patterns
What’s Design Patterns

▪ Design pattern is a solution to a recurring design problem


within a particular context

Design Pattern = Design Problem & solution pair in a context

Design Patterns descriptions are often independent of


programming language and implementation details.
Pattern Description

▪ Basic elements to describe any pattern:


▪ Name: A meaningful name of the pattern
▪ Author
▪ Context: a design situation giving rise to a design problem.
▪ Problem: a statement of the problem and the set of forces
occurring in that context.
▪ Solution : The rules that could be applied to resolve these
forces and the UML diagrams that illustrate the solution.
▪ Consequences: The cost and the benefits of applying the
patterns.
▪ Anti-patterns
GOF design patterns

▪ Gang of Four (GOF) defined 23 design patterns and


classified them into three categories:
▪ Creational Patterns
—Concerned with creation of objects
▪ Structural Patterns
—Concerned with composition of classes or objects into
larger structures
▪ Behavioral Patterns
—Determines the ways in which the classes interact and
distribute responsibilities.

▪ GOF are the most popular design patterns , however there are other
widely used patterns.
Creational Patterns

▪ Singleton
▪Ensures that one instance of a class will be created.

▪ Immutable Pattern (Not GOF)


▪Ensures that the state of the object never changes after
creation.
The Singleton Pattern

▪ Context:
▪ It is very common to find classes for which only one instance should
exist (singleton) .
▪ Examples: university president class , Main Window class in GUI.
▪ Problem:
▪ How do you ensure that it is never possible to create more than one
instance of a singleton class. And provide a global point of access to
it.
▪ Forces:
▪ The use of a public constructor cannot guarantee that no more than
one instance will be created.
▪ The singleton instance must also be accessible to all classes that
require it, therefore it must often be public.
The Singleton Pattern

▪ Solution:

▪Have the constructor private to ensure that no other class


will be able to create an instance of the class singleton.
▪Define a public static method, The first time this method is
called , it creates the single instance of the class
“singleton” and stores a reference to that object in a static
private variable .
The Singleton Pattern

Singleton Object identifier for singleton


instance, static and private
-uniqueInstance
Private constructor only accessible
-singletonData via getInstance()
-Singleton( )
Returns object identifier for
+getInstance( ):Singelton
unique instance, static

+singletonOperation( )

getInstance( ) {
if ( uniqueInstance == null )
{ uniqueInstance = new Singleton( ) }
return uniqueInstance
}
The Singleton Pattern

▪ Example:

▪ Exercise:
▪ Can you generalize the singleton pattern such that a certain
predefined number of instances could be created from a class?
Immutable Pattern
▪ Context:
▪An immutable object is an object that has a state that
never changes after creation.

▪ Problem:
▪How do you create a class whose instances are
immutable?

▪ Forces:
▪There must be no loopholes that would allow ‘illegal’
modification of an immutable object.
Immutable Pattern

▪ Solution:
▪Ensure that the constructor of the immutable class is the
only place where the values of instance variables are set or
modified.

▪Instance methods which access properties must not change


instance variables.

▪If a method that would otherwise modify an instance


variable is required, then it has to return a new instance of
the class.
Design Patterns & Class libraries

▪ Class Libraries :
▪ Reusable code, ex. String class, Math class,…
▪ Language dependent.

▪ Design Patterns :
▪ Reusable Design: Design problem & solution
▪ Language independent.
Motivation for Design Patterns
▪ Patterns provide common language between developers. For
example if a developer tells another developer that he is using
a Singleton, the other developer should know exactly what this
means.

▪ They help to improve software quality & reduce development time.


As They provide proven solution and are based on the basic
principles and heuristics of object orientated design like :
▪ Program to an interface not to an implementation
▪ Favor object composition over inheritance
▪ Encapsulates what varies
▪ Low of Demeter
▪ Reduce coupling
As we will see later
Some Structural Patterns

▪ Delegation pattern
▪For reusing methods.
▪ Adapter Pattern
▪Convert programming interface of one class into another
▪ Façade Pattern
▪ Defines a higher level interface that makes subsystems
easier to use.
▪ Read-only interface Pattern
▪To give privileges to some classes to be able to modify
attributes of objects that are otherwise immutable.
▪ Abstraction-Occurrence Pattern (Not GOF)
Delegation Pattern
▪ Context:
▪You are designing a method in a class
▪You realize that another class has a method which
provides the required service
▪Inheritance is not appropriate
▪ E.g. because the is-a rule does not apply

▪ Problem:
▪How can you most effectively make use of a method that
already exists in the other class?

▪ Forces:
▪You want to minimize development cost by reusing
methods.
Delegation Pattern

▪ Solution:

▪ The delegating method in the delegator class calls a method


in the delegate class to perform the required task
(delegatingMethod() reusing method() of the delegate class).
An association must exist between the delegator and the
delegate classes.
Delegation Pattern
▪ Example:
Assume that there exist a LinkedList class and you need to
create a Stack class. You can create the Stack class using the
delegation pattern. The method push() of Stack calls the
method addfirst() of LinkedList, etc.
Delegation Pattern

▪Anti-Patterns:

1. It’s common for people to overuse generalization and inherit


the method that’s to be reused

—Ex: What is the problem with making stack a subclass of


linked list?

2. Duplication of chunks of code.


The Delegation Pattern

▪ Design Principles:

▪ The delegation pattern brings together two design principles


that encourage flexible design:

1. Favoring association/composition over inheritance when the


full power of inheritance is not needed.

1. Avoiding duplication of chunks of code.


References

▪ Design Patterns: Elements of Reusable Object-Oriented Software,


Gamma, Helm, Johnson and Vlissides, addison-Wesley, 1995. (GOF)

▪ Design Patterns Java Workbook, Steven John Metsker, Addison-


Wesley, 2002.

▪ Head First Design Patterns, Freeman and Freeman, O'Reilly,2004.

▪ Java Design Patterns - A Tutorial, James W. Cooper, Addison-Wesley,


2000.

▪ Design patterns for dummies, Steven Holzner.

You might also like