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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:03:04+00:00 2026-06-17T08:03:04+00:00

Now and again I end up with code along these lines, where I create

  • 0

Now and again I end up with code along these lines, where I create some objects then loop through them to initialise some properties using another class…

ThingRepository thingRepos      = new ThingRepository();
GizmoProcessor  gizmoProcessor  = new GizmoProcessor();
WidgetProcessor widgetProcessor = new WidgetProcessor();

public List<Thing> GetThings(DateTime date)
{
    List<Thing> allThings = thingRepos.FetchThings();

    // Loops through setting thing.Gizmo to a new Gizmo
    gizmoProcessor.AddGizmosToThings(allThings);

    // Loops through setting thing.Widget to a new Widget
    widgetProcessor.AddWidgetsToThings(allThings);

    return allThings;
}

…which just, well, feels wrong.

  1. Is this a bad idea?
  2. Is there a name of an anti-pattern that I’m using here?
  3. What are the alternatives?

Edit: assume that both GizmoProcessor and WidgetProcessor have to go off and do some calculation, and get some extra data from other tables. They’re not just data stored in a repository. They’re creating new Gizmos and Widgets based on each Thing and assigning them to Thing‘s properties.

The reason this feels odd to me is that Thing isn’t an autonomous object; it can’t create itself and child objects. It’s requiring higher-up code to create a fully finished object. I’m not sure if that’s a bad thing or not!

  • 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-06-17T08:03:05+00:00Added an answer on June 17, 2026 at 8:03 am

    If you have complex intialization then you could use a Strategy pattern. Here is a quick overview adapted from this strategy pattern overview

    Create a strategy interface to abstract the intialization

    public interface IThingInitializationStrategy
    {
      void Initialize(Thing thing);
    } 
    

    The initialization implementation that can be used by the strategy

    public class GizmosInitialization
    {
      public void Initialize(Thing thing)
      {
         // Add gizmos here and other initialization
      }
    }
    
    public class WidgetsInitialization
    {
      public void Initialize(Thing thing)
      {
        // Add widgets here and other initialization
      }
    }
    

    And finally a service class that accepts the strategy implementation in an abstract way

    internal class ThingInitalizationService
    {
      private readonly IThingInitializationStrategy _initStrategy;
    
      public ThingInitalizationService(IThingInitializationStrategy initStrategy)
      {
         _initStrategy = initStrategy;
      }
    
      public Initialize(Thing thing)
      {
        _initStrategy.Initialize(thing);
      }
    }
    

    You can then use the initialization strategies like so

    var initializationStrategy = new GizmosInitializtion();
    var initializationService = new ThingInitalizationService(initializationStrategy);
    
    
    List<Thing> allThings = thingRepos.FetchThings();
    
    allThings.Foreach ( thing => initializationService.Initialize(thing) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Code: catch (IOException e) { LOGGER.error(IOException exception happened); //now need throw again the same
I want to use a clause along the lines of CASE WHEN ... THEN
Every now and again, I need to start the Django development server, and have
I'm running Django on Linux using fcgi and Lighttpd. Every now and again (about
Hi Again Masters Of The Web :) Now, I have got a new stupid
Once again I'm receiving structs via UDP from a C++ programm, Now I ported
Again, I was creating buttons dynamically based on this post and now I need
i want to reload ViewdidLoad in a method But now want to call again
I'm back. Again. :3 Right now, I'm working on my RPG project (just for
I had some code to move the control around, but when you reached the

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.