Design Patterns Flashcards
Advantages of Object Oriented Programming
Each Object easier to implement, maintain and reuse
- divide and conquer
Flexible combinations possible
- Versatility
Disadvantages of Object Oriented Programming
Behavior distributed across multiple objects
- lack of clarity (Unübersichtlichkeit)
Any change of one object my affect many others
- interdependence
Design Patterns - Observer Pattern
Communication without coupling
How and Why?
Decouple data model from parties/ observers that are interested in changes of its internal state
- Subject shouldnt know details of observers
- identity and number of observers may vary
- polling is inappropriate and inefficient (nachfrage obs ein update im subject gibt)
Advantages of the Observer Pattern
- abstract coupling between subject and observer
- support for broadcast communication
Disadvantages of the Observer Pattern
- update cascades
- update for all observers even though they may not be interested
- missing details about change
- limited communication interface -> subject cannot send optional parameters
Observer Pattern - Implementation
who should inform observer of state change?
Method that changes the state notifies the observers
-> direct notification after change of state
Client triggerst update
-> if several changes performed at once only one notification needed
Design Patterns - Factory Method
Participants
Product
- interface of objects created by factoryMethod()
ConcreteProduct
- implements product interface
Creator
- declares factory method that returns object of product
ConcreteCreator
- overrides factoryMethod() to return instance of conreteProduct
Factory Methods - Consequences and Variants
- client code only knows product interface -> works with any specific concreteproduct class
- provides hook for subclasses
Variants
- creator abstract
- creator is concrete and provides default implementation
Design Patterns - Abstract Factory
Motivation
For complete abstraction of the database
- clients only needs to interact with interface
avoid knowing the concrete type at creation time
Abstract Factory - Consequences
+ abstracts away from concrete products
+ changing product families is easy
+ ensures consistency among products
- adding unforeseen kinds of products difficult
- client needs to know hot to use factory (interface) instead of constructor