Design patterns Flashcards

1
Q

Cohesion

A

A class should represent a single concept.
A class is cohesive if all of its methods and attributes only relate to the concept it represents.
High cohesion is good.

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

Coupling

A

If many classes have relationships to each other, the coupling is high.
Low coupling is preferred.

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

What is a design pattern?

A

A way to structure some part of the application’s code. A general approach to common design problems.
Techniques that help reduce coupling and increase cohesion.

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

Types of design problems

A

Creational. Focus on object creation.
Structural. How to organize different classes and objects.
Behavioral. Communication patterns between objects.

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

Singleton pattern.

A

Guarantees only 1 instance can exist in a system and provides global access to the
instance from anywhere in the system.

Make constructor private. Private static attribute stores object. Static method returns object.

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

Factory design pattern.

A

Abstracts object creation. You give it a string (or enum) to specify the type you want it to return. I.e. A complex inheritance structure of a bunch of animals.

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

Strategy design pattern.

A

Turn behaviors into classes that implement interfaces. Class that uses these behaviors have instances of these interfaces.
This also allows the behaviors to change at run time if desired.

Define a family of algorithms.
Encapsulate each algorithm.
This makes them interchangeable.

Basic principle: Favor composition over inheritance.

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

Command pattern.

A

Command pattern makes objects out of what needs to be done. Strategy pattern, in comparison, makes an object out of how something should be done.

Basically makes a noun out of a verb.

Hide the action behind a common interface (i.e. Command interface with execute(): void method).

Can then pass around commands, store requests (things to do) in data structures.

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

Observer Design Pattern

A

Observer pattern: One-to-many dependency between objects. When one object changes state, all dependents are notified and updated.

3 elemenents:
Subject or Observable.
Observer
Client/driver program.

Subject has an aggregate of observers, and makes calls to observer.
Client calls both subject and observer.

Subject and observer shouldn’t know everything about each other. Subject just needs to notify that the data has changes, but doesn’t need details of who it is notifying.
I.e. A magazine company telling a subscriber that a new edition is out.

To help this, use interfaces so that if A is subject, and B is observer, A has an interface B implements, so that A is only loosely coupled to B. (Ball and socket notation, A socket, B ball, subscript interface name).

Note that JButton is an example of this. JButton tells action listener something has happened, so that your implementation of an action listener can react.

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

Model-View-Controller Architecture/Pattern

A

Model: Holds and manipulates data. Performs the logic. Notifies View when data changes.

View: Takes user input. Presents and visualizes data.

Controller: Receives user input. Converts input into messages to the model.

Interaction cycle. User sees view, gives input with controller (using the view), which messages the model, which then notifies the view of changes…

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

How does MVC work with Observer Pattern?H

A

The view is the observer of model which is the subject. The model notifies the view of changes, but they don’t need to know everything about each other.

The controller has listener (implementing ActionListener), and observes the view which acts as a subject for it.

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

In MVC with Observer pattern, what might the model do after changing its data?

A

Call setChanged() to indicate a change of data.
Call notifyObservers() to notify all observers that the model has changes.
Causes their update() method to run.

The View can either request data from the model (pull) with accessors methods. Or it can be sent data directly from the model (push).
Note: If using pull, the view needs the model as an instance variable.

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

Todo: Continue work on MVC flashcards.

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