0

In the context of a project, I must implement a card game into java, using MVC pattern. Currently the game is running well on console, but I must add a graphical user interface. I understand well how work controlers and models, but I'm having some difficulties with the view.

You see, i've got many model classes such as "Player" "Card" etc.. which extend from Observable. So as a consequence I suppose I should have a lot of views. But my application will take place in a JFrame object, which would be in a view Class.

My question is : how can all the different views classes have access to the JFrame object (for ex add a button etc..) which is contained in a another view class ? (the JFrame would be in a class such as GameObserver I guess)

Hunteer
  • 1
  • 1
  • Why isn't your JFrame the View class? What are your views? It's hard to understand MVC if you don't have more than one view. – Fuhrmanator Dec 26 '14 at 15:10

1 Answers1

-1

Independently from MVC, if you only have one main JFrame and you never create another, then it may be a good solution to make it Singleton and access it statically, such as SingletonFrame.getInstance().

There are many ways to create a Singleton pattern, here is one:

public class SingletonFrame extends JFrame {

    private SingletonFrame() {
        //your initialization code
    }

    private static class SingletonHolder { 
        public static final SingletonFrame instance = new SingletonFrame();
    }

    public static SingletonFrame getInstance() {
        return SingletonHolder.instance;
    }

}
Kostas Chalkias
  • 3,811
  • 2
  • 21
  • 23
  • Okay thanks ! I didn't think about it. I've also heard about the composite pattern for the view (even if I don't know exactly how it works), would it be also a solution for my problem ? – Hunteer Dec 25 '14 at 18:06
  • Teh Composite pattern enables treating individual objects and compositions of objects uniformly, enabling consistent processing, simplifying code. Regarding Composite Views this would help you to create a wrapping modedl, eg a panel having some items in it, another widget being part of the panel, having some other items in it and so on. check this post http://stackoverflow.com/questions/13578312/mvc-differences-between-two-step-and-composite-view-patterns – Kostas Chalkias Dec 25 '14 at 18:24