Posts

Showing posts from June, 2018

Behavioral Patterns - State Pattern Exercise

behavioral-patterns-state-pattern-exercise The State Pattern allows an object to alter its behavior when its internal state changes. It tremendously reduces the complexity when introducing more states later to the object. In this exercise, we will use the State Pattern to create a modern vending machine to understand the components of the State Pattern. Reading Materials https://en.wikipedia.org/wiki/State_pattern (10 minutes reading) Practice Materials Use the State Pattern to create a modern vending machine that does the following things: It sells different kinds of fruit It has the following states: card swiped accepted card swiped rejected item selected sold sold out each state has the following operations swipe card select item confirm cancel dispense Components: Vending Machine (The state machine that has multiple states) - VendingMachine State (The interface that defines all operations that each state should have) - State C

Behavioral Patterns - Observer Pattern Exercise

behavioral-patterns-observer-pattern-exercise The Observer Pattern is widely used to solve one-to-many object state changes notification problems. The pub/sub model uses the Observer Pattern as the base pattern. It is also used in the event-driven systems. In this exercise, we will create a naive implementation of the pub/sub model to understand the components of the Observer Pattern. Reading Materials https://en.wikipedia.org/wiki/Observer_pattern (10 minutes reading) Practice Materials Use the Observer Pattern to create a naive implementation of the pub/sub model. Components: Observer (The interface that defines the callback method signature or the update method when it is notified.) - Observer Subject (The interface that observers listen to) - Subject AbstractSubscriber (The abstract class that attach the observer to a subject) - AbstractSubscriber AbstractTopic (The abstract class that keep track of a list of observers and provides observer reg

Behavioral Patterns - Memento Pattern Exercise

behavioral-patterns-memento-pattern-exercise The Memento Pattern is used to record the internal state of an object. It is used to create a state machine and also makes undo or redo operation very easy with this pattern. In this exercise, we will use a scenario of account and account manager to understand the components of this pattern. Reading Materials https://en.wikipedia.org/wiki/Memento_pattern (15 minutes reading) Practice Materials Use the Memento Pattern to support an account manager to do save and roll back operations for his managed accounts. Components: Originator (The object whose state will be saved as a memento) - Account Memento (The object that represent the saved state from its originator) - AccountMemento Caretaker (The object that keep the immutable state of mementos) - AccountManager Client (The entry point of the application that will run the given scenario) - App Tip Not all states of the originator have to be saved to a me