SlideShare a Scribd company logo
Design Pattern
Thibaut de Broca
Introduction
History
1977: Christopher Alexander introduces the idea of patterns:
successful solutions to problems. (Pattern Language)
1987: Ward Cunningham and Kent Beck leverage
Alexander’s idea in the context of an OO language.
1987: Eric Gamma’s dissertation on importance of patterns
and how to capture them.
1994: The book ====>
Definition
In software engineering, a software design pattern is a general reusable solution to a
commonly occurring problem within a given context in software design. It is not a
finished design that can be transformed directly into source or machine code.
Fundamental Principles
‘Avoid Tight coupling’
Fundamental Principles
‘Code as modular black boxes’ && ‘Single responsibility principles’
Fundamental Principles
‘Program to an interface and not to an implementation’
The class shouldn’t care about which class they use, but more which
behaviour they will use.
To send messages from class to class, send interfaces not classes.
Be careful ! Don’t over-use interface => 1 class != 1 interface
Fundamental Principles
public class Main {
public static void main(String[] args) {
Animal[] animals = new Animal[3];
animals[0] = new Dog();
animals[1] = new Cat();
animals[2] = new Fox();
for (Animal animal : animals) {
animal.speak();
}
}
}
An interface is a reference type in Java.
It is similar to class. It is a collection of abstract methods. A class implements an interface,
thereby inheriting the abstract methods of the interface. Along with abstract methods, an
interface may also contain constants, default methods, static methods, and nested types.
Interlude: Interface - Recall
Animal
Dog Cat Fox
public interface Animal {
public void speak();
}
public class Dog implements Animal {
public void speak() {System.out.println("Wouaf Wouaf!");}
}
public class Cat implements Animal {
public void speak() {System.out.println("Miaouh!");}
}
public class Fox implements Animal {
public void speak() {
System.out.println("What does the Fox say ?");
}
}
‘Favor Object Composition over inheritance’
The class shouldn’t care about which class they use, but more which
behaviour they will use.
To send messages from class to class, send interfaces not classes.
Bad use of inheritance:
My personal opinion is that there is no "better" or "worse" principle to design. There is "appropriate" and "inadequate" design for the concrete task. In
other words - I use both inheritance or composition, depending on the situation. The goal is to produce smaller code, easier to read, reuse and
eventually extend further.
class Stack extends ArrayList {
public void push(Object value) { … }
public Object pop() { … }
}
Fundamental Principles
1
4
54
2
class Stack extends {
private ArrayList myList;
public void push(Object value) { … }
public Object pop() { … }
}
‘A design must be open for extension but closed for modification’
(Open/Close Principle)
=> Once a class is written, you should not have to modify it to add new
functionality.
Fundamental Principles
‘Avoid Duplication’
Fundamental Principles
- SRP: Single Responsibility Principle
- OCP: Open Close Principle
- LSP: Liskov Substitution Principle. (Derived classes must be substitutable for their
base classes).
- ISP: Interface Segregation Principle. (Client should not be forced to depend upon
interfaces they do not use).
- DIP: Dependency Inversion Principle. High level modules should not depend upon
low level modules. Both should depend upon abstraction).
Fundamental Principles
Keep in mind: “When Coding, 80% of your time is spent by reading code”. So take
care of formatting and naming !
Others Principles
- DRY: Don’t Repeat Yourself. Can Lead to maintenance nightmare
- YAGNI: You ain’t gonna need it. Do not add functionality until necessary.
- KISS: Keep It Simple, Stupid. Avoid accidental complexity.
- Boy Scout Rules: Always leave the campground cleaner than you found it.
Others Principles
UML Recalls
Class Diagram
· Upper part contains the name of the class
· Middle part describes attributes of the class
· Bottom part describes the methods of the class
public class Example {
public static int CONSTANT= 0;
private String attribute1;
public String method() {
return attribute1;
}
private static void privateMethod(int c) {
CONSTANT+=c;
}
}
UML - Recalls
Relations between classes
The simplest of relation :
· Class A depends on Class B if, at some point, it uses Class B
· Symbolized by dotted line and and open arrow indicating the dependency direction
public class A {
public String method(B b) {
return b.toString();
}
}
public class B {
public toString() {
return "B";
}
}
UML - Recalls
Association
Stronger dependency from class A to class B, namely, A uses and contains an instance of class B:
· It is represented by a plain line and an open arrow
· This association can have a cardinality 0..1 or 1..n
· It is directed , in the example below A knows B, but B does not know anything about A
public class A {
private B b;
public void setB(B b) {
this.b = b;
}
}
public class B {
public toString() {
return "B";
}
}
UML - Recalls
Aggregation
Our example models Rockband and members. A rockband can have several members. And Members can be in several Rockand.
The addition of the diamond adds information about the life of the objects
· The empty diamond signifies that ‘Member’ objects can be shared between several ‘RockBand’ objects
· When an ‘RockBand’ object is destroyed, then the instances of ‘Member’ it was associated with do not disappear.
UML - Recalls
Composition
· The plain/full diamond indicates that the contained objects are not shared
· When the container object is destroyed, the contained objects disappear with it
UML - Recalls
class Tattoo {
String description;
public Tattoo(String description) {
this.description = description;
}
}
class Member {
private String name;
private List<Tattoo> tattoos = new
ArrayList<>();
[...]
public ink(String description) {
tattoos.add(new Tattoo(description));
}
}
class RockBand {
private List<Member> members = new ArrayList<>();
public void addMember(Member m) {
members.add(m);
}
public Member getMember(int num) {
return members.get(num);
}
public void toString() {
[..]
}
}
Inheritance / Generalization
· Inheritance enables to create classes hierarchies sharing same attributes
· Children classes also have the attributes and methods of the parent classes,
· These attributes and methods are defined for the objects the children classes.
· This mechanism enables Polymorphism
Properties
· Transitivity: If B inherits from A and C inherits from B then C inherits from A
· Not reflexive: No class can inherit from itself
· Not symmetric: If B inherits from A, A does not inherit from B
· Not cyclic : If B inherits from A and C inherits from B then A can't inherit from C
UML - Recalls
Inheritance / Generalization - Code
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
public class Cat extends Animal {
public Cat(String name) {
super(name); // Call to the parent class constructor
}
public void meow() {
System.out.println("Miaou " + super.getName() + " Miaou");
}
}
public class Dog extends Animal {
public Dog(String name) {
super(name); // Call to the parent class constructor
}
public void bark() {
System.out.println("Wouf " + super.getName() + " Wouf");
}
}
UML - Recalls
Design Patterns
Pattern Catalogue
5 mains classes
Creational Pattern
Creational design patterns are design patterns that deal with object creation
mechanisms, trying to create objects in a manner suitable for the situation.
- Factory Method Pattern
- Abstract Factory
- Singleton
- Builder
- Prototype
Intent:
It allows to create objects without specifying their class (method whose main goal
is to create a class instance).
Application:
- A class can not anticipate the type of objects it must create (i.e.: framework).
- A class wants its subclasses to specify the type of objects it creates.
- A class needs controls over the creation of its objects.
Factory Method Pattern
// Problem: A depends on B
public class A {
public void doIt() {
B b = new B()
}
}
Structure:
public abstract class Animal() {}
public class Dog extend Animal() {}
public class Cat extend Animal() {}
public class Dolphin extend Animal() {}
public interface AnimalFactory {
abstract public Animal create(String type);
}
public AnimalFactory () {
public Animal create(String type) {
switch(type) {
case "Dog": return new Dog(); break;
case "Cat": return new Cat(); break;
case "Dolphin": return new Dolphin(); break;
default : throw Exception("Animal " + type + " is unknown.");
}
}
}
public Main() {
public static void main(String[] args) {
Animalfactory factory = new Animalfactory();
Animal cat = factory.create("cat");
}
}
Factory Method Pattern
Intent:
We want to have only one instance of a class.
Application:
- Instantiate an object can take time and
memory.
- We only need one instance.
// Example:
// Problem: For factory, we only need one
instance
Singleton Pattern
Singleton Pattern
Structure:
The trick is to use a static variable to
remember the instance.
Then, why not to create a static method ?
The big difference between a singleton and a bunch of static
methods is that singletons can implement interfaces, so you can
pass around the singleton as if it were "just another"
implementation.
MyClass
- static MyClass myClass;
static getInstance(): MyClass
C
public class AnimalFactory {
private AnimalFactory();
private static AnimalFactory animalFactory;
public static AnimalFactory getInstance() {
if (animalFactory == null) {
animalFactory = new AnimalFactory();
}
return animalFactory;
}
//…...
}
public Main() {
public static void main(String[] args) {
Animal cat = Animalfactory.getInstance().create("cat");
}
}
Creational Pattern
- Factory Method Pattern
- Abstract Factory
- Singleton
- Builder
- Prototype (très utilisé dans Javascript)
Behavioral Pattern
Behavioral patterns are concerned with the assignment of responsibilities between
objects, or, encapsulating behavior in an object and delegating requests to it.
- Observer
- Strategy
- State
- Visitor
- Memento
- Mediator
- Iterator
Intent:
Supports a relation One To Many so that
several objects get notified when an
object changes its state and can react.
Application:
- Widely use with IHM when several
elements of the view change when
the model changes.
Observer Pattern
Roles: subject and several observers
- Subject:
- Can modify his state.
- He notifies observers when he has
changed.
- Can give his new state
- Observers
- Can subscribe/unsubscribe to a subject
Observer Pattern
subscribe
public abstract class AbstractSubject {
private List<Observer> observers = new ArrayList<Observer>();
public void attach(Observer observer){observers.add(observer);}
public void notifyAllObservers(){
for (Observer observer : observers) {
observer.update();
}
}
}
public class Subject extends AbstractSubject{
private int state;
public int getState() {return state;}
public void setState(int state) {
this.state = state;
notifyAllObservers();
}
}
public class BinaryObserver extends Observer{
public BinaryObserver(Subject subject){
this.subject = subject;
this.subject.attach(this);
}
@Override
public void update() {
System.out.println( "Binary String: " + Integer.toBinaryString(
subject.getState() ) );
}
}
public class ObserverPatternDemo {
public static void main(String[] args) {
AbstractSubject subject = new Subject();
new HexaObserver(subject);
new OctalObserver(subject);
new BinaryObserver(subject);
System.out.println("First state change: 15");
subject.setState(15);
System.out.println("Second state change: 10");
subject.setState(10);
}
}
Observer Pattern
Observer Pattern
Abstract Subject => Classe java.util.Observable
- addObserver(Observer obs)
- notifyObservers();
Concrete Subject => extends Observable
- When state change, you should call notifyObservers();
Abstract Observer: Interface java.util.Observer
- update(Observable obs, Object args);
Concrete Observer: implements java.util.Observer
Behavioral Pattern
- Observer
- Strategy
- State
- Visitor
- Memento
- Mediator
- Iterator
- Interpreter
Intent:
It allows to switch between differents algorithms to accomplish a task.
Application:
- Different variants of an algorithm
- Many related classes differ only in their behaviour
Strategy Pattern
Subject => Interface to outside world
Strategy (Algorithm) => common interface for the differents
algorithms.
Strategy Pattern
Program
- Input: read file
- Output: filtered file
Examples of 4 filters:
- No filtering
- Only words that start with t
- Only words longer than 5 characters
- Only words that are palindroms
Strategy Pattern - Example
public class StrategyPattern {
public static void main(String[] args) {
Context context = new Context();
String filename = args[0];
System.out.println("n* Default(Alll): ");
context.filter(filename);
System.out.println("n* Start with T: ");
context.changeStrategy(new StartWithT());
context.filter(filename);
}
}
public interface CheckStrategy {
public boolean check(String s);
}
public class All implements CheckStrategy {
@Override
public boolean check(String s) {
return true;
}
}
public class StartWithT implements CheckStrategy {
@Override
public boolean check(String s) {
return s != null && s.startWith("t");
}
}
public class Context {
private CheckStrategy strategy;
public Context() {this.strategy = new All();}
public void changeStrategy(CheckStrategy strategy) {
this.strategy = strategy;
}
public void filter(String filename) throws IOException {
BufferedReader infile = new BufferedReader(new
FileReader(filename));
String buffer = null;
while ((buffer = infile.readLine() != null) {
StringTokenizer word = new StringTokenizer(buffer);
while (words.hasMoreTokens()) {
String word = words.nextToken();
if (strategy.check(word)) {
Ssytem.out.println(word);
}
}
}
}
}
Strategy Pattern - Example
Behavioral Pattern
- Observer
- Strategy
- State
- Visitor
- Memento
- Mediator
- Iterator
- Interpreter
State Pattern
Intent:
Implements a state machine in an object-
oriented way.
It lets an object show other methods after a
change of internal state.
Application:
- an application is characterized by large and
numerous case statements. These cases
vector flow of control based on the state of
the application.
interface Statelike {
void writeName(StateContext context, String name);
}
class StateLowerCase implements Statelike {
@Override
public void writeName(final StateContext context, final String name) {
System.out.println(name.toLowerCase());
context.setState(new StateMultipleUpperCase());
}
}
class StateMultipleUpperCase implements Statelike {
/** Counter local to this state */
private int count = 0;
@Override
public void writeName(final StateContext context, final String name) {
System.out.println(name.toUpperCase());
/* Change state after StateMultipleUpperCase's writeName() gets invoked twice */
if (++count > 1) {context.setState(new StateLowerCase());}
}
}
class StateContext {
private Statelike myState;
StateContext() {
setState(new StateLowerCase());
}
void setState(final Statelike newState) {myState = newState;}
public void writeName(final String name) {
myState.writeName(this, name);
}
}
State Pattern
public class DemoOfClientState {
public static void main(String[] args) {
final StateContext sc = new StateContext();
sc.writeName("Monday");
sc.writeName("Tuesday");
sc.writeName("Wednesday");
sc.writeName("Thursday");
sc.writeName("Friday");
sc.writeName("Saturday");
sc.writeName("Sunday");
}
}
Structural Patterns
Design patterns that ease the design by identifying a simple way to realize
relationships between entities.
- Proxy
- Decorator
- Facade
- Adapter
- Aggregate
- Bridge
Proxy Pattern
Translation in French:
Proxy <=> ?????????
Intent:
- A proxy, in its most general form, is a
class functioning as an interface to
something else.
Application:
- The proxy could interface to
anything: a network connection, a
large object in memory, a file.
- For sensitive objects: can verify that
the caller has the authorization.
interface Image {
public void displayImage();
}
// On System A
class RealImage implements Image {
private String filename = null;
public RealImage(final String filename) {
this.filename = filename;
loadImageFromDisk();
}
private void loadImageFromDisk() {System.out.println("Loading " + filename);}
public void displayImage() {System.out.println("Displaying " + filename);}
}
// On System B
class ProxyImage implements Image {
private RealImage image = null;
private String filename = null;
public ProxyImage(final String filename) {
this.filename = filename;
}
public void displayImage() {
if (image == null) {
image = new RealImage(filename);
}
image.displayImage();
}
}
class ProxyExample {
public static void main(final String[] arguments) {
final Image image1 = new ProxyImage("HiRes_10MB_Photo1");
final Image image2 = new ProxyImage("HiRes_10MB_Photo2");
image1.displayImage(); // loading necessary
image1.displayImage(); // loading unnecessary
image2.displayImage(); // loading necessary
image2.displayImage(); // loading unnecessary
image1.displayImage(); // loading unnecessary
}
}
Proxy Pattern - Example
- Proxy
- Decorator
- Facade
- Adapter
- Aggregate
- Bridge
Structural Patterns
Decorator Pattern
Intent:
- A wrapper that adds functionality to a
class. It is stackable.
Application:
- The decorator pattern is often useful
for adhering to the “Single
Responsibility Principle”.
- It allows functionality to be divided
between classes with unique areas
of concern.
Decorator Pattern
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {System.out.println("Shape: Rectangle");}
}
public class Circle implements Shape {
@Override
public void draw() {System.out.println("Shape: Circle");}
}
public abstract class ShapeDecorator implements Shape {
protected Shape decoratedShape;
public ShapeDecorator(Shape decoratedShape){
this.decoratedShape = decoratedShape;
}
public void draw(){decoratedShape.draw();}
}
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape decoratedShape) {
super(decoratedShape);
}
@Override
public void draw() {
decoratedShape.draw();
setRedBorder(decoratedShape);
}
private void setRedBorder(Shape decoratedShape){
System.out.println("Border Color: Red");
}
public class DecoratorPatternDemo {
public static void main(String[] args) {
Shape circle = new Circle();
Shape redCircle = new RedShapeDecorator(new Circle());
Shape redRectangle = new RedShapeDecorator(new Rectangle());
System.out.println("Circle with normal border");
circle.draw();
System.out.println("nCircle of red border");
redCircle.draw();
System.out.println("nRectangle of red border");
redRectangle.draw();
}
}
- Proxy
- Decorator
- Facade
- Adapter
- Aggregate
- Bridge
Structural Patterns
Intent:
- Provide a unified interface to a set of
interfaces in a subsystem.
- Defines a higher-level interface that
makes the subsystem easier to use.
Application:
- A simple interface is required to
access a complex system.
- An entry point is needed to each
level of layered software.
Façade Pattern
Façade Pattern
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {System.out.println("Rectangle::draw()");}
}
public class Square implements Shape {
@Override
public void draw() {System.out.println("Square::draw()");}
}
public class Circle implements Shape {
@Override
public void draw() {System.out.println("Circle::draw()");}
}
public class ShapeMaker {
private Shape circle;
private Shape rectangle;
private Shape square;
public ShapeMaker() {
circle = new Circle();
rectangle = new Rectangle();
square = new Square();
}
public void drawCircle(){circle.draw();}
public void drawRectangle(){rectangle.draw();}
public void drawSquare(){square.draw();}
}
public class FacadePatternDemo {
public static void main(String[] args) {
ShapeMaker shapeMaker = new ShapeMaker();
shapeMaker.drawCircle();
shapeMaker.drawRectangle();
shapeMaker.drawSquare();
}
Generic:
Example:
- Proxy
- Decorator
- Facade
- Adapter
- Aggregate
- Bridge
Structural Patterns
Intent:
- Allows the interface of an existing
class to be used as another interface
- Converts one interface to another so
that it matches what the client is
expecting
Application:
- Example: an adapter that converts the
interface of a Document Object Model
of an XML document into a tree
structure that can be displayed.
Adapter Pattern (Wrapper)
public interface MediaPlayer {
public void play(String audioType, String fileName);
}
public interface AdvancedMediaPlayer {
public void playVlc(String fileName);
public void playMp4(String fileName);
}
public class VlcPlayer implements AdvancedMediaPlayer{
@Override
public void playVlc(String fileName) {System.out.println("Playing vlc file: "+
fileName);}
@Override
public void playMp4(String fileName) {// do nothing}
}
public class Mp4Player implements AdvancedMediaPlayer{
@Override
public void playVlc(String fileName) {// do nothing}
@Override
public void playMp4(String fileName) {System.out.println("Playing mp4 file: "+
fileName);}
}
Example:
Adapter Pattern
public class MediaAdapter implements MediaPlayer {
AdvancedMediaPlayer advancedMusicPlayer;
public MediaAdapter(String audioType) {
If (audioType.equalsIgnoreCase("vlc") ) {
advancedMusicPlayer = new VlcPlayer();
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer = new Mp4Player();
}
}
@Override
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("vlc")){
advancedMusicPlayer.playVlc(fileName);
} else if(audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer.playMp4(fileName);
}
}
}
public class AudioPlayer implements MediaPlayer {
MediaAdapter mediaAdapter;
@Override
public void play(String audioType, String fileName) {
// inbuilt support to play mp3 music files
if(audioType.equalsIgnoreCase("mp3")) {
System.out.println("Playing mp3 file. Name: " + fileName);
} else if (audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")) {
// mediaAdapter is providing support to play other file formats
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
} else {
System.out.println("Invalid media. " + audioType + " format not supported");
}
Example:
Adapter Pattern
public class AdapterPatternDemo {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp3", "beyond the horizon.mp3");
audioPlayer.play("mp4", "alone.mp4");
audioPlayer.play("vlc", "far far away.vlc");
audioPlayer.play("avi", "mind me.avi");
}
}
- Proxy
- Decorator
- Facade
- Adapter
- Aggregate
- Bridge
Structural Patterns
- Encapsulate what varies, OCP (Open Close Principle).
- 1 class, 1 responsability.
- Program against an interface, not an implementation, DIP.
- Prefer composition, over inheritance.
- Loose coupling, modular black boxes.
Golden OO-principles
Approach:
- Understand your design context
- Examine the patterns catalogue
- Identify and study related pattern
- Apply suitable pattern
Pitfalls:
- Selectins wrong patterns
- Inappropriate use of patterns
How to choose a Pattern ?
Read
The Book !
- Wikipedia
- tutorialspoint.com
- http://www.slideshare.net/mkruthika/software-design-patterns-ppt
- http://www.slideshare.net/HermanPeeren/design-patterns-illustrated/
- ...
Credits

More Related Content

What's hot

Design Patterns
Design PatternsDesign Patterns
Design Patterns
Anuja Arosha
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns pptAman Jain
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
Pankhuree Srivastava
 
Design patterns
Design patternsDesign patterns
Design patterns
Luis Goldster
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
Shahzad
 
Jeet ooad unit-2
Jeet ooad unit-2Jeet ooad unit-2
Jeet ooad unit-2
Jitendra s Rathore
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
Asma CHERIF
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
paramisoft
 
CS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design PatternsCS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design Patterns
Kwangshin Oh
 
Software Engineering - chp4- design patterns
Software Engineering - chp4- design patternsSoftware Engineering - chp4- design patterns
Software Engineering - chp4- design patterns
Lilia Sfaxi
 
Creational pattern
Creational patternCreational pattern
Creational pattern
Himanshu
 
Pipes & Filters Architectural Pattern
Pipes & Filters Architectural PatternPipes & Filters Architectural Pattern
Pipes & Filters Architectural Pattern
Fredrik Kivi
 
Composite pattern
Composite patternComposite pattern
Composite pattern
Shakil Ahmed
 
CS8592-OOAD Lecture Notes Unit-1
CS8592-OOAD Lecture Notes Unit-1CS8592-OOAD Lecture Notes Unit-1
CS8592-OOAD Lecture Notes Unit-1
Gobinath Subramaniam
 
Java Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | EdurekaJava Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | Edureka
Edureka!
 
Facade design pattern
Facade design patternFacade design pattern
Facade design pattern
Neelima Sanagavarapu
 

What's hot (20)

Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Design Pattern For C# Part 1
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Jeet ooad unit-2
Jeet ooad unit-2Jeet ooad unit-2
Jeet ooad unit-2
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)Design pattern (Abstract Factory & Singleton)
Design pattern (Abstract Factory & Singleton)
 
CS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design PatternsCS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design Patterns
 
Software Engineering - chp4- design patterns
Software Engineering - chp4- design patternsSoftware Engineering - chp4- design patterns
Software Engineering - chp4- design patterns
 
Design pattern-presentation
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
 
Creational pattern
Creational patternCreational pattern
Creational pattern
 
Pipes & Filters Architectural Pattern
Pipes & Filters Architectural PatternPipes & Filters Architectural Pattern
Pipes & Filters Architectural Pattern
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Component level design
Component   level designComponent   level design
Component level design
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
CS8592-OOAD Lecture Notes Unit-1
CS8592-OOAD Lecture Notes Unit-1CS8592-OOAD Lecture Notes Unit-1
CS8592-OOAD Lecture Notes Unit-1
 
Java Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | EdurekaJava Design Patterns Tutorial | Edureka
Java Design Patterns Tutorial | Edureka
 
Facade design pattern
Facade design patternFacade design pattern
Facade design pattern
 

Viewers also liked

Design pattern
Design patternDesign pattern
Design pattern
Jasdhir Singh
 
Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016
Markus Winand
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
mkruthika
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
Herman Peeren
 
Design pattern
Design patternDesign pattern
Design pattern
Adel Al-ajami
 
Pagination Done the Right Way
Pagination Done the Right WayPagination Done the Right Way
Pagination Done the Right Way
Markus Winand
 
Software Design Patterns - Selecting the right design pattern
Software Design Patterns - Selecting the right design patternSoftware Design Patterns - Selecting the right design pattern
Software Design Patterns - Selecting the right design pattern
Joao Pereira
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
Aniruddha Chakrabarti
 
Modern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial Databases
Markus Winand
 
Design pattern part 1
Design pattern   part 1Design pattern   part 1
Design pattern part 1
Suresh Kumar Rajendran
 
PATTERNS05 - Guidelines for Choosing a Design Pattern
PATTERNS05 - Guidelines for Choosing a Design PatternPATTERNS05 - Guidelines for Choosing a Design Pattern
PATTERNS05 - Guidelines for Choosing a Design Pattern
Michael Heron
 
Factory design pattern
Factory design patternFactory design pattern
Factory design patternFarhad Safarov
 
Factory Pattern
Factory PatternFactory Pattern
Factory Pattern
Anindya Sundar Paul
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - Introduction
Mudasir Qazi
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
Mudasir Qazi
 
Proxy Design Pattern
Proxy Design PatternProxy Design Pattern
Proxy Design Pattern
Anjan Kumar Bollam
 
Floor plan for cashew factory by sotonye anga
Floor plan for cashew factory by sotonye angaFloor plan for cashew factory by sotonye anga
Floor plan for cashew factory by sotonye anga
Sotonye anga
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
Mudasir Qazi
 
How I Learned To Apply Design Patterns
How I Learned To Apply Design PatternsHow I Learned To Apply Design Patterns
How I Learned To Apply Design Patterns
Andy Maleh
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
Julie Iskander
 

Viewers also liked (20)

Design pattern
Design patternDesign pattern
Design pattern
 
Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
Design pattern
Design patternDesign pattern
Design pattern
 
Pagination Done the Right Way
Pagination Done the Right WayPagination Done the Right Way
Pagination Done the Right Way
 
Software Design Patterns - Selecting the right design pattern
Software Design Patterns - Selecting the right design patternSoftware Design Patterns - Selecting the right design pattern
Software Design Patterns - Selecting the right design pattern
 
Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)Design Patterns (Examples in .NET)
Design Patterns (Examples in .NET)
 
