Lecture 10 - Design Patterns Flashcards

(24 cards)

1
Q

What are software design patterns?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the characteristics of design patterns?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why use design patterns (the advantages)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the elements of design patterns?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 3 pattern taxonomies?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are structural patterns?

A

Structural patterns manage interactions with a subsystem.
They reduce coupling between subsystems.
It uses a Facade, and Adapater or a Proxy

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do structural patterns work?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the facade pattern?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is an open architecture?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a closed architecture

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the Adapter pattern?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Adapter Terminology: Client

A

the class that wants to use the third-party library or the external system

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Adapter Terminology: Adaptee

A

a class in the third-party library or the external system that we want to use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Adapter Terminology: Target Interface

A

the desired interface that the client will use

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Adapter Terminology: Adapter

A

this class sits between the client and the adaptee and implements the target interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Comparing Facade to Adapter

A

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

17
Q

What is a proxy pattern?

A

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.

18
Q

Understanding the proxy patterns

A

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

19
Q

What are the 3 types of proxy patterns?

A

Remote Proxy
Virtual Proxy
Protection Proxy

20
Q

What is Remote Proxy?

A

Local representative for an object in a different address space
Caching of information: Good if information does not change too often

21
Q

What is Virtual Proxy?

A

Object is too expensive to create or too expensive to download
Proxy is a stand-in

22
Q

What is Protection Proxy?

A

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.

23
Q

What is an observer pattern?

A

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

24
Q

How does the observer pattern work?

A

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.