Design Patterns
3 Creational Patterns beschreiben.
• Singleton – Provision of a single instance only • Factory – Method in a derived class creates associates • Prototype – Factory for cloning new instances from a prototypical instance
○ Singleton
Provision of a single instance only
Issue
Only one object instance should exist:
- Database access
- Id generator
- Logger
- Communication with hardware
Solution
Example
public class Person { private final Logger LOG = LoggerFactory.getLogger(Person.class); Logger getLogger() { return LOG; } }
In this case
LOG
is the same object even without static due to the way the LoggerFactory works.
○ Factory
Method in a derived class creates associates
Issue
Object creation & configuration is complex
Solution
Example
interface Product { float getPrice(); }public class Milk implements Product { final float price; public Milk(final float price) { this.price = price; } //constructor public float getPrice() { return price; } //getter }public class Sugar implements Product { final float price; public Sugar(final float price) { this.price = price; } //constructor public float getPrice () { return price; } //getter }public class Product Factory { public static Product createProduct(String what){ //When sugar is requested, we return sugar: if (what.equals("Sugar")) { return new Sugar(1.49F) ; } // When milk is needed, we return milk: else if (what.equals("Milk")) { return new Milk(0.99F); } // Otherwise return at least sugar else { return new Sugar(1.49F); } } }
○ Abstract Factory
Factory for building related objects without specifying their concrete classes
Issue
Achieving higher abstraction by grouping individual factories with a common theme
Solution
A group of individual factories that have a common theme
Two hierarchies → abstract AbstractFactory class provides interface
Client only knows abstract interface → Family may grow independently of the client
4 Structural Patterns beschreiben.
• Facade – Facade simplifies the interface for a subsystem • Adapter – Translator adapts a server interface for a client • Proxy – One object approximates another • Bridge – Abstraction for binding one of many implementations
○ Facade / Facet
Simplifies the interface for a subsystem
Issue
Need simplified access to a complex subsystem
Provides an abstracted interface of a subsystem
Solution
Example
public class SimpleMail { public static int sendMail(String address, String subject, String body) { int status = 0; ... //Complex mail sending operation return status; } }
○ Adapter / Translator
Adapts a server interface for a client
Issue
Need to integrate incompatible external functionality
Solution
Translates (data transformation) into a compatible interface
○ Proxy
One object approximates another
Issue
Need to integrate further actions before intended method call
Solution
Extends concept of the delegation pattern
Implements interface and acts as a representative of the „original“ implementation
Used in: security, logging, caching
○ Inversion of Control IoC
Die Verantwortung für das Erzeugen und Initialisieren von Objekten wird an eine zentrale Stelle (z.B. eine Klasse) delegiert.
Von der zentralen Stelle kann man die Abhängigkeiten zwischen den Objekten leichter überblicken und steuern.
Beschreiben Sie das Data Access Object Pattern (DAO) anhand eines UML-Diagramms und erläutern Sie dessen Funktionsweise. Wann kommt dieses Pattern typischerweise zum Einsatz?
Einsatz:
Trennung von Interface/Implementation (mehrere Implementationen möglich
Dependency Injection Systeme (Spring)
Geschäftsmethoden aus der Business Logic layer enthalten keine direkte Interaktion mit Persistenztechnologien wie zB relationalen Datenbanken.
Um Daten (Domänenobjekte) zu persistieren, definiert man Data Access Object DAO Interfaces (mit CRUD befehlen - create, reade, update, delete).
• Create (insert..()) • Read (get..(), search..()) • Update (update..(),save..()) • Delete(delete..())
Das SQLPersonDAO-Objekt ist eine mögliche Implementierung des PersonDAO-Interfaces.
Wie sieht eine moderene 3-Tier-Architektur aus? Beschreiben Sie die folgenden Fälle: a) Thin-Client b) Fat-Client. Beschreiben und skizzieren Sie die beiden Architekturvarianten.
Moderne 3-Tier-Architektur
- Präsentationsschicht
- Anwendungs-/Logikschicht
- Datenschicht
Thin-Client („Heavy Server“ vs. Server per Layer)
Fat-Client (Data-Server)