mardi 19 mai 2020

Is my approach seems logical? Where should I place Listeners to satisfy MVC and Observer Design Pattern?

I am trying to create a basic shared gallery program with Users where they can follow/unfollow each other and add Images to their favoriteImagesList. Whenever they choose an image on JList,it will be shown and comments of the Media object that contains the image will be shown beside. By selecting the comments they will be able to follow the owner of comment. From their profile page they will be able to unfollow them. When I am implementing I should satisfy Model View Controller and Observer Design Patterns. In this assignment the view and controller will be separate. When User follows another User from Profile tab should be refreshed. I can't see how to use Observer Design with this requirements. In the examples shown in my lectures, all the listeners were placed in the Controller. I tried to build some structure but since I use custom subclasses of JPanel, the delegation chain gets longer, and I am having problem to update the objects when it is necessary. Can you please criticize and give some advises?

I know it is not fully containing and the proper class diagram but I am adding to show my approach.

enter image description here

Pages:

Login

Timeline

Profile

Controller which I try to place Listener classes:

public class Controller {

private LoginView loginView;
private Gallery tube;

public Controller(LoginView loginView,Gallery gallery) {
    this.loginView = loginView;
    this.gallery= tube;
    ((LoginView)loginView).addLoginListener(new LoginListener());
    ((Gallery )gallery).addUnfollowListener(new UnfollowListener());
    ((Gallery )gallery).addFollowListener(new FollowListener());
    ((Gallery )gallery).addCommentListener(new CommentListener());

}
class CommentListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
    //System.out.println(tube.getCommentText());
    //currentUser ????
    //Comment newComment = new Comment(currentUser,gallery.getCommentText());
    //Video currentVideo = getVideo();
    //currentVideo.addComment(newComment);

   }
}
class UnfollowListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        //unfollow situation
        //User unfollowedUser = gallery.unfollowSelectedUser();
    }
}
class FollowListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        //follow situation
        //User followedUser = gallery.getFollowedUser();
    }
}
class LoginListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {    
        String username = "";
        String password;
            try {
                username = loginView.getUsername();
                password = new String(loginView.getPassword());

                //login situation   
            } catch (IllegalArgumentException ex) {
                loginView.showError("Wrong username or password.");
            }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire