Design patterns in object orientation

MVC-principle

  • Model: contains program logic
  • View: presentation of program to the user
  • Controller: interaction with the program

The design of the controller depends on the model, not vice versa. The controller manipulates the model. The controller itself can put a user in control through a GUI.

The view should be a passive observer, not affecting the model. This can be implemented using the Observer pattern. The model should however have ways of informing access methods

Observer Pattern

1) The model notifies the view when something occurs. 2) The view observes the model

Terminology: the observer attaches itself to a observable subject

Example: Buttons notify action listeners when they are pressed (1) and action listeners attach themselves to the button to be notified.

When do we use the observer pattern?

Situation: We have some source of events, and we have one or more observer objects that need to be notified.

1) Define observer interface 2) All concrete observers implement it 3) Any observable extends the Observable class (this is not an interface!) 3) The subject maintains a collection of all its observers 4) The subject needs methods for attaching and detaching observers 5) The subject notifies all observers in case there is a change

The Observer interface only has one method:

void update(Observable o, Object arg), where 'o' is the observable object notifying this observer.

MVC + Observer

1) Model extends Observable 2) View implements Observer 3) The main class that extends Application probably is also partially a view that implements Observer 4) Make a model 5) Controller must know the model, so pass it in the constructor 6) Add observers to the model with addObserver function. Pass views that Observer.

Model: 1) Add attribute to the model referring to one or more observers 2) Add a method with which observers can indicate that they want to be informed (e.g. update(Observable,object)). 3) a. Invoke the update method in the Observable classes where needed.

for (Observer o: observers){
     o.update(this, ...);
}

3) b. Extend the Observable class, and use notifyObservers(Object).

View:

1) Implement the Observer interface, and give appropriate definition of update.

Strategy pattern

Pattern to avoid duplicate code.

Let's say you have a game with players, and the only essential difference between the players is only one thing: the strategy they use for playing the game. Since other than that function everything is the same, we could make "Player" an interface requiring every concrete player to implement its abstract classes. This would lead to duplicate code, because most implementations are exactly the same. However, the fact that we need an interface, and cannot suffice with simply one "Player" class, is that each type of player has a different strategy. Since the definition of the strategy function is abstract in the interface, this allows "Players" to implement it differently.

Solution:

1) Define all functions that are exactly the same for each player in a "Player" class, not in an interface. 2) Define an interface "Strategy" only for the player strategy 3) Implement the strategy interface in concrete strategy classes. 4) Let the "Player" class have an attribute of type "Strategy", which is set in the constructor. 5) Every player instance will have a concrete strategy. Calling a function from this strategy class will return the appropriate result.