patterns overview state pattern decorator pattern state and decorator patterns downloads references home
about three-ten software created with contact
Welcome!  

Our group has chosen to use the life of a gambler to demonstrate usages of the decorator and the state design patterns.

The state design pattern allows an instance of a class to alter its behaviour at runtime based on the internal state of the instance. The gambler in our game utilizes the state design pattern to change its behaviour depending on the amount of money the gambler has in his pocket

In our implementation, the gambler is always in one of the three states which are poor, normal, and rich. In addition to placing a bet, the gambler can perform a special operation. The actual action taken when the gambler performs the special operation depends on the current state of the gambler. In the poor state, the gambler can make some extra money by working at a local sweat shop with the special operation. While in the rich state, the gambler is given the option to purchase a car, or cruise around if the gambler already owns a car, when he performs the special operation. If the gambler performs the special operation while in the normal state, it realizes how normal he is and there is nothing special he can do.

The decorator design pattern allows responsibilities to be added to an instance of a class dynamically at runtime. The implementation of the gambler uses this design pattern to attach extra responsibilities to the gambler when he purchases a car.

As mentioned above, the gambler can purchase a car if he is in the rich state. Once the gambler purchases a car, we add the "responsibility" of allowing the gambler to spend money on improving the appearance of the car. The new option is attached to the special operation regardless of the state of the gambler. In addition to being able to perform the state-specific special operation, the gambler is given the option to improve his car after the completion of each state-specific special operation.

With the two patterns working together, we have implemented a realistic model of a gambler with various states of wealth and the ability to purchase and improve a car.