SlideShare a Scribd company logo
1 of 24
Download to read offline
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

Viewers also liked

7 самых мощных инструментов продаж
7 самых мощных инструментов продаж7 самых мощных инструментов продаж
7 самых мощных инструментов продажkutolinadasha
 
341 c5 tudorgrigoriu_soa-principii_de_baza
341 c5 tudorgrigoriu_soa-principii_de_baza341 c5 tudorgrigoriu_soa-principii_de_baza
341 c5 tudorgrigoriu_soa-principii_de_bazaTudor Grigoriu
 
ELCA Churchwide Representative Report 2014 - Rev. Ruben Duran - Greater Milw...
ELCA Churchwide Representative Report 2014  - Rev. Ruben Duran - Greater Milw...ELCA Churchwide Representative Report 2014  - Rev. Ruben Duran - Greater Milw...
ELCA Churchwide Representative Report 2014 - Rev. Ruben Duran - Greater Milw...gmselca
 
La production des comprimés et dragées aux usa après la seconde guerre mo...
La production des comprimés et dragées aux usa  après la seconde guerre mo...La production des comprimés et dragées aux usa  après la seconde guerre mo...
La production des comprimés et dragées aux usa après la seconde guerre mo...Andre Frogerais
 
Разработка PR технологии
Разработка PR технологииРазработка PR технологии
Разработка PR технологииkutolinadasha
 
Mobile phones using graphene
Mobile phones using grapheneMobile phones using graphene
Mobile phones using grapheneNehhal Pota
 
Snickers. История бренда.
Snickers. История бренда.Snickers. История бренда.
Snickers. История бренда.kutolinadasha
 
Примеры успешного маркетинга
Примеры успешного маркетингаПримеры успешного маркетинга
Примеры успешного маркетингаkutolinadasha
 

Viewers also liked (9)

7 самых мощных инструментов продаж
7 самых мощных инструментов продаж7 самых мощных инструментов продаж
7 самых мощных инструментов продаж
 
Mustang Jeans
Mustang JeansMustang Jeans
Mustang Jeans
 
341 c5 tudorgrigoriu_soa-principii_de_baza
341 c5 tudorgrigoriu_soa-principii_de_baza341 c5 tudorgrigoriu_soa-principii_de_baza
341 c5 tudorgrigoriu_soa-principii_de_baza
 
ELCA Churchwide Representative Report 2014 - Rev. Ruben Duran - Greater Milw...
ELCA Churchwide Representative Report 2014  - Rev. Ruben Duran - Greater Milw...ELCA Churchwide Representative Report 2014  - Rev. Ruben Duran - Greater Milw...
ELCA Churchwide Representative Report 2014 - Rev. Ruben Duran - Greater Milw...
 
La production des comprimés et dragées aux usa après la seconde guerre mo...
La production des comprimés et dragées aux usa  après la seconde guerre mo...La production des comprimés et dragées aux usa  après la seconde guerre mo...
La production des comprimés et dragées aux usa après la seconde guerre mo...
 
Разработка PR технологии
Разработка PR технологииРазработка PR технологии
Разработка PR технологии
 
Mobile phones using graphene
Mobile phones using grapheneMobile phones using graphene
Mobile phones using graphene
 
Snickers. История бренда.
Snickers. История бренда.Snickers. История бренда.
Snickers. История бренда.
 
Примеры успешного маркетинга
Примеры успешного маркетингаПримеры успешного маркетинга
Примеры успешного маркетинга
 

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

quelques-patterns- patrons de conception
quelques-patterns- patrons de conceptionquelques-patterns- patrons de conception
quelques-patterns- patrons de conceptionEveryThing68
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstractionIntro C# Book
 
Design Patterns
Design PatternsDesign Patterns
Design Patternsadil raja
 
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfJEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfMarlouFelixIIICunana
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfrajaratna4
 
P/Invoke - Interoperability of C++ and C#
P/Invoke - Interoperability of C++ and C#P/Invoke - Interoperability of C++ and C#
P/Invoke - Interoperability of C++ and C#Rainer Stropek
 
Making Steaks from Sacred Cows
Making Steaks from Sacred CowsMaking Steaks from Sacred Cows
Making Steaks from Sacred CowsKevlin Henney
 
Building modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf FildebrandtBuilding modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf Fildebrandtmfrancis
 
Design patters java_meetup_slideshare [compatibility mode]
Design patters java_meetup_slideshare [compatibility mode]Design patters java_meetup_slideshare [compatibility mode]
Design patters java_meetup_slideshare [compatibility mode]Dimitris Dranidis
 
Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...
Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...
Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...DicodingEvent
 
Object Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsObject Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsDudy Ali
 

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

quelques-patterns- patrons de conception
quelques-patterns- patrons de conceptionquelques-patterns- patrons de conception
quelques-patterns- patrons de conception
 
20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction20.4 Java interfaces and abstraction
20.4 Java interfaces and abstraction
 
Bridge Pattern
Bridge PatternBridge Pattern
Bridge Pattern
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
VR Workshop #2
VR Workshop #2VR Workshop #2
VR Workshop #2
 
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdfJEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
JEDI Slides-Intro2-Chapter20-GUI Event Handling.pdf
 
Bridge Pattern
Bridge PatternBridge Pattern
Bridge Pattern
 
Example for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdfExample for Abstract Class and Interface.pdf
Example for Abstract Class and Interface.pdf
 
P/Invoke - Interoperability of C++ and C#
P/Invoke - Interoperability of C++ and C#P/Invoke - Interoperability of C++ and C#
P/Invoke - Interoperability of C++ and C#
 
Making Steaks from Sacred Cows
Making Steaks from Sacred CowsMaking Steaks from Sacred Cows
Making Steaks from Sacred Cows
 
Building modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf FildebrandtBuilding modular software with OSGi - Ulf Fildebrandt
Building modular software with OSGi - Ulf Fildebrandt
 
My way to clean android V2
My way to clean android V2My way to clean android V2
My way to clean android V2
 
Devoxx 2012 (v2)
Devoxx 2012 (v2)Devoxx 2012 (v2)
Devoxx 2012 (v2)
 
Design patters java_meetup_slideshare [compatibility mode]
Design patters java_meetup_slideshare [compatibility mode]Design patters java_meetup_slideshare [compatibility mode]
Design patters java_meetup_slideshare [compatibility mode]
 
Modules or microservices?
Modules or microservices?Modules or microservices?
Modules or microservices?
 
Test Engine
Test EngineTest Engine
Test Engine
 
7 inheritance
7 inheritance7 inheritance
7 inheritance
 
Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...
Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...
Dicoding Developer Coaching #30: Android | Mengenal Macam-Macam Software Desi...
 
Object Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & DestructorsObject Oriented Programming - Constructors & Destructors
Object Oriented Programming - Constructors & Destructors
 
L07 Frameworks
L07 FrameworksL07 Frameworks
L07 Frameworks
 

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