Modern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial DatabasesModern SQL in Open Source and Commercial Databases
Modern SQL in Open Source and Commercial Databases
 
Design pattern part 1
Design pattern   part 1Design pattern   part 1
Design pattern part 1
 
PATTERNS05 - Guidelines for Choosing a Design Pattern
PATTERNS05 - Guidelines for Choosing a Design PatternPATTERNS05 - Guidelines for Choosing a Design Pattern
PATTERNS05 - Guidelines for Choosing a Design Pattern
 
Factory design pattern
Factory design patternFactory design pattern
Factory design pattern
 
Factory Pattern
Factory PatternFactory Pattern
Factory Pattern
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - Introduction
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory Pattern
 
Proxy Design Pattern
Proxy Design PatternProxy Design Pattern
Proxy Design Pattern
 
Floor plan for cashew factory by sotonye anga
Floor plan for cashew factory by sotonye angaFloor plan for cashew factory by sotonye anga
Floor plan for cashew factory by sotonye anga
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
How I Learned To Apply Design Patterns
How I Learned To Apply Design PatternsHow I Learned To Apply Design Patterns
How I Learned To Apply Design Patterns
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2
 

Similar to Design pattern

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
Suresh Mohta
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
henokmetaferia1
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
kishu0005
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
DevaKumari Vijay
 
