ASD2 Samenvatting
Design Patterns........................................................................................................................... 2
Factory Method (Creational).................................................................................................... 2
Abstract Factory (Creational)...................................................................................................3
Builder (Creational)..................................................................................................................4
Zonder director...................................................................................................................4
Met director........................................................................................................................ 4
Singleton..................................................................................................................................6
Eager loading (Multithreading)...........................................................................................6
Lazy Loading (Multithreading)............................................................................................6
Enum Singleton (Multithread safe).....................................................................................6
Iterator & Composite (Behavioral)........................................................................................... 7
Command (Behavioral)............................................................................................................8
Template Method (Behavioral).................................................................................................9
Adapter (Structural)............................................................................................................... 10
Proxy (Structural)................................................................................................................... 11
Protection proxy............................................................................................................... 11
Remote proxy...................................................................................................................11
Virtual proxy..................................................................................................................... 11
Multithreading............................................................................................................................ 12
Levenscyclus......................................................................................................................... 12
Priorities & Scheduling...........................................................................................................12
ThreadLocalRandom............................................................................................................. 13
Creatie & Executie van threads............................................................................................. 13
.run()................................................................................................................................ 13
Aanmaken........................................................................................................................13
Starten............................................................................................................................. 13
Threadpool Maken........................................................................................................... 13
Thread Synchronisatie...........................................................................................................13
Locks................................................................................................................................13
Producer/consumer relatie (zonder synchronisatie).............................................................. 14
Producer/consumer relatie (met synchronisatie)................................................................... 15
Methodes van klasse Object............................................................................................15
Praktische uitvoering: locks, conditions, await() en signal().............................................15
Set methode.....................................................................................................................16
Get methode.................................................................................................................... 17
Product/consumer relatie: ArrayBlockingQueue....................................................................17
Interface Callable...................................................................................................................18
Stop, suspend & resume threads.......................................................................................... 18
,Design Patterns
Factory Method (Creational)
PizzaStore (abstracte klasse) als Creator.
GhentPizzaStore en BrusselsPizzaStore als Concrete Creator
Beide concrete creators implementeren de abstracte methode createPizza en geven daarbij de
pizza die tot hen behoort. Deze wordt in dit voorbeeld onderling opgeroepen in orderPizza.
Pizza (product) kan hier een abstracte of interface klasse zijn.
Voorbeeld code:
GhentPizzaStore ghentPizzaStore = new GhentPizzaStore();
ghentPizzaStore.orderPizza("pepperoni");
, Abstract Factory (Creational)
De statische methode getFactory zorgt er voor dat we de juiste AbstractFactory terug krijgen.
Eens dat we de juiste factory hebben, kunnen we voort met hun getShape methodes.
Voorbeeld code:
// Aanmaak rounded figuren
AbstractFactory roundedFactory = FactorySupplier.getFactory("rounded");
Shape shape1 = roundedFactory.getShape("RECTANGLE");
shape1.draw();
Shape shape2 = roundedFactory.getShape("SQUARE");
shape2.draw();
// Aanmaak smooth figuren
AbstractFactory smoothFactory = FactorySupplier.getFactory("smooth");
Shape shape3 = smoothFactory.getShape("RECTANGLE");
shape3.draw();
Shape shape4 = smoothFactory.getShape("SQUARE");
shape4.draw();
Design Patterns........................................................................................................................... 2
Factory Method (Creational).................................................................................................... 2
Abstract Factory (Creational)...................................................................................................3
Builder (Creational)..................................................................................................................4
Zonder director...................................................................................................................4
Met director........................................................................................................................ 4
Singleton..................................................................................................................................6
Eager loading (Multithreading)...........................................................................................6
Lazy Loading (Multithreading)............................................................................................6
Enum Singleton (Multithread safe).....................................................................................6
Iterator & Composite (Behavioral)........................................................................................... 7
Command (Behavioral)............................................................................................................8
Template Method (Behavioral).................................................................................................9
Adapter (Structural)............................................................................................................... 10
Proxy (Structural)................................................................................................................... 11
Protection proxy............................................................................................................... 11
Remote proxy...................................................................................................................11
Virtual proxy..................................................................................................................... 11
Multithreading............................................................................................................................ 12
Levenscyclus......................................................................................................................... 12
Priorities & Scheduling...........................................................................................................12
ThreadLocalRandom............................................................................................................. 13
Creatie & Executie van threads............................................................................................. 13
.run()................................................................................................................................ 13
Aanmaken........................................................................................................................13
Starten............................................................................................................................. 13
Threadpool Maken........................................................................................................... 13
Thread Synchronisatie...........................................................................................................13
Locks................................................................................................................................13
Producer/consumer relatie (zonder synchronisatie).............................................................. 14
Producer/consumer relatie (met synchronisatie)................................................................... 15
Methodes van klasse Object............................................................................................15
Praktische uitvoering: locks, conditions, await() en signal().............................................15
Set methode.....................................................................................................................16
Get methode.................................................................................................................... 17
Product/consumer relatie: ArrayBlockingQueue....................................................................17
Interface Callable...................................................................................................................18
Stop, suspend & resume threads.......................................................................................... 18
,Design Patterns
Factory Method (Creational)
PizzaStore (abstracte klasse) als Creator.
GhentPizzaStore en BrusselsPizzaStore als Concrete Creator
Beide concrete creators implementeren de abstracte methode createPizza en geven daarbij de
pizza die tot hen behoort. Deze wordt in dit voorbeeld onderling opgeroepen in orderPizza.
Pizza (product) kan hier een abstracte of interface klasse zijn.
Voorbeeld code:
GhentPizzaStore ghentPizzaStore = new GhentPizzaStore();
ghentPizzaStore.orderPizza("pepperoni");
, Abstract Factory (Creational)
De statische methode getFactory zorgt er voor dat we de juiste AbstractFactory terug krijgen.
Eens dat we de juiste factory hebben, kunnen we voort met hun getShape methodes.
Voorbeeld code:
// Aanmaak rounded figuren
AbstractFactory roundedFactory = FactorySupplier.getFactory("rounded");
Shape shape1 = roundedFactory.getShape("RECTANGLE");
shape1.draw();
Shape shape2 = roundedFactory.getShape("SQUARE");
shape2.draw();
// Aanmaak smooth figuren
AbstractFactory smoothFactory = FactorySupplier.getFactory("smooth");
Shape shape3 = smoothFactory.getShape("RECTANGLE");
shape3.draw();
Shape shape4 = smoothFactory.getShape("SQUARE");
shape4.draw();