This document introduces software design patterns, which provide generalizable solutions to common design problems. It discusses three main categories of patterns: creational (Singleton, Factory), structural (Decorator), and behavioral (Command, Visitor, Observer). Examples are provided for the Singleton, Factory, and Decorator patterns. The Factory pattern shows how to dynamically create Dacia car objects based on their type. The Decorator pattern adds functionality like scrollbars to windows without changing the window class. Design patterns help rapidly and efficiently create system designs and understand existing systems.
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
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());
}
}
}
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