08 - SW Design - Design Patterns - Part4
08 - SW Design - Design Patterns - Part4
§ Iterator Pattern
§ Strategy Pattern
§ Chain of Responsibility
§ Summary
§ The first version of the app could only build the routes over roads.
§ After that, you added an option to let people use public transport.
§ Take a class that does something specific in a lot of different ways and extract all of
these algorithms into separate classes called strategies.
§ The original class must have a field for storing a reference to one of the
strategies.
§ It delegates the work to a linked strategy object instead of executing it on its own.
§ It works with all strategies through the same generic interface, which only exposes a
single method for triggering the algorithm encapsulated within the selected strategy.
§ You can add new algorithms or modify existing ones without changing the code of the
context or other strategies.
§ In our case, each check should be extracted to its own class with a single method that performs the check.
§ The pattern suggests that you link these handlers into a chain.
§ Each linked handler has a field for storing a reference to the next handler in the chain.
§ In addition to processing a request, handlers pass the request further along the chain.
§ The request travels along the chain until all handlers have had a chance to process it.
§ A handler can decide not to pass the request further down the chain and stop any further processing.
§ With ordering systems, a handler performs the processing and then decides whether to pass the request
further down the chain.
§ Assuming the request contains the right data, all the handlers can execute their primary behavior, whether it’s
authentication checks or caching.
§ A handler decides whether it can process it. If it can, it doesn’t pass the request
any further.
§ So it’s either only one handler that processes the request or none at all.
§ When a user clicks a button, the event propagates through the chain of GUI
elements that starts with the button, goes along its containers (like forms or
panels), and ends up with the main application window.
§ The event is processed by the first element in the chain that’s capable of
handling it. This example shows that a chain can always be extracted from an
object tree.
§ The Base Handler is an optional class where you can put the
boilerplate code that’s common to all handler classes.
§ It interprets the input, performs any necessary operations (such as updating the
Model), and decides how to respond.
§ The Controller updates the Model based on the user input or application
logic.
§ The View requests data from the Model to update its display.
§ The Controller updates the View based on the changes in the Model
or in response to user input.
§ Iterator Pattern
§ Strategy Pattern
§ Chain of Responsibility