Strategy Class Design Pattern [AX 2012]

Updated: March 15, 2011

Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012

The Strategy pattern is for doing different things in a similar manner. For example, you have similar switch statements or if statements in your code.

The following code example illustrates similar switch statements:


…
    // First operation
    switch (_direction)
    {
        case up:
             ...
        case down:
             ...
    }
    ...
    ...
}
    ...
    ...

    // Second operation
    switch (_direction)
    {
        case up:
             ...
        case down:
             ...
    }

The following illustrates an example where you split up your code in a class hierarchy:

Class hierarchy for a strategy pattern

 

The following code example illustrates using a construct object and a direction object at the start of your code. Then work on the correct direction object in an abstract way after.


Direction direction = Direction::construct(_direction);
     ...
    direction.firstOperation();
    direction.firstOperation();
     ...
    direction.firstOperation();
     ...
     ...
    direction.secondOperation();
     ...


The correct process will occur as the direction object is instantiated to the correct situation.


Announcements: New book: "Inside Microsoft Dynamics AX 2012 R3" now available. Get your copy at the MS Press Store.

Community Additions

ADD
Show: