This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Factory & Singleton Design Patterns: Building a Simple Form

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers! I posted a question about this a few days ago, but since I marked it as resolved prematurely I thought I might repost it. Here is the link to the previous post.

As I mentioned in the previous post, 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."

I have been struggling with a this a bit, but I rewrote the program following the example I had much closer, and I'm hoping someone can tell me if I'm on the right track at least.

Here's what I have:

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



SubmitButton.java file (I think I'm having the most trouble with this one)



FormFactoryDemo.java file



I am still working on it, but any hints or guidance would be much appreciated. It compiles at the moment but I get a null pointer exception in the main method of the FormFactoryDemo file.

Can anyone tell me if I am on the right track at least? Thanks to all for your input and advice in advance!


 
Ranch Hand
Posts: 64
Netbeans IDE Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

When I first started reading your post I thought the Factory would create some kind of JComponent that would be a form field and label, so when I got to public void getFormField () I was confused by it returning void. (When I see getSomething, I expect it to return Something.)

Then I read the getFormField implementation in Name and saw it was using a Scanner on System.in, I did a double take because I thought this was suppose to be a graphical and not a command line interface program.

FormFactory's getForm is a factory pattern, but I wonder if you meant to use equalsIgnoreCase based on the way you call it in FormFactoryDemo.java.

Does the program work the way you expect it to? Does it work the way your instructor expects it to? If so, I'll need to reset my expectations of using a factory pattern to create input forms that consist of a text label and an input field. If not, you may not be on the right path yet.

 
Dan Pants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Jacob Thanks for your input! If you saw my previous post, I first created a GUI program with the JFrame, but completely changed the program to fit the factory pattern and I must have followed the example I had too closely. And indeed the getFormField() methods should be something more like build or add form field. I guess I have some more work to do.

Thank you for the notes and advice!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic