SlideShare ist ein Scribd-Unternehmen logo
1 von 61
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

Weitere ähnliche Inhalte

Was ist angesagt?

Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
 
Creational pattern
Creational patternCreational pattern
Creational patternHimanshu
 
Design Patterns
Design PatternsDesign Patterns
Design Patternssoms_1
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternMudasir Qazi
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General IntroductionAsma CHERIF
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton PatternMudasir Qazi
 
Prototype design patterns
Prototype design patternsPrototype design patterns
Prototype design patternsThaichor Seng
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design PatternsAnton Keks
 

Was ist angesagt? (20)

Builder pattern
Builder patternBuilder pattern
Builder pattern
 
Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Creational pattern
Creational patternCreational pattern
Creational pattern
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
Decorator Pattern
Decorator PatternDecorator Pattern
Decorator Pattern
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design Pattern - Factory Method Pattern
Design Pattern - Factory Method PatternDesign Pattern - Factory Method Pattern
Design Pattern - Factory Method Pattern
 
Design patterns tutorials
Design patterns tutorialsDesign patterns tutorials
Design patterns tutorials
 
Design Patterns - General Introduction
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
Prototype design patterns
Prototype design patternsPrototype design patterns
Prototype design patterns
 
Proxy Design Pattern
Proxy Design PatternProxy Design Pattern
Proxy Design Pattern
 
Factory Method Pattern
Factory Method PatternFactory Method Pattern
Factory Method Pattern
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Composite pattern
Composite patternComposite pattern
Composite pattern
 
Java Course 11: Design Patterns
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 

Andere mochten auch

Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Row Pattern Matching in SQL:2016
Row Pattern Matching in SQL:2016Markus Winand
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design PatternSanae BEKKAR
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns pptmkruthika
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns IllustratedHerman Peeren
 
Pagination Done the Right Way
Pagination Done the Right WayPagination Done the Right Way
Pagination Done the Right WayMarkus 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 patternJoao Pereira
 
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 DatabasesMarkus Winand
 
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 PatternMichael Heron
 
Factory design pattern
Factory design patternFactory design pattern
Factory design patternFarhad Safarov
 
Design Pattern - Introduction
Design Pattern - IntroductionDesign Pattern - Introduction
Design Pattern - IntroductionMudasir Qazi
 
Design Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternDesign Patterns - Abstract Factory Pattern
Design Patterns - Abstract Factory PatternMudasir Qazi
 
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 angaSotonye anga
 
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 PatternsAndy Maleh
 
Design Pattern lecture 2
Design Pattern lecture 2Design Pattern lecture 2
Design Pattern lecture 2Julie Iskander
 
Design Pattern Craftsmanship
Design Pattern CraftsmanshipDesign Pattern Craftsmanship
Design Pattern CraftsmanshipJason Beaird
 

Andere mochten auch (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
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
 
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
 
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
 
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
 
Design Pattern Craftsmanship
Design Pattern CraftsmanshipDesign Pattern Craftsmanship
Design Pattern Craftsmanship
 

Ähnlich wie Design pattern

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed 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-argumentSuresh 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 LevelRamrao Desai
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
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
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 
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 DiagramsBhathiya Nuwan
 
Binding,interface,abstarct class
Binding,interface,abstarct classBinding,interface,abstarct class
Binding,interface,abstarct classvishal choudhary
 
2CPP14 - Abstraction
2CPP14 - Abstraction2CPP14 - Abstraction
2CPP14 - AbstractionMichael Heron
 

Ähnlich wie 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
 

Kürzlich hochgeladen

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 

Kürzlich hochgeladen (20)

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 

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

Hinweis der Redaktion

  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