typedef
typedef

Reputation: 1930

Strategy pattern modification

How to modify classic strategy pattern in case when each ConcreteStrategy talks to DataStorage? There are different types of data storage and the interface depends on the concrete strategy.

Upvotes: 1

Views: 117

Answers (3)

T.S.
T.S.

Reputation: 19330

Remember that pattern is just general thing. You can do whatever you need to make that pattern work in your situation. You can combine your strategy with Factory, Memento, etc. Right now everybody talks about "injecting". But using interfaces has been around for long time.

public class ConcreteStrategy : IStrategy 
{
    protected IDatastorage _dataStrorage;
} 

This should do it

Upvotes: 0

David Osborne
David Osborne

Reputation: 6781

Make your strategies dependent on DataStorage via constructor injection. Then each strategy can use DataStorage as they please.

If DataStorage implements many interfaces, make each strategy dependent on that particular interface.

Upvotes: 1

Robin
Robin

Reputation: 10368

A brief answer could be:

First Define your common interfaces for data storage like:

Query()
Update()
Delete()
Add()
Create()

Second, implement these interfaces in each concrete data storage implementation class.

When you use these class objects, you call the method via interface methods, the real task carrier is the specific object.

Upvotes: 0

Related Questions