• Post Reply Bookmark Topic Watch Topic
  • New Topic

Factory Design Pattern with Swing JDialog

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question regarding the topic. Is it a good idea to use the factory design pattern for say if I needed to create four different JDialogs for the same parent frame? Would your interface be similar to this?


factory design interface


One of the four JDialog class would look something like this without the comments.


Of course you would have your factory class
 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any reason to use such a pattern. When you want a new dialog of some type, just create a new dialog. A factory pattern is mostly useful if you're going to create many many instances of the same type, that are all just slightly different. For dialogs, Swing has the JOptionPane class that makes stuff like that more easy.

Note that the design you made is flawed. Factories are not of the same type as the types they produce. Your interface should be named DialogFactory, it should contain a method createDialog() that creates a new Dialog, and it should have methods that influence what the created dialog will look like or how it will behave. Your current Dialog interface is not very useful, because no class cares about the initComponents() method except the Dialog itself, and the getInstanceOf() method is an instance method (you need a Dialog to be able to get an instance of Dialog?) that doesn't actually even return anything.

Before you design something, first ask yourself what problem it's going to solve.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I don't see any reason to use such a pattern. When you want a new dialog of some type, just create a new dialog. A factory pattern is mostly useful if you're going to create many many instances of the same type, that are all just slightly different. For dialogs, Swing has the JOptionPane class that makes stuff like that more easy.

Note that the design you made is flawed. Factories are not of the same type as the types they produce. Your interface should be named DialogFactory, it should contain a method createDialog() that creates a new Dialog, and it should have methods that influence what the created dialog will look like or how it will behave. Your current Dialog interface is not very useful, because no class cares about the initComponents() method except the Dialog itself, and the getInstanceOf() method is an instance method (you need a Dialog to be able to get an instance of Dialog?) that doesn't actually even return anything.

Before you design something, first ask yourself what problem it's going to solve.


Thank You
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!