Design Patterns A General reusable solution to a commonly occurring problem in software design....

20
Design Patterns A General reusable solution to a commonly occurring problem in software design. • Creational: Deal with object creation mechanisms – Example: Abstract Factory • Structural: Simplify design by identifying a simple way to realize relationships between entities. – Example: Bridge • Behavioral: Identify common communication patterns between objects and realize these patterns. – Example: Strategy

Transcript of Design Patterns A General reusable solution to a commonly occurring problem in software design....

Page 1: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Design PatternsA General reusable solution to a commonly occurring

problem in software design.

• Creational: Deal with object creation mechanisms– Example: Abstract Factory

• Structural: Simplify design by identifying a simple way to realize relationships between entities.– Example: Bridge

• Behavioral: Identify common communication patterns between objects and realize these patterns.– Example: Strategy

Page 2: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Abstract Factory

Page 3: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Abstract Factory (Creational)• Applying context: A client or application may want to use different types of objects

with common behavior and easily change between those types before or during execution.

• Problem: Changing types of objects through the application is difficult

because instantiation requires to know the type of object to be declared. Also different types might have different methods to use their functionality.

Page 4: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Abstract Factory (Creational)• Solution: Isolate the client code from object creation by having client just

ask for (and handle) a factory object. This factory object is an abstract data type which can only be used through an interface.

• Discussion: By having the client dependant just on the interface of the

Abstract Type Product provided by the Abstract Factory. Each Concrete Product must be able to work under the interface that the Abstract Product provides. The client is able to change the Concrete Type Product by changing the Concrete Factory that is being used.

This change is usually done by changing one line in one file.

Page 5: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:
Page 6: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Factory Classes

Page 7: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Product Classes

Page 8: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Client

Page 9: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Bridge Design Pattern

Page 10: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Problem

• The use of subclasses to provide additional implementation has caused the “hardening of the software arteries.”

• This limits the ability to independently extend the abstraction and the implementation.

Page 11: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

UML Class Diagram

Page 12: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Purpose

• To “decouple abstraction from its implementation.”

• To “publish interface in an inheritance hierarchy, and bury implementation within it’s own inheritance hierarchy.”

http://sourcemaking.com/design_patterns/bridge

Page 13: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:
Page 14: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

http://java.dzone.com/articles/design-patterns-bridge

Page 15: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Disadvantages

• Although it provides more flexibility, it increases the complexity of the software.

• Might have performance issues due to longer communication path between the abstraction and implementation.

http://java.dzone.com/articles/design-patterns-bridge

Page 16: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Strategy

Context: Your system has lots of different behaviors that can change in runtime and not easily manageable.

Page 17: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

“Strategy”’s Strategy• Separate behavior from implementation

(encapsulate behavior).

http://www.netobjectives.com/PatternRepository/index.php?title=TheStrategyPattern

http://images1.wikia.nocookie.net/__cb20090908183029/finalfantasy/images/f/ff/Ff13-paradigm.jpg

Page 18: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

http://www.lepus.org.uk/ref/companion/Strategy.xml

http://en.wikipedia.org/wiki/File:Strategy.JPG

Page 19: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

class StrategyExample {public static void main(String[] args) {Context context; context = new Context(new ConcreteStrategyAdd());int resultA = context.executeStrategy(3,4);context = new Context(new ConcreteStrategySubtract());int resultB = context.executeStrategy(3,4);context = new Context(new ConcreteStrategyMultiply());int resultC = context.executeStrategy(3,4); }

}interface Strategy {int execute(int a, int b); }class ConcreteStrategyAdd implements Strategy {

public int execute(int a, int b) {System.out.println("Called ConcreteStrategyAdd's execute()");return a + b;}

}class ConcreteStrategySubtract implements Strategy {

public int execute(int a, int b) {System.out.println("Called ConcreteStrategySubtract's execute()");return a - b;}

}class ConcreteStrategyMultiply implements Strategy {

public int execute(int a, int b) {System.out.println("Called ConcreteStrategyMultiply's execute()"); return a * b;}

}class Context {

private Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; }public int executeStrategy(int a, int b) { return strategy.execute(a, b); }

} http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Strategy#Java

Page 20: Design Patterns A General reusable solution to a commonly occurring problem in software design. Creational: Deal with object creation mechanisms – Example:

Strategy v Bridge

Same UML Diagram

Different Intent

Strategy = Behavior

Bridge = Structure

http://en.wikipedia.org/wiki/Strategy_pattern#Strategy_versus_Bridge