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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:17:58+00:00 2026-05-15T05:17:58+00:00

If I have a class that holds one or several lists, is it better

  • 0

If I have a class that holds one or several lists, is it better to allow other classes to fetch those lists (with a getter)? Or to implement a doXyzList/eachXyzList type method for that list, passing a function and call that function on each element of the list contained by that object?

I wrote a program that did a ton of this and I hated passing around all these lists, sometimes with method in class A calling method in class B to return lists contained in class C. B contains a C or multiple C‘s.

(note this question is about dynamically typed OO languages languages like ruby or smalltalk)

ex. (that came up in my program):

on a Person class containing scheduling preferences and a scheduler class needing to access them.

  • 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-15T05:17:59+00:00Added an answer on May 15, 2026 at 5:17 am

    There is no single answer – design is about juggling priorities, tradeoffs and comprimises, to arrive at something that works well in your sitluation. I’ll briefly cover the relative merits and drawbacks of using functors, accessors and full encapsulation.

    Functors

    Using functors can be convenient, and avoids boilerplate iteration. This also allows you to cleanly separate what you are executing for each item in the list from when you execute it. With a for-each loop, the two are most often coupled together. Where functors don’t work is if you need to perform an operation on multiple lists, either from the same object, or from multiple objects, or if you only need a few elements of the list. Use of functors constrains execution order – items must be used in the order iterated by the provider. The functor has no control. This can be a blessing, and also a curse.

    The example of Person, scheduling preferences and a Scheduler, the scheduler could provide an external iterator for possible schedule times:

       schedules = scheduler.schedules(person.getSchedulePreferred())
    

    The getSchedulePreferred() returns a predicate that selects the schedules from all those available that are preferred by the given person.

    Iterating across all schedules may not be the most efficient way of implementing this. Say, if the person only wants schedules in June, then all schedules for the rest of the year will be wastefully iterated.

    Accessors

    Making the lists available via gtters can be beneficial when implementing functionality that is not intrinsic to the class. For example, given two Orders, find the items that they have in common. This simple to implement if the lists are provided as getters for external traversal, and very simple if the lists are provide in some known order (e.g. if the Order has a getSortedItems() method.) The complexity here is managing mutability of the list, although many languages have direct support to disable mutation (e.g. const in C++) or wrapping the list in an immutable wrapper.

    For the example, the person could expose the list of schedule preferences, which are then used directly by the scheduler. The scheduler has the opportunity to be “smart” about how the preferences are applied, e.g. it could build a query to a datbase to fetch matching schedules based on the persons preferences.

    Encpasulation

    The third alternative is to question if external access is required. It’s one symptom of an anemic domain model that objects are all properties and no behaviour. But don’t strive to put behaviour in a domain object just to avoid this anti-pattern – the behavior should be a natural part of that object’s responsibility.

    I don’t think this applies here – person, scheduler and scheduling preference clearly fulfill different roles and have clear responsibilities. Adding a method on one entity that tries to compute data from another would be an unnecessary tight coupling.

    Summary

    In this particular case, my preference is for the getter, since it allows the scheduler more control over how the schedule preferences are used, rather than being “force-fed” them through a functor.

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

Sidebar

Related Questions

I have a class includes.vb that holds some variables (sharing them with other pages)
I have an abstract base class that holds a Dictionary. I'd like inherited classes
my problem is a simple one. I have a class template that holds a
I have a class that holds another objects inside it (List, Set and objects
I have a class Team that holds a generic list: [DataContract(Name = TeamDTO, IsReference
I have a private class variable that holds a collection of order items: Private
I have a class in C++ that I can't modify. However, that class holds
I have a simple class that essentially just holds some values. I have overridden
I have this small class called City that simply holds some information about a
I have a specialized list that holds items of type IThing : public class

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.