Posts

Showing posts from June, 2018

Behavioral Patterns - State Pattern Exercise

behavioral-patterns-state-pattern-exerciseThe 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 MaterialsUse the State Pattern to create a modern vending machine that does the following things:It sells different kinds of fruitIt has the following states: card swiped acceptedcard swiped rejecteditem selectedsoldsold outeach state has the following operations swipe cardselect itemconfirmcanceldispenseComponents:Vending Machine (The state machine that has multiple states) - VendingMachineState (The interface that defines all operations that each state should have) - StateConcrete States (All possible states of the vending machine) - CardSwipedAc…

Behavioral Patterns - Observer Pattern Exercise

behavioral-patterns-observer-pattern-exerciseThe 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 MaterialsUse 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.) - ObserverSubject (The interface that observers listen to) - SubjectAbstractSubscriber (The abstract class that attach the observer to a subject) - AbstractSubscriberAbstractTopic (The abstract class that keep track of a list of observers and provides observer registration implementations) - AbstractTopicText…

Behavioral Patterns - Memento Pattern Exercise

behavioral-patterns-memento-pattern-exerciseThe 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 MaterialsUse 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) - AccountMemento (The object that represent the saved state from its originator) - AccountMementoCaretaker (The object that keep the immutable state of mementos) - AccountManagerClient (The entry point of the application that will run the given scenario) - AppTipNot all states of the originator have to be saved to a memento object.To implement rollback or revert in the …