Lecture - Imperative Programming TT23, IV


  • How to design a large program
    • “Seperation of concerns”
      • List the concerns of the program, e.g. what will the program be used for, and what will it not?
      • List the nouns and then these become the candidates for classes (??)
    • “Stability under change”
      • List the changes that the program might undergo, e.g. a text editor might change the way that it represents text
      • Then review the list of nouns and consider a more general idea that will be better adapted to change
    • Make class diagrams to think about how the classes you’ve established might fit together
  • Design patterns
    • Observer pattern:
      • Problem: several users of the same data can mean that the users are tightly coupled to the data.
      • Solution: make a modifiable list of ovservers. Observers can be added and removed at truntime and data owner notifies all registered observers of changes.
      • Consequences: interface not obvious, must choose either to simply notify that some change has happened or specify exactly what has changed, and cannot rely on a specific order amongst observers
      • All observers share the same interface
    • Model-View-Controller
      • Not exactly a pattern but an overall architecture combining several patterns
      • Model: where the data is stored
      • Controller: used to manipulate the data, e.g. the one with which the user interacts. Reasons about what the interaction should do to the model, and asks the model to make the changes.
      • View: model updates the view component and then presents the information
      • The interaction between the model and the view is suitable for capturing with the observer pattern
    • Command
      • Reciever: class that carries out the effect of the command (the waiter)
      • ConcreteCommand: implements the command interface and carries out the command by calling the appropriate method in the Reciever (making an order)
      • Client: creates the concrete commands when required (the customer)
      • Invoker: takes the request/command from the client and passes it to the reciever via the execute method of the reciever class (waiter)
      • Read the section in Head First Design Patterns

Flashcards (GPT-4)

What does “separation of concerns” mean in program design?


Dividing a program into distinct, independent parts, each handling a specific aspect of the system.

What does “stability under change” mean in program design?


Designing a program to be adaptable to future changes, by considering general ideas and possible modifications.

What is the Observer pattern and its primary purpose?


A design pattern where a subject maintains a list of observers and notifies them of any changes; reduces coupling between data and its users.

What are the components and roles in the Model-View-Controller architecture?


Model: data storage; Controller: manipulates data based on user input; View: presents data to the user.

How does the Observer pattern relate to the Model-View-Controller architecture?


The Observer pattern can be used to handle the interaction between the model and the view, where the model notifies the view of changes.

What is the Command pattern?


A design pattern that decouples the object issuing a request from the object executing it, by encapsulating the request as an object.

What are the components in the Command pattern?


Command (interface), ConcreteCommand, Receiver, Invoker, and Client.

Explain the Command pattern in the context of a restaurant ordering system.


Client: customer; ConcreteCommand: order; Invoker: waiter; Receiver: kitchen staff; Command pattern decouples waiter from kitchen, allowing flexible order handling.

What is the role of the Client in the Command pattern?


The Client creates and configures ConcreteCommands, setting their state and context for the Invoker and Receiver to interact.

Why is the separation of the Client from the Invoker and Receiver beneficial in the Command pattern?


It enables better separation of concerns and a more flexible system, allowing the Client to focus on specifying actions without being involved in their execution.




Related posts