Note that there are some explanatory texts on larger screens.

plurals
  1. POAbstract design / patterns question
    text
    copied!<p>I had a bunch of objects which were responsible for their own construction (get properties from network message, then build). By construction I mean setting frame sizes, colours, that sort of thing, not literal object construction. </p> <p>The code got really bloated and messy when I started adding conditions to control the building algorithm, so I decided to separate the algorithm to into a "Builder" class, which essentially gets the properties of the object, works out what needs to be done and then applies the changes to the object.</p> <p>The advantage to having the builder algorithm separate is that I can wrap/decorate it, or override it completely. The object itself doesn't need to worry about how it is built, it just creates a <code>builder</code> and 'decorates' the <code>builder</code> with extra the functionality that it needs to get the job done.</p> <p>I am quite happy with this approach except for one thing... Because my <code>Builder</code> does not inherit from the object itself (object is large and I want run-time customisation), I have to expose a lot of internal properties of the object.</p> <p>It's like employing a builder to rebuild your house. He isn't a house himself but he needs access to the internal details, he can't do anything by looking through the windows. I don't want to open my house up to everyone, just the builder.</p> <p>I know objects are supposed to look after themselves, and in an ideal world my object (house) would build itself, but I am refactoring the <code>build</code> portion of this object only, and I need a way to apply building algorithms dynamically, and I hate opening up my objects with getters and setters just for the sake of the <code>Builder</code>.</p> <p>I should mention I'm working in Obj-C++ so lack <code>friend</code> classes or internal classes. If the explanation was too abstract I'd be happy to clarify with something a little more concrete. Mostly just looking for ideas or advice about what to do in this kind of situation.</p> <p>Cheers folks, Sam</p> <p>EDIT: is it a good approach to declare a </p> <pre><code>interface House(StuffTheBuilderNeedsAccessTo) </code></pre> <p>category inside <code>Builder.h</code> ? That way I suppose I could declare the properties the builder needs and put synthesizers inside House.mm. Nobody would have access to the properties unless they included the <code>Builder</code> header.... </p> <p>That's all I can think of!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload