This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes Design Dilemma Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Design Dilemma " Watch "Design Dilemma " New topic
Author

Design Dilemma

Chris Ramsey
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
I am writing a FlashCard application for my daughter. At the same time I want to learn and implement a design pattern that will allow me to add functionality easily and one that I can re-use for future apps. I've looked at MVC, EventManaging, Visitor and I feel I understand what they do, but it falls apart when I try to implement them. Here's a description of the what I think the classes I have so far and what role I think they play within the app:

1. FlashCardGenerator: (MODEL?)
The FlashCardGenerator class creates FlashCards. It's createFlashCard method is called to generator 2 random numbers and make an addition or subtraction flash card. (multiplication and division to be added later).

I'm not sure whether I should have an actual FlashCard class or a member variable of the FlashCardGenerator since its primarily just a string that holds the numerals, operator and the solution.

2. FlashCardSet: (VIEW of FLASHCARDMODEL?)
I am not sure if this class is necessary. It is an array list that holds the FlashCards generated by the FlashCardGenerator. This way the user can determine how many cards they want to solve for each round. I guess an alternative would be just to loop a call to FlashCardGenerator.createFlashCard method. One reason for having this class it that if any flash cards are answered incorrectly 3 times they are marked as incorrect and stored so that at the end of the round the program will inform her of how many were answered incorrectly and if she would like to try them again. So this class would have two ArrayLists (active and error) The active set holds the flashcard she is currently working on and the error set would house the incorrectly answered cards.

3. FlashCardUI (CONTOLLER TO FLASHCARDSET AND FLASHCARD)
This class has the flashcard panel, the progress panel and the skip and reset buttons. This is where I really want to know which pattern would best fit this scenario.

The basic program flow is this:
a. flash card is displayed
b. flash card is answered
c. answer checked against solution
d. progress panel updated
e. next flash card in set displayed

The skip button allows her to move to the next flash card in the set. The reset button clears the set and asks her if she would like to create a new set of cards. The progress panel has 2 labels (correctLabel and incorrectLabel) that are updated each time she answers the equation based on whether the answer is correct or not. My road block is here. Which class should be responsible for checking her answer against the solution and updating the UI based on the outcome? I have explored creating an AnswerEvent object that would hold her answer and go to some class to be checked against the solution. But again, should there be a separate class that does this? Should the UI class create the answerEvent? Maybe a CheckAnswer class that listens for answers and then returns a status of right or wrong? My thinking is that a design pattern will answer the questions and help me sort out who and where to do what, but which one. Thanks for your patience.

[ June 02, 2004: Message edited by: Chris Ramsey ]
[ June 02, 2004: Message edited by: Chris Ramsey ]
Sachin Joshi
Ranch Hand

Joined: Mar 23, 2004
Posts: 43
Hi Chris,

I will try to answer some of your questions. I have used MVC pattern in the applications that needs to access some Database. My view of looking at this pattern is like :

Model ->
This layer will depict a Data Model. So, in case of database applications, if I am accessing any data, the structure that stores the data will be stored in Model Layer Classes. In your application, I feel, FlashCardGenerator will fit in this Layer, as it is responsible for generating the cards in first place.
Regarding having a separate class as FlashCard, I would suggest you should have it. A separate class will be a good way to represent your Flashcard data. Also, it would be a flexible approach in case tomorrow if you need to add some more details for each flash card.

Controller ->
This layer contains the business logic or application logic to modify the data. I feel, you should have 1 more class that will be responsible for handling your applications logic like Deciding the answer is right or wrong, whether the current card is active or error card and so on. FlashCardSet class described by you can still be stored in Model Layer as it is storing the Cards. and it can be used as a data for Controller Layer class.

View -->
These are the classes that creates the front end of the application. Usually, this layer is created using JSP pages, Html pages, Swing applications, etc. Above said FlashCardUI class can be a good fit here.

So, in usual cycle of operations, View Layer class will be responsible for interacting with the user actions and take the user inputs. After taking the inputs, it can call Controller Layer classes to handle those inputs. In turn, controller can use Model Layer classes as their data to be acted upon. After the controller is ready with the response, it can refresh View layer classes to update the UI and display the results.

I hope this should push your road block a step ahead. Do reply with your views on this.

regards,
Sachin


Originally posted by Chris Ramsey:
I am writing a FlashCard application for my daughter. At the same time I want to learn and implement a design pattern that will allow me to add functionality easily and one that I can re-use for future apps. I've looked at MVC, EventManaging, Visitor and I feel I understand what they do, but it falls apart when I try to implement them. Here's a description of the what I think the classes I have so far and what role I think they play within the app:

