• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Using factory method to call constructor versus calling constructor directly

 
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an exercise where I create bank branches, and customers of each branch. I have created two methods named newCustomer but only intend to use one of them: one calls the Customer constructor directly and the other calls the Customer.createCustomer method (which I created) to call the constructor (I believe this is called factory method). I prefer to call the createCustomer method because later if I decided to change something (don't know what as of now) I would just need to change the method of which I am calling and not affect the constructor.

Is one preferred over the other?  




 
Marshal
Posts: 71025
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Find yourself a copy of Effective Java by Joshua Bloch (page 5 in the 3rd edition). That tells you much more than I could tell you.
I am afraid neither of your implementations is ideal. In the first case the constructor should have private access, so you only create instances via the factory method (yes, you had that part right ). Once you do that I would buy the implementation as good. Do you actually want to start off the list of transactions with the opening balance on a new object? Why are you using doubles or Doubles, which are both unsuitable for precise arithmetic?
Neither of the methods in the second block is a factory method, and neither should therefore be called newCustomer. Call them addCustomerByName() or similar. Why don't you want two customers with the same name? Why have you got a method returning null? Why don't you write a contains() method? Is there a suitable method ready‑made in the List interface? (No, I don't think there is.) Obviously you can't persuade both versions of that method to compile together. Maybe you shouldn't call the class Branch because you can't tell whether that means branch of a bank or branch of a tree or branch of a railway line. Declare the Lists as type List not ArrayList. Don't repeat the actual type parameter: write new ArrayList<>();
 
bryant rob
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, I appreciate your feedback so much that I had to respond on almost everything you posted.

Campbell Ritchie wrote:Find yourself a copy of Effective Java by Joshua Bloch (page 5 in the 3rd edition). That tells you much more than I could tell you.


I have been reading a lot of good things about this book and put it on my "To get" list.

Campbell Ritchie wrote: In the first case the constructor should have private access, so you only create instances via the factory method (yes, you had that part right ). Once you do that I would buy the implementation as good.


I meant to change that and could kick myself for not doing so because I figured you would comment on that. But, I forgot to. As I am writing code I haven't quite gotten to the point where I know at first what should be public or private so I write my code. Then, I go back and analyze it to determine if I should make a method private. I try in the end to make as much private as possible especially my constructors.

Campbell Ritchie wrote:Do you actually want to start off the list of transactions with the opening balance on a new object? Why are you using doubles or Doubles, which are both unsuitable for precise arithmetic?


The way I see it is when a new customer opens an account they must have an initial amount(beginning balance) so that it is I agree with my code, but I am open to your suggestions here. As far as the double/Double, I realize that a double is not accurate for banking purposes and I should use a data type like big decimal but I haven't got to that point in java so I am using what I know.

Campbell Ritchie wrote:Neither of the methods in the second block is a factory method, and neither should therefore be called newCustomer. Call them addCustomerByName() or similar.


I like your method name better than mine. I have read about factory methods and I thought that by creating the createCustomer method and then calling the constructor, instead of calling the constructor directly I was in utilizing a factory method. So, I don't quite understand the difference but will go back and read some more. I just know that I would rather create a method to call a constructor rather than calling a constructor directly if possible. The main reason is that I believe I always want my constructor to be declared private so as to restrict its access.

Campbell Ritchie wrote:Why don't you write a contains() method? Is there a suitable method ready‑made in the List interface? (No, I don't think there is.)


I haven't got there yet. But, I am almost there. With my day job, I am a little behind on my studies but should be to the that section within a couple of weeks...hopefully sooner.

Campbell Ritchie wrote:Maybe you shouldn't call the class Branch because you can't tell whether that means branch of a bank or branch of a tree or branch of a railway line.


You are right here. I do plan on going back and changing some names especially since I am not a big fan of one name classes and methods. You did have me rolling on the floor laughing with this one...tree or railway line.

Campbell Ritchie wrote:Don't repeat the actual type parameter: write new ArrayList<>();


Thanks for this tidbit. I was wondering why IntelliJ skips over the <> and moves directly to the ();. Now I know, because it does not expect me to repeat the type parameter and type in <Double>. I have to laugh sometimes because when I start to think "how come IntelliJ does or doesn't do something like place my cursor in a specific position, I should know there obviously is a reason.
 
Marshal
Posts: 26106
72
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe you shouldn't call the class Branch because you can't tell whether that means branch of a bank or branch of a tree or branch of a railway line.



This rang a bell for me. For years I worked at a company which shipped products from several warehouses. In the old days a warehouse was called a Branch. Then later it was decided to call it a Division. And later still it was decided to call it a Distribution Centre. So now when you look at some code in the computer system, you can tell approximately when it was written by looking at the names of database columns which refer to warehouses.
 
bryant rob
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I definitely agree with Campbell and after his reply with regard to my "branch", I changed it to bankBranch. I like the idea of a 2 name variable. It helps me know what is what. But a lot of the code I read on the web that is actually code that is or has been in use is so hard to understand. I realize that I may not understand the flow of the code but when coders use letters and one word names I have to wonder how many people can understand it.

I am however still having a difficult time understanding what factory method is. It may be because I have not gotten far enough in my java studies but everything I read seems to be more advanced than where I am at.    
 
Paul Clapham
Marshal
Posts: 26106
72
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:But a lot of the code I read on the web that is actually code that is or has been in use is so hard to understand. I realize that I may not understand the flow of the code but when coders use letters and one word names I have to wonder how many people can understand it.



There's a name for that phenomenon: Sturgeon's Law. "Ninety percent of everything is crap." This applies twice as much on the web.
 
Paul Clapham
Marshal
Posts: 26106
72
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for the factory method: you would use that when you want to control how the user generates instances of a class. One example is the "singleton", where for some reason you only want one instance of the class to exist, so the factory method always returns the same instance. (This is now obsolete in Java, there are better ways of doing that, but it used to be common.) Or you might want to apply certain standard decorations to a newly-created instance before letting the user work with it; for example if you're designing a GUI then you might want your components to use a standardized font or have a standard background colour, so you could encapsulate those standards in a factory method.
 
bryant rob
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so does experienced coders have issues with reading other coders code? When it comes to names, Junilu and Campbell never fail to tell me something about my names...and I am grateful. So I am always thinking of good names even though I did miss the branch name on this app. I changed it to bankBranch. But, if others have a hard time with the 90% crap out there how do supervisors and managers allow it to continue? Do they have the mindset as long as the code works lets get it into production and get paid?
 
Paul Clapham
Marshal
Posts: 26106
72
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:Ok, so does experienced coders have issues with reading other coders code?



Oh yeah. There's a whole website The Daily WTF which celebrates that phenomenon.

But, if others have a hard time with the 90% crap out there how do supervisors and managers allow it to continue? Do they have the mindset as long as the code works lets get it into production and get paid?



I'm sure there's a lot of that mindset out there. Not to tar the whole software development field, but it does happen.
 
bryant rob
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for The DailyWTF. I am always interested in that kind of reading. Just didn't know it existed.
 
Sheriff
Posts: 15993
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Context is important. Expressive names are important if it helps the reader understand what's going on. For example, a variable like i can be totally acceptable if it's used idiomatically as an index variable of a for-loop that iterates over an array. However, if it's supposed to represent the maximum value found in a collection, then it's not the best name to use.

As for "branch," since you said this is a banking program, I doubt people will think that it's related to part of a tree or railroad. I mean when was the last time you were at the dentist to get your teeth cleaned and you thought that you were about to get strapped in and executed via electrocution when the hygienist asked you to get in the chair? In my opinion, the name bankBranch in a banking-related program is probably redundant.
 
Junilu Lacar
Sheriff
Posts: 15993
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To paraphrase (a quote often attributed to) Einstein: Make names as expressive as possible, but not expressive-er.

Here's an example of the kind of problem when you go overboard with specificity in names:

I call this "stuttering code."

I would argue that the following code gives the reader just as much context, without as much of the stutter:
 
Junilu Lacar
Sheriff
Posts: 15993
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I'd be concerned about is the assignment of responsibility for creating new customers to the Branch class. That seems conceptually dubious to me. I'm guessing the rationale behind that might be that since a customer has to go in to a branch to open up a bank account, it might seem logical to have a Branch class be the container for the newCustomer() or createCustomer() methods. That would smell to me like inappropriately modeling the real world in your program. This is not the point of object-orientation.

Certainly, relationships between classes in your program that are analogous to real-world relationships can help the reader more easily get some context around the logic. However, real world relationships aren't always appropriate models for software designs. A well-known illustration is the circle-ellipse or square-rectangle example.
 
Campbell Ritchie
Marshal
Posts: 71025
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:Campbell, I appreciate your feedback . . . .

Think nothing of it

I meant to change that.

Change things when they need changing not because you think I might notice, or somebody else

. . . I try in the end to make as much private as possible

Good idea. As recommended by Winston Gutkowski, one of our (inactive) mods.

especially my constructors. . . .

Look in the JLS (=Java® Language Specification) and find out what extra powers that gives you.

. . . when a new customer opens an account they must have an initial amount(beginning balance) . . .

In which case your business rules require that form of code.

a data type like big decimal . . . using what I know.  . . .

Look here for three links to old posts about BigDecimal.

. . . laughing with this one...tree or railway line. . . .

As long as you didn't laugh your arse off as well.

. . . I was wondering why IntelliJ skips over the <> . . .

Type inference, using the diamond operator, introduced in Java7. Java™ Tutorials section. Works first time in something like 99% of cases provided your desired type parameter is the same as that declared..
 
Saloon Keeper
Posts: 22783
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The full story, legend says, is that the statement was made that "90% of science fiction is crap". To which Sturgeon (a noted SF author) replied "90% of everything is crap". That's Sturgeon's law, and it's an accurate statement of something that statisticians have seen over and over again since the observation was first made about Renaissance(?) Italy: if you have two types of members in a population, 90% of them will belong to one (for example, being poor) and 10% of them will belong to the other (being rich). It's not so much that managers "let" so many employees be mediocre as it is that they don't have that many outstanding ones available. Especially given salary/compensation restrictions.

This 90/10 rule is so prevalent that it surprised me greatly that in US politics we've displayed more like a 60/40 divide in recent years. But then 100% of politics is crap.

Irrespective of that, however, I learned from IBM's OS/2 that if you have more than one mechanism to do something, you'll end up with a lot of grief. It's one thing to be able to create a new document via menu, hotkey, toolbar button, scripting and the like, but if they don't all call the same "create document" function, for example, ugh.
 
Campbell Ritchie
Marshal
Posts: 71025
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . . But then 100% of politics is crap. . . .

Only 100%?
 
Tim Holloway
Saloon Keeper
Posts: 22783
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, a politician would say 110% and a pundit would sat 130%.

But if you believe that kind of math, I have some prime riparine real estate I can sell you, complete with finished structures in a major metropolitan area.
 
Campbell Ritchie
Marshal
Posts: 71025
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a guaranteed supply of fresh water?

Maybe we should return to the original topic.
 
Bartender
Posts: 2732
133
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since we're talking about naming conventions, have a look at "Smurf Naming Convention" at https://blog.codinghorror.com/new-programming-jargon/
While I dont agree with all items there, but some of them are hilarious !!
 
Junilu Lacar
Sheriff
Posts: 15993
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:have a look at "Smurf Naming Convention" at https://blog.codinghorror.com/new-programming-jargon/


Today I learned that there's a name for the mess my team and I used to maintain. Every. Single. Class. And. Package.

I think Elmo from Sesame Street does that, too. He must be some kind of Red Mutant Smurf.
 
Tim Holloway
Saloon Keeper
Posts: 22783
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, that's great! I haven't seen one of these in a long time and the updates are wonderful. I worked on a Fear Driven Development team once. Worst employer I ever had.  Never heard the term "Egyptian Brackets" before, though.

I've worked with systems where Smurf naming was the only way to keep track of similar sets of functions that ran against different types of objects, and I suppose you could even consider it a variant of Hungarian Notation. But that was mainly when using Procedural programming languages. Smurf naming in an Object-Oriented environment should be considered suspect.
 
bryant rob
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:I am working on an exercise where I create bank branches, and customers of each branch. I have created two methods named newCustomer but only intend to use one of them: one calls the Customer constructor directly and the other calls the Customer.createCustomer method (which I created) to call the constructor (I believe this is called factory method). I prefer to call the createCustomer method because later if I decided to change something (don't know what as of now) I would just need to change the method of which I am calling and not affect the constructor.

Is one preferred over the other?  




 
bryant rob
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to edit the original post and totally forgot that I could not do this, and it created a new post today...sorry. (What was I thinking?? ) But, I wanted Campbell to see I changed the constructor access modifier to private.


Campbell,
After receiving my copy of Effective Java I have a couple of questions for you.

Campbell Ritchie wrote:Find yourself a copy of Effective Java by Joshua Bloch (page 5 in the 3rd edition). That tells you much more than I could tell you.
I am afraid neither of your implementations is ideal. In the first case the constructor should have private access, so you only create instances via the factory method (yes, you had that part right ). Neither of the methods in the second block is a factory method, and neither should therefore be called newCustomer. Call them addCustomerByName() or similar.


My createCustomer() method on Line 12 in the first block is the method that I was writing to be a factory method. According to the book this method would qualify as a factory method since it is a static method that returns an instance of the class. Am I correct with this?

With reference to the methods in the second block, you stated:

Campbell Ritchie wrote:and neither should therefore be called newCustomer. Call them addCustomerByName()


Why is this? Is it because I should not use the keyword new in a method name?

Lastly, for subclassing purposes, would it be safe to state that a constructor with protected access should be the access modifier to use?
 
Marshal
Posts: 7842
538
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cowgratulations, your post has been published in CodeRanch's August 2020 journal.
Staff note (Liutauras Vilda) :

@OP

 
bryant rob
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Liutauras.
 
Paul Clapham
Marshal
Posts: 26106
72
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

bryant rob wrote:With reference to the methods in the second block, you stated:

Campbell Ritchie wrote:and neither should therefore be called newCustomer. Call them addCustomerByName()


Why is this? Is it because I should not use the keyword new in a method name?



No, it's because a method does something, and so its name should be a verb or a verb phrase. So "addCustomerByName" is a verb phrase but "newCustomer" isn't.
 
Campbell Ritchie
Marshal
Posts: 71025
291
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't notice that question earlier which was posted whilst I was away. Another thing wrong with the name newCustomer() is that the method doesn't return a new Customer reference, so it doesn't tell us what it does.
 
bryant rob
Rancher
Posts: 115
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Paul and Campbell thanks for the reply. I will start paying closer attention to my method names. What about making my constructor protected accessible versus private?
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
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