views:

101

answers:

6

Not sure if the title captures what I'm trying to say here.

When designing in OO should I be splitting my objects up into their most specific areas - so if I have a factory object that deals with creating objects but later on i come across a way of creating objects for another purpose even though they may be the same objects is it worth creating a seperate fcatory or just add to the exsiting.

My biggest worry is bulking up classes with tons of stuff, or splitting objects and diluting my projects into a sea of classes.

Any help?

EDIT:

I guess on a side note/sub topic part of me wants to find out the level of granularity you should use in a program. Kind of, how low should you go?

+1  A: 

Finding the correct level of detail and responsibility is what makes OOP design so difficult. We can help you with a specific case but not with anything this general. If there were algorithms or strict methodologies of how to solve this, everyone could be an OOP designer.

dark_charlie
this answer is kind of what i was looking for..it makes me feel better about what i'm doing. Nice to know i'm not missing something and yes it is hard!
Franco
It really is just experience. When you do something that you later decide was too complex/unmaintainable/had bad "code smells" take a moment to reflect on why that is and what caused it to get like that.
awoodland
But then again, OOP is no different from other engineering disciplines; after all the bridge designers must decide the correct level of abstraction. And by far, they are much more successful in in defining requirements and specifications than most software engineers. This might be a point worth noting, and to investigate.
Schedler
The actual code execution is no different from other engineering disciplines, but the proper planning of abstraction must take into account the ability of humans to understand the design, not just machines. The requirements and specifications acquired by engineers is more precise because their domain is (generally) well defined and entrenched in physical concerns and not linguistic and symatic issues.
wllmsaccnt
+1  A: 

A rule of thumb I like for deciding "is this getting too big now?" is "can I explain the purpose of it concisely?" If you start having to introduce caveats and lots of weasel words to explain the functions of a component of your design (be it class, member variable, method or whatever) it might be a good indicator that it's getting too complex and should be split up.

awoodland
As an added bonus it gives you the first sentence of javadoc or whatever documentation system you're using for free!
awoodland
+1  A: 

In your specific case, if you already have a factory object then the DRY Principle (Don't Repeat Yourself) would say that it's a bad idea to create another factory that does the same thing.

Is this an actual problem that you face? Or merely a fear about how your code might grow in the future?

Anon
I might have worded things badly in the Q. I guess I wouldnt be repeating myself its just more of a case of where to put the code, it could be added to an exsiting factory that creates design objects for exporing data to excel spreadsheets. On the other hand I could see it could also have its own factory for importing excel data. Both factories would produce the same objects but the inner workings are completely different.
Franco
@Franco - from your comment, it sounds like the objects that those factories create are the real problem. If you're not already doing it, I recommend coming up with a set of "narrow" interfaces that describe the functionality you want. From an implementation perspective, you can have the same object implement multiple interfaces (although there's rarely a need for that).
Anon
Would this be a good idea considering this factory will likely be the only factory of its kind and no other objects will ever use the interfaces.
Franco
A: 

If you are using the same type of object to solve drastically different problems then you may need to redesign the class to focus on seperation of concerns. If you need a more specific answer, you will need to provide an example of a type of class that would need this functionality.


I might have worded things badly in the Q. I guess I wouldnt be repeating myself its just more of a case of where to put the code, it could be added to an exsiting factory that creates design objects for exporing data to excel spreadsheets. On the other hand I could see it could also have its own factory for importing excel data. Both factories would produce the same objects but the inner workings are completely different. –

If you aren't doing or plan on doing any class abstraction (subclassing or using interfaces) you may not need to use the factory pattern at all. The factory pattern is generally best suited for supplying objects of a base class type or that implement a specific interface.

wllmsaccnt
+4  A: 

My biggest worry is bulking up classes with tons of stuff, or splitting objects and diluting my projects into a sea of classes

This is a very valid point and in any even reasonably sized project, extremely difficult to get right up front especially because realistically, requirements themselves evolve over time in most cases. This is where "Refactoring" come in. You design based on what you know at any given point and try not too make too many leaps of faith as to what you think the system MAY evolve to.

Given that you know what you are building right now, you design your classes trying to make the best possible use of OO concepts - eg encapsulation / polymorphism. This is itself, like others have noted as well, can be notoriously difficult to achieve and thats where experience, both in designing OO systems as well as knowledge of the domain can really come in handy.

Design based on what you know --> Build It --> Review it --> Refactor it --> Re-design --> and it goes on and on..

InSane
Love this answer. Few YES! moments reading it. I always try to think ahead this is a strength but also a weakness. I am forever thinking but what if..But I'm no psychic I can only work with what I have/know. Thanks for this, deserves more than my single upvote.
Franco
+1 Upvoted it for you as well. Just read these same sentiments in a Domain Driven Design book the other day.
wllmsaccnt
Which book was it?
Franco
Domain Driven Design Quickly. You can download it for free. It is more of an overview of the Domain Driven Design principles than a book on specific implementation concerns, but it reads well and is fairly concise. DDD is heavy into refactoring and being very precise with termonology gleaned from domain experts.
wllmsaccnt
A: 

Both factories would produce the same objects but the inner workings are completely different.

Not sure if I've understood you correctly, but this sounds like a candidate for the AbstractFactory pattern.

Qwerky
From reading Franco's other comments I think he means that the inner workings of the objects after they are created is completely different...not just the inner workings of both of the factories.
wllmsaccnt
Yeah, that's fine. So long as the objects share a common interface then that's what abstract factory is all about.
Qwerky