• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Factory and Singleton Design Patterns to Create a Simple Form  RSS feed

 
Dan Pants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all! This question might be asking to much of the code ranch but I honestly do not know where to begin with this one.

My assignment was to create a simple form that demonstrates the use of the factory and singleton design patterns. "Use the Factory pattern to ensure that each form input consists of a text label and a textfield. Use the Singleton pattern for the submit button. When the submit button is clicked, a pop-up should show all the information that was typed into all of the form fields."

I used JFrame to create the form without the design patterns and I although I get the desired result, I'm not quite sure how I can integrate the design patterns into the code I wrote. The example I have to go off uses shapes, not text fields so I think that's why I'm not quite clear on how to approach this.

Any hints to get me started in the right direction would be much appreciated. Thank you!

Here's my code so far:

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Pants wrote:Hi all! This question might be asking to much of the code ranch but I honestly do not know where to begin with this one.
My assignment was to create a simple form that demonstrates the use of the factory and singleton design patterns. "Use the Factory pattern to ensure that each form input consists of a text label and a textfield. Use the Singleton pattern for the submit button.

Hi Dan,

Not quite sure what you want here, and I also notice that the thread has been marked as "resolved", but here goes:

1. Do you know what the Factory and Singleton patterns are? Can you describe them?
2. Can you explain how they might be used in such an application? I have to admit being somewhat mystified as to why you might want to implement a submit Button as a Singleton; but I'm certainly no GUI expert, and I'm sure your tutors have their reasons.

So far, what I see is a fairly standard GUI program, so perhaps you could explain where you've tried to implement the patterns you've been asked to.

Winston
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Pants wrote:This question might be asking to much of the code ranch but I honestly do not know where to begin with this one.

Unless you are asking us to find the cure to cancer, we like to think that very few things are too much to ask of the CodeRanch

When the submit button is clicked, a pop-up should show all the information that was typed into all of the form fields.

I don't want to confuse matters more for you but this doesn't exactly seem like a proper use of singleton to me.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lot of people here think singleton is an anti‑pattern.

As Winston suggests, you would do well to write out what you know about factory and singleton. That will focus your thoughts and enable you to work things out for yourself. There is a name for that sort of programming. There is even a picture of it on Wikipedia.
 
Dan Pants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all! Thanks for the replies. I marked this as resolved because I thought I'd give it another go on my own. I scrapped all the code I wrote and started from scratch, this time trying to closely follow factory design pattern template in my textbook.

These design patterns are basically "best practices" and by learning these patterns, inexperienced programmers such as myself can learn software design more easily. I know the Factory design pattern is an ideal way to create objects in java. It uses a common interface to create its new objects. The singleton pattern is an ideal way to create a single object and to ensure only one instance of that single object is created.

@ Junilu After re-reading my question, it looked like I was asking you all "Here's my code, now re-write it for me this way." That's what i meant by asking too much, so I marked it resolved and tried it on my own. I think the way it was explained to me (using shapes instead of text fields and demonstrated in Greenfoot (a java learning environment) and not in an IDE) I was a bit intimidated about getting started so I just wrote the program the way I knew how.

@ Winston & Junilu I admit, the more I read, it seems like Singleton submit button is not the way to go, but this is what I was asked for. However, my mention that "When the submit button is clicked, a pop-up should show all the information that was typed into all of the form fields." was not part of the assignment, just something I thought would be helpful for me for testing purposes.

Still working on the singleton submit button but here's what I have so far:

Form.java file




Name.java file (I have a similar files just like this for Address.java, City.java, State.java, Zip.java and Phone.java)



FormFactory.java file



FormFactoryDemo.java file


As I said I am still working on it, but because you guys were nice enough to respond I thought I'd post what I have presently. It compiles at the moment but I get a null pointer exception in the main method of the FormFactoryDemo file. Need to add that singleton design pattern submit button ASAP so I can work through this.

Can you tell me if I am on the right track at least?? Thanks to all for your input and advice it was extremely helpful!

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