L13: Continuing to exchange behaviour at runtime Flashcards

1
Q

Template: what it does, what it is, what it is used for

A

The template defines behavior with refinable steps.

Implement an extensible skeleton of an algorithm
- Subclasses can refine individual steps (without impacting the general structure)
- Separate constant (invariant) skeleton from variable (variant) steps
- Define concrete extension points (“hook” methods)

Used for:
- Providing templates in programming languages (e.g., Java AbstractList)
- Games
- Loggers
- Data validation

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

Problems with template?

A

Need some flexibility/checks
- Formatting of messages
- Distinguish between message and time logging
- Prevent incorrect inputs/customizations

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

Notes regarding templates

A
  • Non-abstract methods can be ignored by extension (uses default) -> Optional refinements (theoretically, even the template method can be exchanged)
  • Abstract methods must be implemented by extension -> Mandatory/expected refinements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Being careful with the template

A
  • Can challenge comprehension and maintenance (complexity, indirections, inheritance, … of many steps and extensions)
  • Without multiple inheritance, some tweaks and workarounds needed (previous example if we could use multiple inheritance for formatting, time, …)
  • May violate Liskov Substitution (oiof a subclass does not define all interface methods)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Template vs Strategy

A

Template method:
- Builds on inheritance
- Modifies parts of an algorithm via subclasses
- Done on class level, so changes are done statically and to all objects

Strategy:
- Builds on composition
- Exchanges the entire algorithm by supplying strategy object
- Done on object level, so changes are done dynamically and impact one object

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

State: what it does, what it is, what it is used for?

A

The state alters object behavior on internal change.

Separate state-dependent behavior into own, referenced units
- Fixed number of states, of which only once can be active
- A set of triggers causes specific state changes (i.e., a trigger causes a specific change from one specific to another specific state)
- Closely related to state machines

Used for
- Game development (e.g., character control)
- State machines

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

Strategy vs state

A

State:
- Behaviour may change completely between states (different goals, depending on state)
- Each state explicitly linked to other states

Strategy:
- Family of exchangeable behaviors (same goal, different implementation)
- Client can flexibly use/change strategies (if known)

Structure of state and strategy are identical

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

Being careful with the state

A
  • Can be overkill if the represented state machine is small
  • May be over-engineering the problem
  • Hardcoded state changes (reducing flexibility and extensibility)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly