Design Patterns Flashcards
(90 cards)
What is a design pattern
A proven solution for a recurring problem
a solution idea, scheme or template
Blackboard
collaborate on common data to get the best solution
have a common data store and let several competing nodes work collaboratively or against each other on that common data the one with the best performance/data wins, will get selected by the client
Scoped resource
make object available in a specifically defined scope, when exiting the scope, the object gets destroyed
create a critical section and requiring resources in that section
client cannot forget to release the resource
e.g. using statement which works as the context manager, or the “with” statement in python
Bridge
- Decoupling abstractions from implementations with adapters
- abstraction interface (client-requirements)
- implementor interface
- Both sides can evolve independently from each other
- client is only allowed to use abstraction interface
- abstraction interface only allowed to use implementor interface, not concrete implementations
- hide implementation side completely from the client
- improves extensibility, both sides can grow
- hiding implementation details from the client
Early aqcuisition
make resources available as early as possible
Eager Aqcuisition
make resource available as early as possible when the context is opened (e.g. a window)
[E] what do scoped lock, scoped resource and scope.. have in common?
all of them use common mechanism to automatically release the scoped resource in the end
[E] What is the difference between lazy and eager loading?
Lazy = load resource as late as possible (on access)
Eager = load resource when needed but also rather early than late
Flyweight
Store away similar values to only have one instance of them
You have only one instance for similar/same attributes
Pooling
You store multiple objects or reserve a bigger block of memory to be reused over and over again
until pool is used up
when more objects are needed, client has to wait until objects are freed again
store the results temporarily to be used again
[E] How does Caching differ from Pooling?
In Pooling: you reuse the already allocated memory instead of freeing/destroying. Only one client should use a resource at a time (exclusive access). Can be predefined or builds up over time.
In Caching: always builds up over time, non exclusive access
both have upper limits
[E] What is better for accessing a Database: Active Object or Active Record? Explain why!
Active record, contains CRUD operations
Active object is not at all about the database
Active Object
asynchronious thread pool
execute the command in a different thread than it was created initially
contains tasks the execution about commands that get executed in other threads
you have to encapsulate the command with its context
What is the purpose of design patterns
Easier knowledge transfer
efficient problem solving by reusing existing ideas
common vocabulary, terminology, or language
usefulness of an idea by generalizing the solution
What types of design patterns are there
Architechtural (fundamental structural patterns, Layers, Pipes-And-Filters, Broker, MVC)
Idioms (fine graned patterns for spec. contexts, Composite, Adapter, Proxy)
Design Patterns (more isolated problems, Counted Pointer, Scoped Locking)
What types of design patterns are there
Architechtural (fundamental structural patterns)
Idioms (fine graned patterns for spec. contexts)
Design Patterns (more isolated problems)
Pattern Format
- *Name**: A catchy name for the pattern
- *Context**: The situation where the problem occurs
- *Problem**: General Problem Description
- *Forces**: Requirements and Constraints - why does problem hurt?
- *Solution**: Generic Description of a proven solution.
- *Consequences** (Rationale, Resulting Context): What are the benefits and drawbacks? Pro and Contra?
- *Known-Uses**: Real Life Examples
How Design Patterns emerge?
Design Patterns are found - not invented!
They emerge out of real use-cases/known-uses
Pattern Languages
coherent systems of patterns
consisting of Patterns, Relations and Principles
SOLID
• Single Responsibility: A class should have one, and
only one, reason to change.
• Open Closed: You should be able to extend a class’s
behavior, without modifying it.
• Liskov Substitution: Derived classes must be
substitutable for their base classes.
• Interface Segregation: Make fine grained
interfaces that are client specific.
• Dependency Inversion: Depend on abstractions,
not on concrete implementations.
Principles of Good Programming
- Decomposition make a problem manageable decompose it into sub-problems
- Abstraction wrap around a problem abstract away the details
- Decoupling reduce dependencies, late binding shift binding time to “later”
- Usability & Simplicity make things easy to use right, hard to use wrong adhere to expectations, make usage intuitive
Decorator
Extend the functionality of an object, while maintaining the same interface.
add responsibilities to individual objects without affecting other
objects
reuse funcionality
disadvantages: hard to learn and debug
Proxy
Provide a placeholder for another object to control it.
Maintain a reference that lets the proxy access
the real subject and provide interface identical
to Subject
Control access to the real subject
Layers
Split your large system into layers based on abstraction levels
Problem: Hard to understand structure, many dependencies
Every layer uses defined services of sublayer
Every layer provides defined services to upper layer
Defined Interfaces between Layers
Dependencies/Changes are kept local
e.g.: Network stack, API’s, Operating Systems