{"id":945,"date":"2009-08-13T16:00:34","date_gmt":"2009-08-14T00:00:34","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=945"},"modified":"2009-07-19T20:28:58","modified_gmt":"2009-07-20T04:28:58","slug":"component-pattern","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/component-pattern\/","title":{"rendered":"Component pattern"},"content":{"rendered":"<p>Sometimes, your object hierarchy is determined by the kinds of data you're throwing around - if you have a pile of pieces of information about each customer in a database, it's obvious that you should have a Customer object. But if this is the only rule you use for deciding when to make a class, you'll often end up with enormous classes. This tends to happen for the object representing the program itself. A program tends to have a lot of things that there are only one of, and if you stick all these things in one big object then its unwieldy.<\/p>\n<p>So you break up your Singleton object into components. You can do this in whatever way makes most logical sense for your program - perhaps a set of routines and data for allocating memory go in one object, and a set of routines and data for dealing with the program's main window go in another. Then you need a master object responsible for wiring all these components together. The construction of this object may be somewhat non-trivial:<\/p>\n<p>In the master constructor, each of the components is constructed. However, we don't tell the components about the master yet because it's constructor is incomplete.<\/p>\n<p>Once the constructor is complete, we need to loop through all the components again and \"site them\" (give them a pointer to the master that they can use to obtain pointers to other components they need).<\/p>\n<p>Depending on how complex the interdependencies between components are, more initialization steps may be needed. The order of the components may also make a difference.<\/p>\n<p>This doesn't have to be used for just the singleton - any sufficiently complex object can benefit from being broken up this way.<\/p>\n<p>I looked for this pattern in the literature but didn't see it anywhere. However, it seems to be a reasonably common thing to do so I'm recording it here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, your object hierarchy is determined by the kinds of data you're throwing around - if you have a pile of pieces of information about each customer in a database, it's obvious that you should have a Customer object. But if this is the only rule you use for deciding when to make a class, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-945","post","type-post","status-publish","format-standard","hentry","category-computer"],"_links":{"self":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/comments?post=945"}],"version-history":[{"count":1,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/945\/revisions"}],"predecessor-version":[{"id":946,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/945\/revisions\/946"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}