Win a copy of Terraform in Action this week in the Cloud 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

object initialization

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When an object is created, does it violate any general principles (such as Robert Martin's principles, or Craig Larman's patterns, or your favorite idioms) for the object to seek data from pre-existing objects in order to set its state?

An object can be given the data for its state, or it can get the data by itself. Usually I see the data for the state passed in the constructor. Is there anything wrong with the object having the responsibility to acquire the data on its own?

Regards, Marlene
[ October 23, 2004: Message edited by: Marlene Miller ]
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would the object seek the data inside the constructor, or outside the constructor?

There is a programming paradigm where "object creation is initialization". If you ensure that the object meets its intended invariant by the time the constructor exits, then you can program to ensure that the object always meets its invariant. This can help prevent very large classes of bugs. You can't use this paradigm if you use initializers outside the constructor.

Of course, there are other reasons why you can't always use that paradigm in Java, anyway.

If the object is seeking the data from within the constructor, that's a different issue. There the problem is that you can run into practical constraints, or in some cases issues about thread safety.
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dependency Injection comes close to what you mention. This technique is used by many popular frameworks like Spring, Pico Container, etc.

What it basically says is that the dependencies of a particular instance must be injected into the object from the creating class in order to provide maximum flexibility.

For example, if A depends on B, then B must be passed to A either as a parameter in its constructor or using a setB() method in A.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Would the object seek the data inside the constructor, or outside the constructor?


Outside

You can't use this paradigm if you use initializers outside the constructor.


okay

What it basically says is that the dependencies of a particular instance must be injected into the object from the creating class in order to provide maximum flexibility.


okay

Thank you, Warren and Sathya. That is exactly what I wanted to know.
[ October 24, 2004: Message edited by: Marlene Miller ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm playing with a system of persisted objects where new does not load the object. You can do stuff like:

Does that suck? I could maybe add a boolean to the constructor for load or not. Then the first becomes:

This code is nicely hidden in the Repository. Other users of MyFile are not expected to use load & save. Maybe that's a design smell, too!
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Stan.

file.load(); // create a new one from scratch
file.setContent(stuff); file.save();

Do I understand correctly, in your examples, the object effectively initializes itself?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. As an experiment I wanted to avoid getters for a while. More particularly I wanted to avoid:

Why should some other object be touching the data? Getting it from one place and putting it in another seems clunky. So I have:

File never gives up its ownership or reveals the implementation of its data. Like I say, this is an experiment in style. We'll see how it goes.

BTW: If you wonder about File "knowing" how it is stored, it doesn't. It uses an IDataStore interface with simple put(file), get(file), delete(file) APIs. I have implemented IDataStore in memory for unit testing and as flat files on disk. A database version would be a piece of cake. Configuration for the DataStoreFactory is "injected" so I can set the concrete datastore for each test and actually run my test suite twice - once for each datastore. Renderers for HTML, straight text, binary are also coded to an interface.
[ October 26, 2004: Message edited by: Stan James ]
 
Warren Dew
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stan James:



Looks like it's starting to get to be a swiss army knife there. A small one so far, but still....

I'd use a factory method for recovering an object from a persistent data store:

 
Sathya Srinivasan
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My suggestion would be to separate persistance from the business object. Do something similar to what Java does with Files.

Have a File object and have FileReader and FileWriter objects to do persistance.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have another question that is also about the consequences of an object going outside of itself to initialize its state.

This is as close as I could get using ASCII art to Figure 4.2 in Martin Fowler´┐Żs UML Distilled, 3rd Edition, page 55.

In a previous class diagram, Order has a state variable price, and OrderLine has state variables quantity and price.

I assume Order calls calculatePrice on one or more OrderLines and sums the return values to get a base value.

When Customer calls Order.getBaseValue, the base value calculated above must be available to Order as a state variable, not a local variable.

As I see it, calculatePrice sets (initializes) two state variables, price and baseValue.

Does it seem odd that in the middle of a method which sets a state variable (baseValue), another method is called that depends on that variable being set?
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seemed a bit odd at first that anOrder passes itself to getDiscountedValue instead of just passing base value, but I guess anOrder has no coupling to the algorithm used to compute discounted value. Customer may use any values it can get from the order to do the calculation and the order doesn't have to know anything about it.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. Thank you.
 
You showed up just in time for the waffles! And this 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