lundi 2 septembre 2019

How to make Single Responsibility Principle work with my Strategy class?

How do I make my "complex" Strategy class fulfill Single Responsibility Principal? Is there any other pattern that I can apply instead?

I thought about Decorator pattern that can add more responsibility/operations to my "algorithm", but it does not seem to fit.

See this SimpleStrategy class. It gets cached and latest records, compares them and updates if required, and then performs any post processing.

class SimpleStrategy {
    private DbRecordFetcher dbRecordFetcher;
    private LatestRecordFetcher latestRecordFetcher;
    private RecordComparator recordComparator;
    private DataUpdater dataUpdater;
    private PostProcessor postProcessor;

    public void process(Input input) {
        I cached = dbRecordFetcher.fetch(input);
        I latest = latestRecordFetcher.fetch(input);

        boolean isChanged = recordComparator.compare(cached, latest);

        if (isChanged) {
            dataUpdater.update(input, latest);
        }

        postProcessor.process(input, latest);
    }
}


All the classes that SimpleStrategy is composed off are interfaces, so I can inject different implementations, for example, to fetch db and latest records.

How do I refactor it to make it follow SRP?

Aucun commentaire:

Enregistrer un commentaire