Linking two abstract Factory patterns in Java

I am writing a quote matching program that requires two abstract Factory templates, these are two interfaces; QuoteFactory and ModeFactory . ModeFactory switches between EasyMode and HardMode , and QuoteFactory selects quotes between several different topics (i.e. Political Circles , SportsQuotes ). In short, the user will select the mode, if EasyMode is selected, then the user must guess the quote, while if the user selects HardMode, the user will be told who said the quote and then guess, so the implementation of quotes will vary depending on the mode and the selected quotes .

So far I have created ModeFactory as an interface and implemented it in EasyMode and HardMode, but now I need to somehow integrate another abstract Factory template (or more) into these modes so that Quotes can be selected. If this is useful, I also created a Quote class that defines my quotes.

Can someone help me come up with a basic implementation of these abstract factories? These are sketches of what I still have, although I cannot help but feel that I have somehow overdone it ...

EDIT: repeat what I mean: if the user selects Easy Mode, they are given the beginning of the quote and the author of this quote, whereas if they choose hard mode, they are provided only from the beginning of the quote. for instance

Easy mode: "I felt the power ..." Jose Mourinho

Hard mode: "I felt the power ..."

Hard mode does not allow the author to complicate the user the ability to guess the rest of the quote. In addition, this is not a school assignment. I read Head First Design Patterns, and now I'm trying to apply what I learned in different situations (instead of the Pizza example, I'm working on a Guessing Game quote after reading QI (British TV Show).

public interface ModeFactory { public Mode retrieveMode(String s); } public interface QuoteFactory { public Quote retrieveQuote(String s); } 
+4
source share
4 answers

Without thinking too much and trying to stick to your design, how about something like this (I assume the Quote class has getText () and getAuthor methods, you can also make it getText (int numberOfWords) so that you can configure which part quotes give):

 public enum Mode { EASY, HARD, } public enum Category { SPORTS, POLITICS, } public abstract class QuoteFactory { public QuoteFactory getQuoteFactory(final Mode mode) { // return either the Hard or Easy QuoteFactory } public abstract Quoute getQuote(Category category) } class HardQuoteFactory extends QuoteFactory { public Quote getQuote(final Category category) { // ... } } class EasyQuoteFactory extends QuoteFactory { public Quote getQuote(final Category category) { // ... } } 
+1
source

Can you take one step back and repeat what you are trying to do? I sometimes find that, having gone through the effort to explain the challenge to someone not familiar with this, the solution appears to me as if organically.

From your description, it seems that you started with a partial solution (β€œI want to use the Abstract Factory template”) instead of starting with what you really want to do (for example, β€œI want to provide a quote class that provides different quotes depending from taste (political, sporty) and mode (easy, hard). "

It seems to me that this is an enumeration, not a factory. There listing for {political, sports} and separate for the regime.

EDIT : thinking about it again, maybe this is a school assignment ("Use the abstract Factory template for ... blah blah blah"), and for this very reason you have the sorting started with half-solution.

+2
source

Perhaps you are probably over designing here. First of all, you need the Quote class. This should encapsulate the quote and have the appropriate accessors (getters) for both simple and hard mode. Then you need a collection of quotes. If they are stored in a database, you can easily select the type of quote.

I also believe that ModeFactory is not needed. Why do you need an interface only to choose between two modes? The state flag is simpler.

+2
source

It seems to me that you are missing an important concept, the Question class. This is built with both a mode and a quote, it can give the right question and call if you answered correctly:

 public class Question { public Question(Mode mode, Quote quote) { /* store these */ } public String getQuestion() { return quote.getQuote() + (mode == Mode.EASY ? quote.getAuthor() : ""); } public boolean isCorrect(String answer) { return quote.getFullQuote().equals(answer); } } 

My idea here is that the quote itself does not change between modes, namely, which parts of the quote are presented to the user. With this approach, you can have one repository of Quote objects that you can use anywhere. The question is responsible for displaying the correct amount of information to the user and deciding whether he has the correct answer.

+1
source

All Articles