際際滷

際際滷Share a Scribd company logo
Design patterns
(Sabloane de proiectare)
Tudor Grigoriu
341C5
Introducere (I)
 Un design pattern este o solutie generala si
reutilizabila a unei probleme comune in design-ul
software
 Nu toate pattern-urile software sunt design
patterns. De exemplu, algoritmii rezolva probleme
computationale, nu probleme de design.
Introducere(II)
 De ce sa folosim Design Patterns?
 Pentru a crea rapid si eficient design-ul unui sistem
software
 Pentru a intelege un sistem existent
Introducere (III)
Introducere (III)
 Categorii:
 Creational patterns
 Singleton
 Factory
 Structural Patterns
 Decorator
 Behavioral Patterns
 Command
 Visitor
 Observer
Creational Patterns
Singleton (I)
Singleton(II)
public class BeerShop {
private static final BeerShop INSTANCE = new Beershop();
// Constructorul private previne instantierea din cadrul altor clase
private BeerShop() {
//...
}
public static BeerShop getInstance() {
return INSTANCE;
}
}
Factory Pattern
Factory Pattern (II)
abstract class Dacia {
public abstract double getPrice();
}
class DaciaMD87 extends Dacia {
public double getPrice() {
return 8.5;
}
}
class DaciaEstafette extends Dacia {
public double getPrice() {
return 10.5;
}
}
class DaciaD33 extends Dacia {
public double getPrice() {
return 11.5;
}
}
Factory Pattern (III)
class DaciaFactory {
public enum DaciaType {
DaciaMD87,
DaciaEstafette,
DaciaD33
}
public static Dacia createDacia(DaciaType daciaType) {
switch (daciaType) {
case DaciaMD87:
return new DaciaMD87();
case DaciaEstafette:
return new DaciaEstafette();
case DaciaD33:
return new DaciaD33();
}
throw new IllegalArgumentException("The car type " + daciaType + " is not recognized.");
}
}
Factory Pattern (IV)
class DaciaLover {
public static void main (String args[]) {
for (DaciaFactory.DaciaType daciaType : DaciaFactory.DaciaType.values()) {
System.out.println("Price of " + daciaType + " is " + DaciaFactory.createDacia(daciaType).getPrice());
}
}
}
Structural Patterns
Decorator
 Permite adaugarea dinamica de noi
comportamente unui obiect deja existent
 E o alternativa pentru folosirea de sub-clase.
Exemplu (I)
 Sa ne imaginam ca ...
Exemplu (II)
interface Window {
public void draw(); // deseneaza fereastra
public String getDescription(); // o descriere a ferestrei
}
// fereastra simpla fara scrollbar
class SimpleWindow implements Window {
public void draw() {
// draw window
}
public String getDescription() {
return "simple window";
}
}
Exemplu (III)
abstract class WindowDecorator implements Window {
protected Window decoratedWindow; // fereastra decorata
public WindowDecorator (Window decoratedWindow) {
this.decoratedWindow = decoratedWindow;
}
public void draw() {
decoratedWindow.draw(); //delegation
}
public String getDescription() {
return decoratedWindow.getDescription(); //delegation
}
}
Exemplu (IV)
class VerticalScrollBarDecorator extends WindowDecorator {
public VerticalScrollBarDecorator (Window decoratedWindow) {
super(decoratedWindow);
}
@Override
public void draw() {
decoratedWindow.draw();
drawVerticalScrollBar();
}
private void drawVerticalScrollBar() {
// draw vertical scrollbar
}
@Override
public String getDescription() {
return decoratedWindow.getDescription() + ", including vertical
scrollbars";
}
}
Exemplu (V)
class HorizontalScrollBarDecorator extends WindowDecorator {
public HorizontalScrollBarDecorator (Window decoratedWindow) {
super(decoratedWindow);
}
@Override
public void draw() {
decoratedWindow.draw();
drawHorizontalScrollBar();
}
private void drawHorizontalScrollBar() {
// draw the horizontal scrollbar
}
@Override
public String getDescription() {
return decoratedWindow.getDescription() + ", including horizontal scrollbars";
}
}
Exemplu (VI)
public class DecoratedWindowTest {
public static void main(String[] args) {
// creaza o fereastra decorata cu scrollbar orizontal/extern
Window decoratedWindow = new HorizontalScrollBarDecorator (
new VerticalScrollBarDecorator(new SimpleWindow()));
System.out.println(decoratedWindow.getDescription());
}
}
Behavioral Patterns
Observer Pattern
 Cand un obiect isi schimba starea, toti dependentii
lui sunt notificati si actualizati automat.
 Implicit exista un obiect subiect, care are o lista
de obiecte dependente.
 Observatorii sunt obiectele ce sunt apelate
automata de fiecare data cand subiectul face o
actiune
Observer Pattern
Concluzii
 De ce sa folosim Design Patterns?
 Pentru a crea rapid si eficient design-ul unui sistem
