Lecture 10 - Design Patterns Flashcards
(24 cards)
What are software design patterns?
- A design pattern describes a problem which occurs over and over again
- A design pattern describes the core of the solution to that problem, in such a way that you can reuse the solution in many situations
What are the characteristics of design patterns?
Smart – an elegant solution not obvious to a novice
Generic – not dependent upon a system, programming language or application domain
Well-Proven – has been identified from real OO systems
Simple – is usually quite small, involving only a handful of classes
Reusable – is documented in such a fashion that it is easy to reuse
Why use design patterns (the advantages)?
Reuse: Once a design pattern has been verified, it can be used in any number of ways in a given context.
Common Vocabulary: Design patterns give software designers a common vocabulary that concisely encapsulates solutions to design problems.
Easy to modify: Designs patterns are easy to modify to apply to a particular problem. The solutions can also be modified to give flexibility with minimal risk.
What are the elements of design patterns?
a Pattern Name – a handle we can use to describe a design problem, its solutions and consequences
the Problem – describes when to apply the pattern. It explains the problem and its context
the Solution – describes the elements which make up the solution and their relationships
the Consequences – the results and trade-offs of using the design pattern
What are the 3 pattern taxonomies?
Architectural Patterns: Model-View-Controller
- Architectural patterns have broader scope than other Design Patterns
Structural Patterns: Adapters, Bridges, Facades and Proxies all:
- Reduce the coupling between two or more classes
- Encapsulate complex structures
Behavioural Patterns
- Concerned with algorithms and assignment of responsibilities between objects: Who does what?
- Characterize complex control flow that is difficult to follow at runtime.
What are structural patterns?
Structural patterns manage interactions with a subsystem.
They reduce coupling between subsystems.
It uses a Facade, and Adapater or a Proxy
How do structural patterns work?
A subsystem consists of
- an interface object
- a set of application domain objects (entity objects) modeling real entities or existing systems
- one or more control objects
Realization of Interface Object: Facade
- Provides the interface to the subsystem
Interface to Existing systems: Adapter
- Provides the interface to existing system (legacy system)
- The existing system is not necessarily object-oriented!
Defer object creation or initialisation: Proxy
What is the facade pattern?
Provides a unified interface to a set of objects in a subsystem.
A facade defines a higher-level interface that makes the subsystem easier to use
Facades allow us to provide a closed architecture
What is an open architecture?
Open Architecture:
The internal components and structure of a system are fully accessible to external clients or developers
—
Why is this good?
Efficiency
—
Why is this bad?
Can’t expect to understand how the subsystem works or the complex relationships within the subsystem.
The subsystem may be misused, leading to non-portable code
What is a closed architecture
Closed Architecture:
Internal components of the subsystem are hidden or encapsulated.
Clients interact with the subsystem only through a well-defined interface or API.
Direct access to internal classes, data, or operations is restricted.
—
Why is this good?
Encapsulation improves maintainability and modularity.
Reduces misuse by hiding internal complexity.
Increases portability and reusability since clients are decoupled from internal implementation.
—
Why might it seem bad?
Can introduce additional abstraction layers, which might reduce performance in some cases.
Less flexible if a client wants to perform advanced or non-standard operations.
What is the Adapter pattern?
Adapter is a special object that converts the interface of one object so that another object can understand it.
- An adapter wraps one of the objects to hide the complexity of conversion happening behind the scenes. The wrapped object isn’t even aware of the adapter.
- Adapters can not only convert data into various formats but can also help objects with different interfaces collaborate.
Adapter Terminology: Client
the class that wants to use the third-party library or the external system
Adapter Terminology: Adaptee
a class in the third-party library or the external system that we want to use
Adapter Terminology: Target Interface
the desired interface that the client will use
Adapter Terminology: Adapter
this class sits between the client and the adaptee and implements the target interface
Comparing Facade to Adapter
Facade defines a new interface for existing objects
Adapter tries to make an existing interface usable
Adapter usually wraps just one object
Facade works with an entire subsystem of objects
What is a proxy pattern?
Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.
Understanding the proxy patterns
What is expensive?
- Object Creation
- Object Initialization
–
Defer object creation and object initialization to the time that you need the object
–
Proxy pattern:
- Uses another object (“the proxy”) that acts as a stand-in for the real object
- Reduces the cost of accessing objects
- The proxy creates the real object only if the user asks for it
What are the 3 types of proxy patterns?
Remote Proxy
Virtual Proxy
Protection Proxy
What is Remote Proxy?
Local representative for an object in a different address space
Caching of information: Good if information does not change too often
What is Virtual Proxy?
Object is too expensive to create or too expensive to download
Proxy is a stand-in
What is Protection Proxy?
Proxy provides access control to the real object
Useful when different objects should have different access and viewing rights for the same document.
Example: Grade information for a student shared by administrators, teachers and students.
What is an observer pattern?
Defines a one to many dependency where changes to one object notify all its dependents automatically
Observer solutions is also called Publish and Subscribe
The Observer pattern:
- Maintains consistency across redundant state
- optimizes batch changes to maintain consistency
REFER TO SLIDES FOR EXAMPLE
How does the observer pattern work?
Publisher will notify others about changes to its state
Subscribers are objects that want to track changes to the publisher’s state
—
Observer pattern adds a subscription mechanism to the publisher class comprising:
+ An array to store a list of references to subscriber objects
+ Public methods to add and remove subscribers from the list
—
Whenever an important event happens to the publisher, it loops through its subscribers and calls the specific notification method on their objects.