Bendrinti naudojant


Strategy Class Design Pattern

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.

See also

Microsoft Dynamics AX Class Design Patterns

Microsoft Dynamics AX Design Patterns

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