Only oop
Only oopOnly oop
Only oop
anitarooge
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
EmanAsem4
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
OOP Concepets and UML Class Diagrams
OOP Concepets and UML Class DiagramsOOP Concepets and UML Class Diagrams
OOP Concepets and UML Class Diagrams
Bhathiya Nuwan
 
L02 Software Design
L02 Software DesignL02 Software Design
L02 Software Design
Ólafur Andri Ragnarsson
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
shubhra chauhan
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
vishal choudhary
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
Michael Heron
 
Abstraction
AbstractionAbstraction
Abstraction
zindadili
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
talha ijaz
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 

Similar to Design pattern (20)

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Only oop
Only oopOnly oop
Only oop
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
OOP Concepets and UML Class Diagrams
OOP Concepets and UML Class DiagramsOOP Concepets and UML Class Diagrams
OOP Concepets and UML Class Diagrams
 
L02 Software Design
L02 Software DesignL02 Software Design
L02 Software Design
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct class
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - Abstraction
 
Java02
Java02Java02
Java02
 
Abstraction
AbstractionAbstraction
Abstraction
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 

Recently uploaded

Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
KrzysztofKkol1
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 

Recently uploaded (20)

Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Designing for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web ServicesDesigning for Privacy in Amazon Web Services
Designing for Privacy in Amazon Web Services
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 

