Implementation

[Previous] [Next]

You have a few options for designing and implementing the State design pattern. Your design will obviously influence your implementation. In the design we'll be discussing, all state concrete classes implement the same interface expected by a client application. A single central state manager class implements the state transition behavior that results in dynamic behavior changes at run time.

Implementing state subclasses is straightforward. In a class module, use the keyword Implements and implement all methods of the expected interface (State). Ensure that the class's Instancing property is of the public and creatable variety to allow reuse in different contexts. Next define a reference to a central state manager. All invocations to state interface methods that result in state changes should be forwarded to the central state manager. All method invocations that preempt behavior should be delegated to the appropriate state subclass object. A general rule of thumb is that class property settings (Property Let or Property Set procedures) should reflect state changes, and class method invocations should represent behavior. Here's a code extract of a BullishTrader class module that represents a state subclass implementation:

 ' BullishTrader.cls Implements Trader Private m_TradeSystem As TradeSystem Private m_tsm As TraderStateManager  Private Property Let Trader_StockPrice(Stock As String, _ RHS As Double) m_tsm.StockPrice(Stock) = RHS End Property Private Sub Trader_ExecOrder() Dim newTrader As Trader Set newTrader = m_tsm.getTraderObject(Me) If TypeName(newTrader) = TypeName(Me) Then Call m_TradeSystem.Buy(1000, Stock, m_tsm.StockPrice _ + ONE_UP_TICK) Else newTrader.ExecOrder End If End Sub  

Notice that setting the StockPrice property value is delegated to the TraderStateManager (CentralStateManager) object. Depending on the stock price change, the state of the Trader object might or might not be affected. As the Trader.ExecOrder method implementation illustrates, first you call the TraderStateManager.getTraderObject method to determine the correct Trader object that needs to be created. If the price hasn't moved against whatever threshold is determined by the TradeStateManager object, the BullishTrader object will get a reference to itself back and will run through its implementation. Otherwise, the BullishTrader object will delegate to the object referenced by the newTrader object variable.

Implementing the CentralStateManager object requires a bit more work. It must maintain state and define state transition behavior based on state changes. To allow for the introduction of new behavior at run time based on state changes, the CentralStateManager object must have access to resources at run time that can be updated by some means external to it, such as a database. New conditions can also be introduced in the same manner. Building on the Trader theme from the last code extract, the following extract is a code sample of the TraderStateManager class. The TraderStateManager object maintains a Dictionary of Trader subclass objects that could have been populated either at class initialization time or on demand. When the StockPrice property value is updated, a property dirty flag is set to true to indicate to the getTraderObject method that it needs to evaluate the state prior to determining the correct Trader subclass object reference to return.

 ' TraderStateManager.cls Private Property Let StockPrice(Stock As String, RHS As Double) m_Security.StockName = Stock m_Security.StockPrice = RHS m_bIsDirty = True End Property Public Function getTraderObject(currTrader As Trader) As Trader If m_bIsDirty Then ' Go through a set of rules to determine which Trader object ' reference to return. If...Then Set Trader = m_Traders("BearishTrader") End If  Else Set Trader = currTrader End If End Sub 

This TraderStateManager class implementation is obviously simplified, but the point is that it maintains the state and that a reference to the appropriate Trader object cached in a Dictionary is returned from getTraderObject. The sample application for this chapter covers in detail a full implementation of the State design pattern.



Microsoft Visual Basic Design Patterns
Microsoft Visual Basic Design Patterns (Microsoft Professional Series)
ISBN: B00006L567
EAN: N/A
Year: 2000
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net