File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes DTOs with public final fields Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "DTOs with public final fields" Watch "DTOs with public final fields" New topic
Author

DTOs with public final fields

Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

I just read
Why getter and setter methods are evil
and an interesting discussion on this article on TSS

Opinion

I am myself leaning a lot towards using public final variables for a small and simple subsystem. There are no methods and I only need to transfer data in the form of String(s). I could use a String[], but it is easier to read code if I use an object consisting only of public final variables instead.

To mark these objects as 'Data Structures' rather than true objects, I can keep them all in a separate package with a name that clearly indicates that these are not objects in the OO sense.

The use of these objects is limited to a small subsystem, and is not part of a public API that will be used by external systems, so refactoring them to promote these to 'real' objects if needed wouldn't be too hard.

It just seems like the simplest thing that will get the work done, at the moment. What do the ranchers think?

Sonny
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
In my opinion, if you like it, then it's all good.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

Thanks Lasse.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Which is better

A class with all static methods or a singelton for implementing utility function?


Groovy
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Pradeep Bhat:
A class with all static methods or a singelton for implementing utility function?

I usually start with static methods. Then, if I realize that a singleton would be better, I switch.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
if no business logic inside, a class with static methods is better.

if there are business logics, singleton seems a better choice.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Originally posted by Nicholas Cheung:
if no business logic inside, a class with static methods is better.

if there are business logics, singleton seems a better choice.

Nick


What is wrong in putting business logic in static method?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Then, if I realize that a singleton would be better, I switch.


what was the realization?
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

What is wrong in putting business logic in static method?

Each business object has its own business logic, which should not be stateless. If some processes can be stateless, like data value validation, you may consider to put it in utility class.

Nick
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

what was the realization?

By 6th sense.

Nick
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Nicholas Cheung:

If some processes can be stateless, like data value validation, you may consider to put it in utility class.

Nick


Nick in the above you agreed with having business in static :-). However in a service oriented architecture, you can have stateless business.

./pope


blog - InfoQ.com
Daniel Mayer
Ranch Hand

Joined: Sep 09, 2004
Posts: 103
Originally posted by Pradeep Bhat:
A class with all static methods or a singelton for implementing utility function?


Well, often it's best to put the method on a real object...
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

However in a service oriented architecture, you can have stateless business.

It depends on how your system design.

Personally, I vote for business logic stated with stateful objects.

Nick
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

What does this have to do with DTOs with public final fields :roll:
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

What does this have to do with DTOs with public final fields :roll:

Nothing.

I am response to the 4th message raise by Pradeep.

Nick
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Originally posted by Pradeep Bhat:
Which is better

A class with all static methods or a singelton for implementing utility function?



If you want to create only one object in all time, you should be use Singleton that it's best guideline or pattern.

But

If you want to use static method , you should be use with Utilities class.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
What is wrong in putting business logic in static method?


I think, Static method should be used in Utilities class that better..
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
What does this have to do with DTOs with public final fields


http://java.sun.com/blueprints/corej2eepatterns/Patterns/TransferObject.html
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Nicholas Cheung:

It depends on how your system design.

Personally, I vote for business logic stated with stateful objects.

Nick


I have to not agree with you this time. In an EJB session layer I would do my best to stay stateless ;-).

./pope
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8898

Personally, I vote for business logic stated with stateful objects.


I can have my state in singleton or as static fields in a class which has only static methods.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

I have to not agree with you this time. In an EJB session layer I would do my best to stay stateless ;-).

Sometimes can, while sometimes cannot. We certainly need to keep some states for some business operations, especally when we need to adopt the Session Facade pattern.

Nick
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

I can have my state in singleton or as static fields in a class which has only static methods.

As there is only 1 object, there is only 1 state you can hold.
If the method is accessed by many stateful clients, it is not that easy to maintain their states.

Even you can argue that, the object contains a Vector which holds all states of clients, but it messes up everything.

I feel holding stateful info for multiple objects in a singleton is much more difficult.

Nick
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
State in static is possible but require much attention in case of concurrent/multithreading environment.

./pope
Alexandru Popescu
Ranch Hand

Joined: Jul 12, 2004
Posts: 995
Originally posted by Nicholas Cheung:

Sometimes can, while sometimes cannot. We certainly need to keep some states for some business operations, especally when we need to adopt the Session Facade pattern.

Nick


How is involved session facades in your application that they require state? Does it mean it is a statefull session facade to other statefull sessions?

./pope
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DTOs with public final fields
 
Similar Threads
Executing exe using getRuntime.exec() through servlet
Pattrens Used is SCJP contents
resources for preparing section 1 and 5 in IBM XML Test 141
heap and stack
if anybody can shoe me the path for SCJD