1. FlashCardGenerator: (MODEL?)
The FlashCardGenerator class creates FlashCards. It's createFlashCard method is called to generator 2 random numbers and make an addition or subtraction flash card. (multiplication and division to be added later).

I'm not sure whether I should have an actual FlashCard class or a member variable of the FlashCardGenerator since its primarily just a string that holds the numerals, operator and the solution.

2. FlashCardSet: (VIEW of FLASHCARDMODEL?)
I am not sure if this class is necessary. It is an array list that holds the FlashCards generated by the FlashCardGenerator. This way the user can determine how many cards they want to solve for each round. I guess an alternative would be just to loop a call to FlashCardGenerator.createFlashCard method. One reason for having this class it that if any flash cards are answered incorrectly 3 times they are marked as incorrect and stored so that at the end of the round the program will inform her of how many were answered incorrectly and if she would like to try them again. So this class would have two ArrayLists (active and error) The active set holds the flashcard she is currently working on and the error set would house the incorrectly answered cards.

3. FlashCardUI (CONTOLLER TO FLASHCARDSET AND FLASHCARD)
This class has the flashcard panel, the progress panel and the skip and reset buttons. This is where I really want to know which pattern would best fit this scenario.

The basic program flow is this:
a. flash card is displayed
b. flash card is answered
c. answer checked against solution
d. progress panel updated
e. next flash card in set displayed

The skip button allows her to move to the next flash card in the set. The reset button clears the set and asks her if she would like to create a new set of cards. The progress panel has 2 labels (correctLabel and incorrectLabel) that are updated each time she answers the equation based on whether the answer is correct or not. My road block is here. Which class should be responsible for checking her answer against the solution and updating the UI based on the outcome? I have explored creating an AnswerEvent object that would hold her answer and go to some class to be checked against the solution. But again, should there be a separate class that does this? Should the UI class create the answerEvent? Maybe a CheckAnswer class that listens for answers and then returns a status of right or wrong? My thinking is that a design pattern will answer the questions and help me sort out who and where to do what, but which one. Thanks for your patience.

[ June 02, 2004: Message edited by: Chris Ramsey ]

[ June 02, 2004: Message edited by: Chris Ramsey ]
Chris Ramsey
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
Thanks Sachya Joshi,

So the FlashCardSet class is a part of the model layer and not the controller. You are saying that I need a class I'll call FlashCardController that will check the answer and move the flash card to its appropriate list depending on if the answer she gave is correct or not and update the view to report whether her answer was correct or not.
This means that it needs to have a reference to the FlashCardSet, to get the flash card solution to compare her answer with and the FlashCardUI display the result. So is the textfield that she enters her answer in actually a part of the controller layer and not the view, like I was thinking?
Sachin Joshi
Ranch Hand

Joined: Mar 23, 2004
Posts: 43
Originally posted by Chris Ramsey:
Thanks Sachya Joshi,

So the FlashCardSet class is a part of the model layer and not the controller. You are saying that I need a class I'll call FlashCardController that will check the answer and move the flash card to its appropriate list depending on if the answer she gave is correct or not and update the view to report whether her answer was correct or not.
This means that it needs to have a reference to the FlashCardSet, to get the flash card solution to compare her answer with and the FlashCardUI display the result. So is the textfield that she enters her answer in actually a part of the controller layer and not the view, like I was thinking?


Hi Chris,

ya, I would suggest FlashCardSet is a part of Model and controller class like FlashCardController will use it as a data.So, once the user enters the answer, your controller will take appropriate action and will update the Data (Model).
But, I differ on what u said that FlashCardUI is part of controller. NO, I would suggest it to be a part of View. Once FlashCardController completes the data updations, it will refresh the View. As the View class is using the data class from Model layer to display the result, it will get the refreshed data to display till this time.

Regards,
Sachin
Fintan Conway
Ranch Hand

Joined: Apr 03, 2002
Posts: 141
Originally posted by Chris Ramsey:
So is the textfield that she enters her answer in actually a part of the controller layer and not the view, like I was thinking?


Hi Chris,

The TextField is part of the 'View'. In the actionPerformed method of the submitAnswer button (assuming this is how you are doing the answer check), I would put a call to the FlashCardController like the following :


This passes the answer submitted to the Controller, who can now check this answer with the correct answer which it requests from the Model Layer.

Regards,

Fintan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Design Dilemma
 
Similar Threads
Ready-to-use flash cards
Adding an action listener to an entire class
Better way of doing this?
failed with 51%; am upset
Flash Cards ? What are those ?