Design pattern

  • 3. History 1977: Christopher Alexander introduces the idea of patterns: successful solutions to problems. (Pattern Language) 1987: Ward Cunningham and Kent Beck leverage Alexander’s idea in the context of an OO language. 1987: Eric Gamma’s dissertation on importance of patterns and how to capture them. 1994: The book ====> Definition In software engineering, a software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code.
  • 6. ‘Code as modular black boxes’ && ‘Single responsibility principles’ Fundamental Principles
  • 7. ‘Program to an interface and not to an implementation’ The class shouldn’t care about which class they use, but more which behaviour they will use. To send messages from class to class, send interfaces not classes. Be careful ! Don’t over-use interface => 1 class != 1 interface Fundamental Principles
  • 8. public class Main { public static void main(String[] args) { Animal[] animals = new Animal[3]; animals[0] = new Dog(); animals[1] = new Cat(); animals[2] = new Fox(); for (Animal animal : animals) { animal.speak(); } } } An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Interlude: Interface - Recall Animal Dog Cat Fox public interface Animal { public void speak(); } public class Dog implements Animal { public void speak() {System.out.println("Wouaf Wouaf!");} } public class Cat implements Animal { public void speak() {System.out.println("Miaouh!");} } public class Fox implements Animal { public void speak() { System.out.println("What does the Fox say ?"); } }
  • 9. ‘Favor Object Composition over inheritance’ The class shouldn’t care about which class they use, but more which behaviour they will use. To send messages from class to class, send interfaces not classes. Bad use of inheritance: My personal opinion is that there is no "better" or "worse" principle to design. There is "appropriate" and "inadequate" design for the concrete task. In other words - I use both inheritance or composition, depending on the situation. The goal is to produce smaller code, easier to read, reuse and eventually extend further. class Stack extends ArrayList { public void push(Object value) { … } public Object pop() { … } } Fundamental Principles 1 4 54 2 class Stack extends { private ArrayList myList; public void push(Object value) { … } public Object pop() { … } }
  • 10. ‘A design must be open for extension but closed for modification’ (Open/Close Principle) => Once a class is written, you should not have to modify it to add new functionality. Fundamental Principles
  • 12. - SRP: Single Responsibility Principle - OCP: Open Close Principle - LSP: Liskov Substitution Principle. (Derived classes must be substitutable for their base classes). - ISP: Interface Segregation Principle. (Client should not be forced to depend upon interfaces they do not use). - DIP: Dependency Inversion Principle. High level modules should not depend upon low level modules. Both should depend upon abstraction). Fundamental Principles
  • 13. Keep in mind: “When Coding, 80% of your time is spent by reading code”. So take care of formatting and naming ! Others Principles
  • 14. - DRY: Don’t Repeat Yourself. Can Lead to maintenance nightmare - YAGNI: You ain’t gonna need it. Do not add functionality until necessary. - KISS: Keep It Simple, Stupid. Avoid accidental complexity. - Boy Scout Rules: Always leave the campground cleaner than you found it. Others Principles
  • 16. Class Diagram · Upper part contains the name of the class · Middle part describes attributes of the class · Bottom part describes the methods of the class public class Example { public static int CONSTANT= 0; private String attribute1; public String method() { return attribute1; } private static void privateMethod(int c) { CONSTANT+=c; } } UML - Recalls
  • 17. Relations between classes The simplest of relation : · Class A depends on Class B if, at some point, it uses Class B · Symbolized by dotted line and and open arrow indicating the dependency direction public class A { public String method(B b) { return b.toString(); } } public class B { public toString() { return "B"; } } UML - Recalls
  • 18. Association Stronger dependency from class A to class B, namely, A uses and contains an instance of class B: · It is represented by a plain line and an open arrow · This association can have a cardinality 0..1 or 1..n · It is directed , in the example below A knows B, but B does not know anything about A public class A { private B b; public void setB(B b) { this.b = b; } } public class B { public toString() { return "B"; } } UML - Recalls
  • 19. Aggregation Our example models Rockband and members. A rockband can have several members. And Members can be in several Rockand. The addition of the diamond adds information about the life of the objects · The empty diamond signifies that ‘Member’ objects can be shared between several ‘RockBand’ objects · When an ‘RockBand’ object is destroyed, then the instances of ‘Member’ it was associated with do not disappear. UML - Recalls
  • 20. Composition · The plain/full diamond indicates that the contained objects are not shared · When the container object is destroyed, the contained objects disappear with it UML - Recalls class Tattoo { String description; public Tattoo(String description) { this.description = description; } } class Member { private String name; private List<Tattoo> tattoos = new ArrayList<>(); [...] public ink(String description) { tattoos.add(new Tattoo(description)); } } class RockBand { private List<Member> members = new ArrayList<>(); public void addMember(Member m) { members.add(m); } public Member getMember(int num) { return members.get(num); } public void toString() { [..] } }
  • 21. Inheritance / Generalization · Inheritance enables to create classes hierarchies sharing same attributes · Children classes also have the attributes and methods of the parent classes, · These attributes and methods are defined for the objects the children classes. · This mechanism enables Polymorphism Properties · Transitivity: If B inherits from A and C inherits from B then C inherits from A · Not reflexive: No class can inherit from itself · Not symmetric: If B inherits from A, A does not inherit from B · Not cyclic : If B inherits from A and C inherits from B then A can't inherit from C UML - Recalls
  • 22. Inheritance / Generalization - Code public class Animal { private String name; public Animal(String name) { this.name = name; } public String getName() { return this.name; } } public class Cat extends Animal { public Cat(String name) { super(name); // Call to the parent class constructor } public void meow() { System.out.println("Miaou " + super.getName() + " Miaou"); } } public class Dog extends Animal { public Dog(String name) { super(name); // Call to the parent class constructor } public void bark() { System.out.println("Wouf " + super.getName() + " Wouf"); } } UML - Recalls
  • 25. Creational Pattern Creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable for the situation. - Factory Method Pattern - Abstract Factory - Singleton - Builder - Prototype
  • 26. Intent: It allows to create objects without specifying their class (method whose main goal is to create a class instance). Application: - A class can not anticipate the type of objects it must create (i.e.: framework). - A class wants its subclasses to specify the type of objects it creates. - A class needs controls over the creation of its objects. Factory Method Pattern // Problem: A depends on B public class A { public void doIt() { B b = new B() } }
  • 27. Structure: public abstract class Animal() {} public class Dog extend Animal() {} public class Cat extend Animal() {} public class Dolphin extend Animal() {} public interface AnimalFactory { abstract public Animal create(String type); } public AnimalFactory () { public Animal create(String type) { switch(type) { case "Dog": return new Dog(); break; case "Cat": return new Cat(); break; case "Dolphin": return new Dolphin(); break; default : throw Exception("Animal " + type + " is unknown."); } } } public Main() { public static void main(String[] args) { Animalfactory factory = new Animalfactory(); Animal cat = factory.create("cat"); } } Factory Method Pattern
  • 28. Intent: We want to have only one instance of a class. Application: - Instantiate an object can take time and memory. - We only need one instance. // Example: // Problem: For factory, we only need one instance Singleton Pattern
  • 29. Singleton Pattern Structure: The trick is to use a static variable to remember the instance. Then, why not to create a static method ? The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces, so you can pass around the singleton as if it were "just another" implementation. MyClass - static MyClass myClass; static getInstance(): MyClass C public class AnimalFactory { private AnimalFactory(); private static AnimalFactory animalFactory; public static AnimalFactory getInstance() { if (animalFactory == null) { animalFactory = new AnimalFactory(); } return animalFactory; } //…... } public Main() { public static void main(String[] args) { Animal cat = Animalfactory.getInstance().create("cat"); } }
  • 30. Creational Pattern - Factory Method Pattern - Abstract Factory - Singleton - Builder - Prototype (très utilisé dans Javascript)
  • 31. Behavioral Pattern Behavioral patterns are concerned with the assignment of responsibilities between objects, or, encapsulating behavior in an object and delegating requests to it. - Observer - Strategy - State - Visitor - Memento - Mediator - Iterator
  • 32. Intent: Supports a relation One To Many so that several objects get notified when an object changes its state and can react. Application: - Widely use with IHM when several elements of the view change when the model changes. Observer Pattern
  • 33. Roles: subject and several observers - Subject: - Can modify his state. - He notifies observers when he has changed. - Can give his new state - Observers - Can subscribe/unsubscribe to a subject Observer Pattern subscribe
  • 34. public abstract class AbstractSubject { private List<Observer> observers = new ArrayList<Observer>(); public void attach(Observer observer){observers.add(observer);} public void notifyAllObservers(){ for (Observer observer : observers) { observer.update(); } } } public class Subject extends AbstractSubject{ private int state; public int getState() {return state;} public void setState(int state) { this.state = state; notifyAllObservers(); } } public class BinaryObserver extends Observer{ public BinaryObserver(Subject subject){ this.subject = subject; this.subject.attach(this); } @Override public void update() { System.out.println( "Binary String: " + Integer.toBinaryString( subject.getState() ) ); } } public class ObserverPatternDemo { public static void main(String[] args) { AbstractSubject subject = new Subject(); new HexaObserver(subject); new OctalObserver(subject); new BinaryObserver(subject); System.out.println("First state change: 15"); subject.setState(15); System.out.println("Second state change: 10"); subject.setState(10); } } Observer Pattern
  • 35. Observer Pattern Abstract Subject => Classe java.util.Observable - addObserver(Observer obs) - notifyObservers(); Concrete Subject => extends Observable - When state change, you should call notifyObservers(); Abstract Observer: Interface java.util.Observer - update(Observable obs, Object args); Concrete Observer: implements java.util.Observer
  • 36. Behavioral Pattern - Observer - Strategy - State - Visitor - Memento - Mediator - Iterator - Interpreter
  • 37. Intent: It allows to switch between differents algorithms to accomplish a task. Application: - Different variants of an algorithm - Many related classes differ only in their behaviour Strategy Pattern
  • 38. Subject => Interface to outside world Strategy (Algorithm) => common interface for the differents algorithms. Strategy Pattern
  • 39. Program - Input: read file - Output: filtered file Examples of 4 filters: - No filtering - Only words that start with t - Only words longer than 5 characters - Only words that are palindroms Strategy Pattern - Example
  • 40. public class StrategyPattern { public static void main(String[] args) { Context context = new Context(); String filename = args[0]; System.out.println("n* Default(Alll): "); context.filter(filename); System.out.println("n* Start with T: "); context.changeStrategy(new StartWithT()); context.filter(filename); } } public interface CheckStrategy { public boolean check(String s); } public class All implements CheckStrategy { @Override public boolean check(String s) { return true; } } public class StartWithT implements CheckStrategy { @Override public boolean check(String s) { return s != null && s.startWith("t"); } } public class Context { private CheckStrategy strategy; public Context() {this.strategy = new All();} public void changeStrategy(CheckStrategy strategy) { this.strategy = strategy; } public void filter(String filename) throws IOException { BufferedReader infile = new BufferedReader(new FileReader(filename)); String buffer = null; while ((buffer = infile.readLine() != null) { StringTokenizer word = new StringTokenizer(buffer); while (words.hasMoreTokens()) { String word = words.nextToken(); if (strategy.check(word)) { Ssytem.out.println(word); } } } } } Strategy Pattern - Example
  • 41. Behavioral Pattern - Observer - Strategy - State - Visitor - Memento - Mediator - Iterator - Interpreter
  • 42. State Pattern Intent: Implements a state machine in an object- oriented way. It lets an object show other methods after a change of internal state. Application: - an application is characterized by large and numerous case statements. These cases vector flow of control based on the state of the application.
  • 43. interface Statelike { void writeName(StateContext context, String name); } class StateLowerCase implements Statelike { @Override public void writeName(final StateContext context, final String name) { System.out.println(name.toLowerCase()); context.setState(new StateMultipleUpperCase()); } } class StateMultipleUpperCase implements Statelike { /** Counter local to this state */ private int count = 0; @Override public void writeName(final StateContext context, final String name) { System.out.println(name.toUpperCase()); /* Change state after StateMultipleUpperCase's writeName() gets invoked twice */ if (++count > 1) {context.setState(new StateLowerCase());} } } class StateContext { private Statelike myState; StateContext() { setState(new StateLowerCase()); } void setState(final Statelike newState) {myState = newState;} public void writeName(final String name) { myState.writeName(this, name); } } State Pattern public class DemoOfClientState { public static void main(String[] args) { final StateContext sc = new StateContext(); sc.writeName("Monday"); sc.writeName("Tuesday"); sc.writeName("Wednesday"); sc.writeName("Thursday"); sc.writeName("Friday"); sc.writeName("Saturday"); sc.writeName("Sunday"); } }
  • 44. Structural Patterns Design patterns that ease the design by identifying a simple way to realize relationships between entities. - Proxy - Decorator - Facade - Adapter - Aggregate - Bridge
  • 45. Proxy Pattern Translation in French: Proxy <=> ????????? Intent: - A proxy, in its most general form, is a class functioning as an interface to something else. Application: - The proxy could interface to anything: a network connection, a large object in memory, a file. - For sensitive objects: can verify that the caller has the authorization.
  • 46. interface Image { public void displayImage(); } // On System A class RealImage implements Image { private String filename = null; public RealImage(final String filename) { this.filename = filename; loadImageFromDisk(); } private void loadImageFromDisk() {System.out.println("Loading " + filename);} public void displayImage() {System.out.println("Displaying " + filename);} } // On System B class ProxyImage implements Image { private RealImage image = null; private String filename = null; public ProxyImage(final String filename) { this.filename = filename; } public void displayImage() { if (image == null) { image = new RealImage(filename); } image.displayImage(); } } class ProxyExample { public static void main(final String[] arguments) { final Image image1 = new ProxyImage("HiRes_10MB_Photo1"); final Image image2 = new ProxyImage("HiRes_10MB_Photo2"); image1.displayImage(); // loading necessary image1.displayImage(); // loading unnecessary image2.displayImage(); // loading necessary image2.displayImage(); // loading unnecessary image1.displayImage(); // loading unnecessary } } Proxy Pattern - Example
  • 47. - Proxy - Decorator - Facade - Adapter - Aggregate - Bridge Structural Patterns
  • 48. Decorator Pattern Intent: - A wrapper that adds functionality to a class. It is stackable. Application: - The decorator pattern is often useful for adhering to the “Single Responsibility Principle”. - It allows functionality to be divided between classes with unique areas of concern.
  • 49. Decorator Pattern public interface Shape { void draw(); } public class Rectangle implements Shape { @Override public void draw() {System.out.println("Shape: Rectangle");} } public class Circle implements Shape { @Override public void draw() {System.out.println("Shape: Circle");} } public abstract class ShapeDecorator implements Shape { protected Shape decoratedShape; public ShapeDecorator(Shape decoratedShape){ this.decoratedShape = decoratedShape; } public void draw(){decoratedShape.draw();} } public class RedShapeDecorator extends ShapeDecorator { public RedShapeDecorator(Shape decoratedShape) { super(decoratedShape); } @Override public void draw() { decoratedShape.draw(); setRedBorder(decoratedShape); } private void setRedBorder(Shape decoratedShape){ System.out.println("Border Color: Red"); } public class DecoratorPatternDemo { public static void main(String[] args) { Shape circle = new Circle(); Shape redCircle = new RedShapeDecorator(new Circle()); Shape redRectangle = new RedShapeDecorator(new Rectangle()); System.out.println("Circle with normal border"); circle.draw(); System.out.println("nCircle of red border"); redCircle.draw(); System.out.println("nRectangle of red border"); redRectangle.draw(); } }
  • 50. - Proxy - Decorator - Facade - Adapter - Aggregate - Bridge Structural Patterns
  • 51. Intent: - Provide a unified interface to a set of interfaces in a subsystem. - Defines a higher-level interface that makes the subsystem easier to use. Application: - A simple interface is required to access a complex system. - An entry point is needed to each level of layered software. Façade Pattern
  • 52. Façade Pattern public interface Shape { void draw(); } public class Rectangle implements Shape { @Override public void draw() {System.out.println("Rectangle::draw()");} } public class Square implements Shape { @Override public void draw() {System.out.println("Square::draw()");} } public class Circle implements Shape { @Override public void draw() {System.out.println("Circle::draw()");} } public class ShapeMaker { private Shape circle; private Shape rectangle; private Shape square; public ShapeMaker() { circle = new Circle(); rectangle = new Rectangle(); square = new Square(); } public void drawCircle(){circle.draw();} public void drawRectangle(){rectangle.draw();} public void drawSquare(){square.draw();} } public class FacadePatternDemo { public static void main(String[] args) { ShapeMaker shapeMaker = new ShapeMaker(); shapeMaker.drawCircle(); shapeMaker.drawRectangle(); shapeMaker.drawSquare(); } Generic: Example:
  • 53. - Proxy - Decorator - Facade - Adapter - Aggregate - Bridge Structural Patterns
  • 54. Intent: - Allows the interface of an existing class to be used as another interface - Converts one interface to another so that it matches what the client is expecting Application: - Example: an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed. Adapter Pattern (Wrapper)
  • 55. public interface MediaPlayer { public void play(String audioType, String fileName); } public interface AdvancedMediaPlayer { public void playVlc(String fileName); public void playMp4(String fileName); } public class VlcPlayer implements AdvancedMediaPlayer{ @Override public void playVlc(String fileName) {System.out.println("Playing vlc file: "+ fileName);} @Override public void playMp4(String fileName) {// do nothing} } public class Mp4Player implements AdvancedMediaPlayer{ @Override public void playVlc(String fileName) {// do nothing} @Override public void playMp4(String fileName) {System.out.println("Playing mp4 file: "+ fileName);} } Example: Adapter Pattern
  • 56. public class MediaAdapter implements MediaPlayer { AdvancedMediaPlayer advancedMusicPlayer; public MediaAdapter(String audioType) { If (audioType.equalsIgnoreCase("vlc") ) { advancedMusicPlayer = new VlcPlayer(); } else if (audioType.equalsIgnoreCase("mp4")) { advancedMusicPlayer = new Mp4Player(); } } @Override public void play(String audioType, String fileName) { if(audioType.equalsIgnoreCase("vlc")){ advancedMusicPlayer.playVlc(fileName); } else if(audioType.equalsIgnoreCase("mp4")) { advancedMusicPlayer.playMp4(fileName); } } } public class AudioPlayer implements MediaPlayer { MediaAdapter mediaAdapter; @Override public void play(String audioType, String fileName) { // inbuilt support to play mp3 music files if(audioType.equalsIgnoreCase("mp3")) { System.out.println("Playing mp3 file. Name: " + fileName); } else if (audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")) { // mediaAdapter is providing support to play other file formats mediaAdapter = new MediaAdapter(audioType); mediaAdapter.play(audioType, fileName); } else { System.out.println("Invalid media. " + audioType + " format not supported"); } Example: Adapter Pattern public class AdapterPatternDemo { public static void main(String[] args) { AudioPlayer audioPlayer = new AudioPlayer(); audioPlayer.play("mp3", "beyond the horizon.mp3"); audioPlayer.play("mp4", "alone.mp4"); audioPlayer.play("vlc", "far far away.vlc"); audioPlayer.play("avi", "mind me.avi"); } }
  • 57. - Proxy - Decorator - Facade - Adapter - Aggregate - Bridge Structural Patterns
  • 58. - Encapsulate what varies, OCP (Open Close Principle). - 1 class, 1 responsability. - Program against an interface, not an implementation, DIP. - Prefer composition, over inheritance. - Loose coupling, modular black boxes. Golden OO-principles
  • 59. Approach: - Understand your design context - Examine the patterns catalogue - Identify and study related pattern - Apply suitable pattern Pitfalls: - Selectins wrong patterns - Inappropriate use of patterns How to choose a Pattern ?
  • 61. - Wikipedia - tutorialspoint.com - http://www.slideshare.net/mkruthika/software-design-patterns-ppt - http://www.slideshare.net/HermanPeeren/design-patterns-illustrated/ - ... Credits

Editor's Notes

  1. Translation in French: Proxy <=> Mandataire
  2. Translation in French: Proxy <=> Mandataire
  3. Translation in French: Proxy <=> Mandataire
  4. Translation in French: Proxy <=> Mandataire
  5. Translation in French: Proxy <=> Mandataire
  6. Translation in French: Proxy <=> Mandataire
  7. Translation in French: Proxy <=> Mandataire
  8. Translation in French: Proxy <=> Mandataire