software
 Pentru a intelege un sistem existent
 Se cere la interviuri :)
 3 categorii:
 Creational patterns: Singleton, Factory, Multiton
 Structural patterns: Decorator, Adaptor, Facade
 Behavioral patterns: Observer, Iterator, Strategy

More Related Content

Grigoriu tudor 341 c5_(tudorgrigoriu@yahoo.com)_designpatterns

  • 1. Design patterns (Sabloane de proiectare) Tudor Grigoriu 341C5
  • 2. Introducere (I) Un design pattern este o solutie generala si reutilizabila a unei probleme comune in design-ul software Nu toate pattern-urile software sunt design patterns. De exemplu, algoritmii rezolva probleme computationale, nu probleme de design.
  • 3. Introducere(II) De ce sa folosim Design Patterns? Pentru a crea rapid si eficient design-ul unui sistem software Pentru a intelege un sistem existent
  • 5. Introducere (III) Categorii: Creational patterns Singleton Factory Structural Patterns Decorator Behavioral Patterns Command Visitor Observer
  • 8. Singleton(II) public class BeerShop { private static final BeerShop INSTANCE = new Beershop(); // Constructorul private previne instantierea din cadrul altor clase private BeerShop() { //... } public static BeerShop getInstance() { return INSTANCE; } }
  • 10. Factory Pattern (II) abstract class Dacia { public abstract double getPrice(); } class DaciaMD87 extends Dacia { public double getPrice() { return 8.5; } } class DaciaEstafette extends Dacia { public double getPrice() { return 10.5; } } class DaciaD33 extends Dacia { public double getPrice() { return 11.5; } }
  • 11. Factory Pattern (III) class DaciaFactory { public enum DaciaType { DaciaMD87, DaciaEstafette, DaciaD33 } public static Dacia createDacia(DaciaType daciaType) { switch (daciaType) { case DaciaMD87: return new DaciaMD87(); case DaciaEstafette: return new DaciaEstafette(); case DaciaD33: return new DaciaD33(); } throw new IllegalArgumentException("The car type " + daciaType + " is not recognized."); } }
  • 12. Factory Pattern (IV) class DaciaLover { public static void main (String args[]) { for (DaciaFactory.DaciaType daciaType : DaciaFactory.DaciaType.values()) { System.out.println("Price of " + daciaType + " is " + DaciaFactory.createDacia(daciaType).getPrice()); } } }
  • 14. Decorator Permite adaugarea dinamica de noi comportamente unui obiect deja existent E o alternativa pentru folosirea de sub-clase.
  • 15. Exemplu (I) Sa ne imaginam ca ...
  • 16. Exemplu (II) interface Window { public void draw(); // deseneaza fereastra public String getDescription(); // o descriere a ferestrei } // fereastra simpla fara scrollbar class SimpleWindow implements Window { public void draw() { // draw window } public String getDescription() { return "simple window"; } }
  • 17. Exemplu (III) abstract class WindowDecorator implements Window { protected Window decoratedWindow; // fereastra decorata public WindowDecorator (Window decoratedWindow) { this.decoratedWindow = decoratedWindow; } public void draw() { decoratedWindow.draw(); //delegation } public String getDescription() { return decoratedWindow.getDescription(); //delegation } }
  • 18. Exemplu (IV) class VerticalScrollBarDecorator extends WindowDecorator { public VerticalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } @Override public void draw() { decoratedWindow.draw(); drawVerticalScrollBar(); } private void drawVerticalScrollBar() { // draw vertical scrollbar } @Override public String getDescription() { return decoratedWindow.getDescription() + ", including vertical scrollbars"; } }
  • 19. Exemplu (V) class HorizontalScrollBarDecorator extends WindowDecorator { public HorizontalScrollBarDecorator (Window decoratedWindow) { super(decoratedWindow); } @Override public void draw() { decoratedWindow.draw(); drawHorizontalScrollBar(); } private void drawHorizontalScrollBar() { // draw the horizontal scrollbar } @Override public String getDescription() { return decoratedWindow.getDescription() + ", including horizontal scrollbars"; } }
  • 20. Exemplu (VI) public class DecoratedWindowTest { public static void main(String[] args) { // creaza o fereastra decorata cu scrollbar orizontal/extern Window decoratedWindow = new HorizontalScrollBarDecorator ( new VerticalScrollBarDecorator(new SimpleWindow())); System.out.println(decoratedWindow.getDescription()); } }
  • 22. Observer Pattern Cand un obiect isi schimba starea, toti dependentii lui sunt notificati si actualizati automat. Implicit exista un obiect subiect, care are o lista de obiecte dependente. Observatorii sunt obiectele ce sunt apelate automata de fiecare data cand subiectul face o actiune
  • 24. Concluzii De ce sa folosim Design Patterns? Pentru a crea rapid si eficient design-ul unui sistem software Pentru a intelege un sistem existent Se cere la interviuri :) 3 categorii: Creational patterns: Singleton, Factory, Multiton Structural patterns: Decorator, Adaptor, Facade Behavioral patterns: Observer, Iterator, Strategy