mercredi 3 juin 2020

Where should I put Factory instance (Factory Design Pattern)?

In my application I have two packages Counters and Checkers. Counters counts some values like number of sentences, words, characters. Checkers check some conditions like if value is less than 100. I used Factory Design Pattern here, every Checker implements Checking interface and every Counter implements Counting interface.

enter image description here

If I use ReadabilityChecker Factory in Main everything is simple and in accordance with the design pattern. But what if I want to use Counter in any of Checker class. Should I put new factory instance in every class or put it in CheckingText interface? If I put it in interface I will have to make it with empty Counter constructor and then setCounting() in every class. If I place a new instance in each class there will be a code repetition and creation of new instances each time. Which way should I go?

numberOfSentences = new Counter(new SentenceCounter()).count(text); //new object every time
numberOfTheWords = new Counter(new WordCounter()).count(text);

or

Counter counter = new Counter(); //factory with empty constructor in CheckingText interface 
counter.setCounting(new SentenceCounter()).count(text); //and then

Aucun commentaire:

Enregistrer un commentaire