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 3855964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:51:26+00:00 2026-05-19T17:51:26+00:00

Why would I want to make all my search/get functions that retrieve rows from

  • 0

Why would I want to make all my search/get functions that retrieve rows from a database, instance methods? If I have a getByID(id) or a findPeople(person attributes) function, that either returns object(s) or throws an exception, what justification is there for the overhead of creating an instance of this class every time I want to call one of these methods?

I’ve heard it makes things more testable, but in this case I don’t see why — the instance methods don’t really interact with the other properties of the class at all.

  • 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-19T17:51:26+00:00Added an answer on May 19, 2026 at 5:51 pm

    Well, the question is, how do you want it to work… There are a few patterns built around this concept.

    If you want to get one:

    If you have many similar things (like types of people), then you might want an Abstract Factory. In that case, you’d likely not want it to be static. But then again, you’d want an instance so you can bind your individual factories to the abstract factory. That way, you’d pass around a “Person Builder” factory. Then, when you’re looking up an individual, you’d call builder.buildPerson(id). That method would look up the individual, and determine the class to actually instantiate, and call the respective factory.

    If there’s only one type of “person”, then I’d use a Factory method. In that case, since the class (and its children) are responsible for instantiation, static methods are the preferred way. So you’d then call person::getPerson(id).

    If you want to get many:

    If you want to get many people (Such as with a findPeople method), then the ultimate solution would likely be dependent upon your need.

    If you need some kind of efficiency with object creation, then you’re likely looking for a Flyweight pattern.

    Otherwise, if you’re using an abstract factory, then make an instance method to find multiple based upon attributes. If you’re using a factory method, then add another static method to find them.

    But another way of looking at it

    Is that “storing” and “loading” of the data for an object is not related to the object itself, so it does not belong as a method (static or not). In that case, it would be better to have a model that represents the datastore. Then, to get a list of users, you’d call peoplemodel.getPerson(id). The peoplemodel would fetch against the DB, and load the information necessary to construct the object(s). Then, it would call the factory on the person class to construct the actual object, and return it.

    This is nice, because it’s separating the storage from the implementation. Sure, it’s another layer, but the extra layer lets you do things like have multiple datastores, or use the same person class for multiple applications with different storage requirements (since all the class cares about is the data passed in).

    So, in conclusion:

    Now, you can’t achieve loose coupling of separate components via static methods, so in this case you’d need to use instances on both sides. So you’d pass in a builder to the model (dependency injection) to create the people objects. And since the model itself is loose-coupled, you’d get an instance of that and pass it where you need to load people.

    So in short, it depends on what you’re trying to do. But if you want the loosest coupled code (most reusable and most maintainable), then stay away from static methods and stick to abstract factories/builders and DI…

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

Sidebar

Related Questions

I have a special url which I would want only few people to have
I have some utility functions like: void myVibratePhone() { AudioServicesPlaySystemSound (kSystemSoundID_Vibrate) ; } that
I have a bunch of keys that each have an unlikeliness variable. I want
I want to make a simple game on python, all text based. Sort of
Have a question about full text search. I have a query that works, but
I would want to do something like: >>> lst = [1, 2, 3, 4,
Why you would want to use a switch block over a series of if
Can anyone explain to me why I would want to use IList over List
Why would someone want to use a linked-list over an array? Coding a linked-list
Why would I want to use PHP's filter library? Why wouldn't I? It seems

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.