Design patterns Flashcards
Cohesion
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.
Coupling
If many classes have relationships to each other, the coupling is high.
Low coupling is preferred.
What is a design pattern?
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.
Types of design problems
Creational. Focus on object creation.
Structural. How to organize different classes and objects.
Behavioral. Communication patterns between objects.
Singleton pattern.
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.
Factory design pattern.
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.
Strategy design pattern.
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.
Command pattern.
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.
Observer Design Pattern
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.
Model-View-Controller Architecture/Pattern
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 does MVC work with Observer Pattern?H
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.
In MVC with Observer pattern, what might the model do after changing its data?
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.
Todo: Continue work on MVC flashcards.