Question about application architecture

I am new to developing OO systems. I have a simple application for flash cards in which I had problems finding the right way to archive the system. The application has a simple graphical interface with buttons for questions, answers and buttons. The question and answer data set is stored in the Derby embedded database.

Quick setup:
Database class - processes the connection, disconnects and returns an ArrayList based on the selected filter (currently called by the CardSet) (the DB class is configured as static)
The CardSet class. Uses the used ArrayList, holds the current Map. Map
class - contains data for a flash card (question and answer, a couple of other things)
Application class - creates a graphical interface and processes action events

So here is my question: I want to separate the GUI and application logic. I think this may be a good example for MVC, but I'm not sure how to really separate it all out (never used it). Does the controller create a class basically, which then launches the graphical interface and then creates other classes (in my case, CardSet). How about access? Should some things be static?

Another question. To handle GUI events, did you just configure it to call a common method in the controller class? For example, the "Next card" button is clicked if it just calls something like controller.nextCardAction ()? Should I try to use the Observer pattern so the GUI can retrieve data?

, Java-. . . Head First Design Patterns, , .

+3
2

, .

?

, CardSet, , , . CardSet , Card . , , .

: , , . reset, . , .

, MVC - , - . Observable Observer update(), EventListenerList.

+1

.

, " ". GUI :

public interface ModelListener {
   public void modelChanged();
}

public class FlashCardView implements ModelListener {

   private FlashCard model;

   public FlashCardView(FlashCard card) {
     setModel(card);
   }

   public void setModel(FlashCard card) {
     if (model != null) {
       model.removeListener(this);
     }
     model = card;
     model.addListener(this);
   }

   public void modelChanged() {
     (read out model values and update the screen displayed values)
   }

}

public interface Model {
    public void addListener(ModelListener listener);
    public void removeListener(ModelListener listener);
}

public class FlashCard() {

   private String answer;

   private String question;

   private Vector<ModelListener> listeners;

   public FlashCard(String question, String answer) {
     this.question = question;
     this.answer = answer;
     this.listeners = new Vector<ModelListener>();
   }

   public void setAnswer(String newAnswer) {
     this.answer = newAnswer;
   }

   public void setQuestion(String newQuestion) {
     this.question = newQuestion
   }

   public void addListener(ModelListener listener) {
     listeners.add(listener);
   }

   public void removeListener(ModelListener listener) {
     listeners.remove(listener);
   }

   private void notifyListeners() {
     for (ModelListener listener : listeners) {
        listener.modelChanged();
     }
   }

}

, , , "" "". , setModel(...), "view" .

-, , , -. , , , .. .

+1

Source: https://habr.com/ru/post/1753013/


All Articles