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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:59:52+00:00 2026-05-17T19:59:52+00:00

I have a class that represents a pretty complex object. The objects can be

  • 0

I have a class that represents a pretty complex object. The objects can be created by many ways: incremental building, by parsing text strings in different formats and by analyzing binary files. So far my strategy was as follows:

  • Have the constructor (__init__, in my case) initialize all the internal variables to None

  • Supply different member functions to populate the object

  • Have those functions return the new, modified object to the caller so we can do sd = SuperDuper().fromString(s)

For example:

class SuperDuper:
    def __init__(self):
        self.var1 = None
        self.var2 = None
        self.varN = None

    ## Generators
    def fromStringFormat1(self, s):
        #parse the string
        return self 
    def fromStringFormat2(self, s):
        #parse the string
        return self
    def fromAnotherLogic(self, *params):
        #parse params
        return self
    ## Modifiers (for incremental work)
    def addThis(self, p):
        pass
    def addThat(self, p):
        pass
    def removeTheOtherOne(self, p):
        pass

The problem is that the class becomes very huge. Unfotunately I am not familiar with OOP pattern designs, but I assume that there is a more ellegant solution for this problem. Is taking the generator functions out of the class (so that fromString(self, s) becomes superDuperFromString(s) a good idea?

  • 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-17T19:59:53+00:00Added an answer on May 17, 2026 at 7:59 pm

    What might be a better idea in your case is dependency injection and inversion of control. The idea is to create another class that has all of the settings that you are parsing out of all of these different sources. Then subclasses can define the method to actually parse it. Then when you instantiate the class, pass an instance of the settings class to it:

    class Settings(object):
        var1 = None
        var2 = None
        var3 = None
    
        def configure_superduper(self, superduper):
            superduper.var1 = self.var1
            # etc
    
    class FromString(Settings):
        def __init__(self, string):
            #parse strings and set var1, etc.
    
    class SuperDuper(object):
        def __init__(self, settings): # dependency injection  
            settings.configure_superduper(self)  # inversion of control
            # other initialization stuff
    
    sup = SuperDuper(object, FromString(some_string))
    

    Doing it this way has the advantage of adhering more closely to the single responsibility principle which says that a class should only have one (likely to occur) reason to change. If you change the way you’re storing any of these strings, then the class has to change. Here, we’re isolating that into one simple, separate class for each source of data.

    If on the other hand, you think that the data that’s being stored is more likely to change than the way it’s stored, you might want to go with class methods as Ignacio is suggesting because this is (slightly) more complicated and doesn’t really buy you much in that case because when that happens you have to change two classes in this scheme. Of course it doesn’t really hurt much either because you’ll only have to change one more assignment.

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

Sidebar

Related Questions

I have a base class that represents a database test in TestNG, and I
I have a data structure that represents C# code like this: class Namespace: string
I have a string variable that represents the name of a custom class. Example:
Let's say I have a table that represents a super class, students . And
I have class method that returns a list of employees that I can iterate
I have a class that map objects to objects, but unlike dictionary it maps
I have a class that creates several IDisposable objects, all of these objects are
I have a class that I want to use to store properties for another
I have a class that I wish to expose as a remote service using
I have a class that looks like this public class SomeClass { public SomeChildClass[]

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.