How could I implement Command (Design Pattern) in this work?

Asked

Viewed 238 times

3

I’m doing a work in Java, where we should implement 3 Design Patterns from the software we created earlier.
The software I created is basically a CRUD for movies. In one tab, you add (Insert) and change (update) the records and in the other tab you have a view of the records as follows:

Visualizaçao dos registros
Parte de cadastro


As this work uses database, I’ve used two Design Patterns: Singleton (I read that it is very criticized, etc. but we are only using to learn it) and DAO (I studied it on the internet and managed to implement).
The professor, if I remember correctly, recommended Command, but I went to take a look at it and I couldn’t see a way to fit it into the code. How could I implement it here?

  • I’ll leave it to anyone who wants to comment because the answer would be too long. Strategy on your DAO, Command<Input, Output> to perform screen actions and Singleton as you said, to recover database connections for example.

  • I think one that would look good would be the MVC.

  • So, how would a Command work for the screen actions in this case? I’ll read on MVC and Strategy tbm.

  • 2

    The Swing has support to Action (implementation of Command Pattern). The classic example is to have an option in the Menu that does the same thing as a certain UI component (e.g., Update / Close, etc).

  • 4

    See that events and listeners are also implementations of Observer Pattern. Window containers and components follow the Composite Pattern. You probably used the Decorator Pattern not knowing when to decorate components with Scrolls... In fact, being absolutely lazy, you can justify that your work is ready simply by having used Swing hehehe.

  • Hmm got it. Thanks a lot for the answers, guys.

Show 1 more comment

1 answer

1


The softest to make is the Design Pattern Builder. For example, in the movie class would look something like this:

class Filme {

   private String nome;
   private Date duracao;
   ...


   /** Setters **/

   public Filme setNome(String nome) {
        this.nome = nome;
        return this;
   }

   public Filme setDuracao(Date duracao) {
        this.duracao = duracao;
        return this;
   }
   .....

}

Then in time to use it is something like this:

...
    Filme filme = new Filme().setNome("Terminator").setDuracao(duracao);
...

Or so:

...
    Filme filme = new Filme();
    filme.setNome("Terminator").setDuracao(duracao);
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.