0

Lets say I have the following code in my GamePanel class:

public GamePanel(Menu menu){
this.addMouseListener(new PlanterListener(this))

PlanterListener class:

public PlanterListener(GamePanel game){
        this.game=game;
        this.plantActive=game.getPboard().getPlantActive();
        this.stage=game.getStage();
    }
    
    public void mouseClicked(MouseEvent e) {
        this.mxPos=game.getmxPos();
        this.myPos=game.getmyPos();
        int row=this.myPos/100;
        int col=this.mxPos/100;
        this.plantActive=game.getPboard().getPlantActive();
        row-=1;
        if(row>=0&&col>0&&this.plantActive!=null){
            Plant plant=null;
            switch(this.plantActive){
                case "PeaShooter":
                    plant=new PeaShooter(col*100,row*100,this.stage);
                    break;
                case "CherryBomb":
                    plant=new CherryBomb(col*100,row*100,this.stage);
                    break;
                case "PotatoMine":
                    plant=new PotatoMine(col*100,row*100,this.stage);
                    break;
                case "Repeater":
                    plant=new Repeater(col*100,row*100,this.stage);
                    break;
                case "WallNut":
                    plant=new WallNut(col*100,row*100,this.stage);
                    break;
            }
            if(plant!=null){
                if(this.stage.checkSpace(plant)&&!this.stage.getStatus()){
                    System.out.println("Plant added");
                    this.stage.addPlant(plant);
                }else{
                    System.out.println("C"+col*100);
                    System.out.println("R"+row*100);
                }   
            }
        }

Would you still call this an implementation of the Observer Pattern? I feel like the code just made use of the built in functionality of the java.swing library to handle user input, and not much has been gained in terms of decoupling since the PlanterListener relies heavy on both the GamePanel class and the current stage. Is my argue valid?

1
  • FYI: you can make an enum instead of using strings; then the compiler will tell you if you have a typo; slightly more advanced, you can also create methods in the individual enum values so you can write plant=this.plantActive.createNewPlant(col*100, row*100, this.stage); – user253751 Dec 16 '20 at 15:29
1

What exactly are you considering "this"?

Is your code an implementation of observer? No. You are not doing the observer: you are not implementing a class that stores its subscribers to notify them of changes. You are using an event system.

Is observer implemented somewhere down the line? Who is to tell. Do you know how java's event system is implemented under hood? I sure don't. But if I remember correctly, the implementation is actually system specific.

So it might very well be the case of Schrödinger's observer—until you open the box there is no tell if its there or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.