This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Having trouble with strings

 
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be your objection to this?
 
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:What would be your objection to this?

       
       return tempOffice;
   }[/code]



Because

Are no longer in the Office.class but in the Address.class

I can't figure out how to combine

and

into
 
Carey Brown
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still don't see your objection. An Office has-an Address, so when you construct an Office you give it an Address that you've constructed, and now everything is part of the Office Object.
 
Marshal
Posts: 67464
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . .

Which class would you want that method in? Would you prefer a static method, like a factory method, to an instance method.
 
Carey Brown
Saloon Keeper
Posts: 6770
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Carey Brown wrote:. . .

Which class would you want that method in? Would you prefer a static method, like a factory method, to an instance method.


'static', like a factory method.
 
Ray Gilbert
Ranch Hand
Posts: 109
1
MyEclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey, that worked, somewhat in the DAO, however the formatting on the address was wrong when it populated the JTable, Still having trouble figuring out formatting. Also, the add/update office dialog no longer functions and the same type of adjustment doesn't work there. I think, though I have 17 parameters in the office constructor, it still functioned. Trying to limit that by adding a seperate Address.class, though it makes sense to me, it is beyond me in how to integrate that with the existing code.  
 
Sheriff
Posts: 14758
245
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When there are more than 4 items in a parameter list, it's time to refactor. When there are 17 then it's way past time.

Google for "refactor long parameter list"
 
Ranch Hand
Posts: 108
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Coming to the point mentioned earlier in the thread of many parameters in the constructor, To eliminate the possibility of assigning wrong values to the constructor because there are too many parameters you can refactor your Office class along this lines. You can either have the OfficeBuilder as a standalone class
or as the static inner class in the Office class.



And when you build your object you can write something like this




And you can get the values through the builder.
 
Marshal
Posts: 6643
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really like your implementation of the Builder pattern, especially having all the access to the field in the builder alone.  Still, there are a few improvements (or just my preferences) that I would make.
First, there's no need for a private no-arg constructor.  It's not used internally and it won't be available since there is a constructor with args.

Both the Office constructor and the getOfficeBuilder() method should be private.  

Next, I made the decision to use an OfficeBuilder constructor instead of a static office() method.  The main reason is so the user can have mandatory fields and default values to make the created Office object in a reasonable state.  I chose to make the ID field mandatory.

Your comment seems to indicate that the getters should be in the OfficeBuilder class, but I rather put them in the Office class itself, using the getOfficeBuilder() method to access the builder's fields.  This hides the internal structure of the class a bit better.

You would use my Office class like this:
 
I once met a man from Nantucket. He had a tiny ad
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!