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

  • SEARCH
  • Home
  • 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 3964738
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:22:15+00:00 2026-05-20T03:22:15+00:00

What is the proper (or a good) design if I need to have a

  • 0

What is the proper (or a good) design if I need to have a class that loads its data from a file (while keeping the load functions in a different class)?

This is what I have now. Can’t help but think there is a better way to structure it all though. The part that is tripping me up is when Loader has to call a method in Primary before continuing.

class Primary {
  public int x1, x2, x3;   //data that is read from file or calculated

  public void LoadPrimary {
    Loader L = new Loader();
    L.Load(this);   //pass self as parameter (this can't be the  best way though)
  }

  public void DoStuff() {
    x1++; x2--;
  }
}

class Loader {
  public void Load(Primary PrimToLoad) {
    PrimToLoad.x1 = 2; PrimToLoad.x2 = 4; 
    PrimToLoad.DoStuff();   //call a method in the calling class (better way for this?)
    PrimToLoad.x3 = 6;
  }
}

class Stub {
  public void SomeMethod() {
    Primary P = new Primary();
    P.LoadPrimary();
  }
}

In my real code I am using the Loader class to encapsulate a few different formats read from various sources (so there are multiple Load functions), otherwise I would just include the function in Primary and be done with it. Is there a way to have the Loader class return the Primary instead of void (where now it is passing a Param). It seems too “coupled” to be good design this way.

Any suggestions on a better way to accomplish this scenario? I assume it is pretty common but just don’t know enough about class design or terminology to find an answer on google/SO/etc (how do you search the dictionary for a word you can’t spell).

Updates/Notes
Both the answers (so far) point towards Factory pattern. Does that mean that for each Load method I have to have a separate class? Seems like overkill in my particular case. Doesn’t that also imply that my Stub class has to know/decide the format of a file (so it can call the correct factory class) instead of just letting the Primary class worry about it? Seems to be trading coupling for encapsulation.

Definitely know that I should be using properties (am actually) and an interface (am actually) but wanted to simplify it down for the question. Didn’t think about the injection aspect which is a good suggestion.

If anyone can update their answer to show how multiple load functions would work (please keep them simple) I will most likely accept. I am also considering moving the load functions back into the Primary class as an alternative solution.

  • 1 1 Answer
  • 1 View
  • 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-20T03:22:15+00:00Added an answer on May 20, 2026 at 3:22 am

    This looks like a pretty straightforward example of the Factory pattern. You don’t want the object being created to know about the factory though. So rip out LoadPrimary(), and do this instead:

    class Primary {
      public int x1, x2, x3;   //data that is read from file or calculated
    
      public void DoStuff() {
        x1++; x2--;
      }
    }
    
    public interface PrimaryFactory 
    {
        Primary Load();
    }
    
    public class FileTypeAPrimaryFactory {
    
        FileTypeAPrimaryFactory(File f) 
        {
           ...
        }
    
        public void Load() {
            var item = new Primary();
            item.x1 = 2; PrimToLoad.x2 = 4; 
            item.DoStuff(); 
            item.x3 = 6;
        }
    }
    
    public class FileTypeBPrimaryFactory {
    
        FileTypeBPrimaryFactory(File f) 
        {
           ...
        }
    
        public void Load() {
            var item = new Primary();
            item.x1 = 2; PrimToLoad.x2 = 4; 
            item.DoStuff(); 
            item.x3 = 6;
        }
    }
    
    class Stub {
      public void SomeMethod() {
        PrimaryFactory factory = PrimaryFactory(<get file>);
        Primary P = factory.Load();
      }
    
      public PrimaryFactory(File file) 
      { 
           if (<check file format>) return new FileTypeAPrimaryFactory(file);
           return new FileTypeBPrimaryFactory(file);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to MVC and am unsure about proper design. I have class objects
I need to design object that support some sort of uncertainty (or wild characters,
I am trying to come up with good design while implementing Store Kit. Trying
I have been asked to write a class that connects to a server, asynchronously
Possible Duplicate: Does anyone have a good Proper Case algorithm I am currently in
Good day, I found a lot of topics about that, but, without a proper
I have a database design problem which I have been researching for a while
What's the proper way to add a literal text value from a field to
This yields proper results, but I would like to have the resulting row in
I am trying to create a proper parent/child relationship within my data model. I

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.