How to extract business logic in this example

109 views Asked by At

The following method filters a data collection based on order statuses. Im wondering what would be the best way to extract the business logic held within about the order statuses. For example, say the order statuses were to change down the line i.e. ('in_transit', 'at_courier') then this method, and associated tests would all need amended:

interface extractor {
    public function extract();
}

class ShippedOrderExtractor implements extractor {

    public function extract()
    {
        $dataCollection = $this->source->filter(
            array(
                'state_one' => 'shipped',
                'state_two' => 'with_courier'
            )
        );

        return $dataCollection;
    }
}
1

There are 1 answers

0
Dzianis Yafimau On BEST ANSWER

I'm c# developer, so I wont provide you a code. But in you case one of 2 patterns can be useful depending on what exactly is changed.

  1. To encapsulate behavior use Strategy pattern. Just attach Strategy object with extract() method and replace this object on different states. extract() method should provide different actions (change status line etc.).

  2. For more graceful state change of your orders - use State pattern. It uses context object with all essential data to read and to change. This context is shared with subscribers. For example, you can put in Context your order and for different states object change this context=>order status line and other things. Then you can notify subscribers that state is changed and return them context.

It's just thoughts, don't use patterns as-is but see main idea among them and adapt to your needs. Good luck!