Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Object Creation - Whose Responsibility

Object Creation - Whose Responsibility

Scheduled Pinned Locked Moved Design and Architecture
databaseregexhelpquestion
8 Posts 4 Posters 26 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    I have a class A whose instances contain a few class B's, and a few class C's. The objects are all constructed from database records, one record representing a single A, B or C. The objects are read from the database and created at application startup. The problem is that I can't decide between different instance creation strategies. -- Should I call a single method of class A, and have A construct all of its contained B's and C's? -- Should I have a separate factory class construct all three classes and set which objects are contained by which other objects? -- Or maybe a factory class that constructs A, and in turn A constructs its B's and C's? Which pattern is most preferred? Perhaps one I haven't mentioned?

    The difficult we do right away... ...the impossible takes slightly longer.

    L J 2 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      I have a class A whose instances contain a few class B's, and a few class C's. The objects are all constructed from database records, one record representing a single A, B or C. The objects are read from the database and created at application startup. The problem is that I can't decide between different instance creation strategies. -- Should I call a single method of class A, and have A construct all of its contained B's and C's? -- Should I have a separate factory class construct all three classes and set which objects are contained by which other objects? -- Or maybe a factory class that constructs A, and in turn A constructs its B's and C's? Which pattern is most preferred? Perhaps one I haven't mentioned?

      The difficult we do right away... ...the impossible takes slightly longer.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I tend towards "builder" classes: static classes with static methods that construct these "object graphs" (that reside in memory / data class / repository). And may also serialize and deserialize them. They often run async. Sometimes I build top down; sometimes bottom up (summing). "Adapters" let me step "outside the box" instead of thinking "how can I build 'myself'". I sort of patterned it after the .NET table adapter and the concept of the "fill" method (which in that case fills tables and data sets). (So: "option 2")

      "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

      Richard Andrew x64R A 2 Replies Last reply
      0
      • L Lost User

        I tend towards "builder" classes: static classes with static methods that construct these "object graphs" (that reside in memory / data class / repository). And may also serialize and deserialize them. They often run async. Sometimes I build top down; sometimes bottom up (summing). "Adapters" let me step "outside the box" instead of thinking "how can I build 'myself'". I sort of patterned it after the .NET table adapter and the concept of the "fill" method (which in that case fills tables and data sets). (So: "option 2")

        "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Thanks for the reply, Gerry. What's the theory behind your preference? Is there one, or do you just like to do it that way?

        The difficult we do right away... ...the impossible takes slightly longer.

        L 2 Replies Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Thanks for the reply, Gerry. What's the theory behind your preference? Is there one, or do you just like to do it that way?

          The difficult we do right away... ...the impossible takes slightly longer.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Some of the object graphs I build contain thousands of entries; and 4 levels deep. I'm wiring up a hierarchy. There is no container; the hierarchy is an illusion; what "level" should build the tree? An "adapter" builds the tree (a recursive structure of entities imported from a csv type text file; then serialized to binary).

          "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            I have a class A whose instances contain a few class B's, and a few class C's. The objects are all constructed from database records, one record representing a single A, B or C. The objects are read from the database and created at application startup. The problem is that I can't decide between different instance creation strategies. -- Should I call a single method of class A, and have A construct all of its contained B's and C's? -- Should I have a separate factory class construct all three classes and set which objects are contained by which other objects? -- Or maybe a factory class that constructs A, and in turn A constructs its B's and C's? Which pattern is most preferred? Perhaps one I haven't mentioned?

            The difficult we do right away... ...the impossible takes slightly longer.

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Richard Andrew x64 wrote:

            few class B's, and a few class C's...Which pattern is most preferred?

            Unless there is more information than what you posted then you are spending time on something that is not worth spending time on. Any way that you do it is going to be sufficient.

            Richard Andrew x64R 1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              Thanks for the reply, Gerry. What's the theory behind your preference? Is there one, or do you just like to do it that way?

              The difficult we do right away... ...the impossible takes slightly longer.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              I agree here; the static factory has the advantage that you can easily track and modify the way an object is created. Instead of modifying a lot of new This() and new That(A, B) statements, you have a BuildA(). And you can modify the behavior's of all BuildA's at a single point. To make a short story longer, a practical example is adding a logger in a factory; A short tale on two patterns that live in .NET[^] :rolleyes:

              Bastard Programmer from Hell :suss: "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              1 Reply Last reply
              0
              • J jschell

                Richard Andrew x64 wrote:

                few class B's, and a few class C's...Which pattern is most preferred?

                Unless there is more information than what you posted then you are spending time on something that is not worth spending time on. Any way that you do it is going to be sufficient.

                Richard Andrew x64R Offline
                Richard Andrew x64R Offline
                Richard Andrew x64
                wrote on last edited by
                #7

                jschell wrote:

                you are spending time on something that is not worth spending time on

                I understand this point. One solution I told myself was to pick the way that I think I can do it with the fewest difficulties. I thought I would post the question because it is one that I have quite often, and I'm wondering if there's a best practice. So the question is not really a waste of time.

                The difficult we do right away... ...the impossible takes slightly longer.

                1 Reply Last reply
                0
                • L Lost User

                  I tend towards "builder" classes: static classes with static methods that construct these "object graphs" (that reside in memory / data class / repository). And may also serialize and deserialize them. They often run async. Sometimes I build top down; sometimes bottom up (summing). "Adapters" let me step "outside the box" instead of thinking "how can I build 'myself'". I sort of patterned it after the .NET table adapter and the concept of the "fill" method (which in that case fills tables and data sets). (So: "option 2")

                  "Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I

                  A Offline
                  A Offline
                  asghar mohebi
                  wrote on last edited by
                  #8

                  beauty center in Iran with baroro https://baroro.ir

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups