Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 662557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:23:30+00:00 2026-05-13T23:23:30+00:00

This is a design question. I’m trying to decide between 2 implementations. In order

  • 0

This is a design question. I’m trying to decide between 2 implementations.

In order to properly explain this I need an example. So, for the sake of example:

I have a class that generates a report about, let’s say, certain Stock Market values on a specific day. I create a StockMarketReportGenerator object, passing it today’s date, and it generates a report based on today’s market values.

The StockMarketReportGenerator “has a” StockMarketData object. The purpose of the StockMarketData object is to contain all the stock market values that are stored in a table (probably called StockMarket 🙂 ) in the database, and some further values calculated from the table data. It has private methods that connect with the database, retrieve the data, do the necessary calculations, and store the final values in the object’s member variables. (It then has getter methods to expose these values, but no setter methods.) The StockMarketData class is basically a “holder” for stock market data values. I have one central function called something like “calculateStockMarketData()” that calls all these private helper methods and sets up the object. (I am aware that all this processing could really be more easily handled by a framework such as Hibernate; but the decision was made to do it manually as it’s a very small, somewhat temporary project and not worth the setup.)

My question is this – from my ReportGenerator class, I only need the StockMarketData object in order to access it’s properties /
member variables – post-processing and post-calculations. Which means that really, I want to get the object pre-filled
with data. And so I keep the calculateStockMarketData method private and call it automatically from the StockMarketData
constructor. But I’m feeling somewhat uneasy about doing all my processing in a constructor and then not having any public methods. Is this a design flaw? Or is it really the most logical way to do this? Basically, which of the following 2 implementations is better?

1) (My current implementation) Make the central calculateStockMarketData() method private and call it from the StockMarketData method constructor (passing today’s date), so that whenever you have a StockMarketData object, it’s already filled. So all I would need from the ReportGenerator class before I start using the object properties is the line:

StockMarketData myData = new StockMarketData(someDate);

2) Make the central calculateStockMarketData() method public, so that in order to set up a StockMarketData object you need to explicitly call the method. So from the ReportGenerator class I would code:

StockMarketData myData = new StockMarketData(someDate);
myData.calculateStockMarketData();

The first strikes me as the better design, especially since there is then no possibility of using the object properties before they are initialized. But I am also unsure about the standards regarding executing a great deal of code from a constructor… Which should I choose?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:23:30+00:00Added an answer on May 13, 2026 at 11:23 pm

    I would go with number 2, especially if there is a possibility of adding methods to the class that don’t rely on that data being there.

    On the other hand, if you would consider the class to be in an invalid state without the data populated, you should do it on construction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a design question: when do I need to create/use a static method
This is more of a design question. I have a template class, and I
This is more of a general design question I guess... I have an Ajax
This is probably more a design or style question: I have just been considering
This is a design/algorithm question. Here's the outline of my scenario: I have a
this is a CSS / design question. I have three textboxes that i want
I'm struggling since a few hours with this design question. In Cocoa for example,
First of all, this is a design question rather than an issue: I have
This is a technical design question more then a syntax question. I have a
This is a SQL design question. First, the setup. I have three tables: A,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.