What is the best practice for communicating between objects in OOP to perform work? - oop

I'm curious on what the great minds have to say about the best practise in communicating between objects in OOP and getting things done? To illustrate, I'll give an example. Say I have a few class objects for some aspects in a file converter:
An input class that deals with the input. Perhaps with inheritance for difference file formats.
A filter class that filters the input in some kind of a way.
An output class that write the output to files
Now we need something that instantiates these objects and performs the work, perhaps first interpreting commandline options etc. This may seem straightforward, but challenges may come quickly. The output class may need to know things from the input class to do its work.
This was just a simple example, but I've found it hard to locate any good reading on this particular topic which interests me. This is not so much about when to use inheritance or polymorphism, but rather how to best put these building blocks together without causing a mess. Any good links on this?

Related

How can I change enum based system to object oriented structure?

I have a enum based and complicated structure. I want to change my structure with object oriented structure. You must know this there are too much states. So I searched in Internet and I found solutions like that
http://blogs.microsoft.co.il/gilf/2009/11/22/applying-strategy-pattern-instead-of-using-switch-statements/,
Ways to eliminate switch in code .
When I apply this solutions, there will be too much classes. What do you think about it, Should I apply like that.
Yes, definitely. You should go for the Strategy solution.
And in my experience, there is almost never a case of too much classes, as you put it. On the contrary, the more modular your code is, the easier it is to test/maintain/deploy it.
You'll run a lot in the opposite problem: a class you thought is small enough and there will be no reason to change, and then after a change in the requirements or a refactoring you see that you need to make it more modular.

OOP - pass one object to several others?

I am not quite sure if this question belongs here or to another community on stackexchange, if the latter, I could not find the right one and would be glad about a hint.
I am at the moment programming a little game (roundbased -no threading) in Java and I am wondering:
Is it bad practice if one object is known to several others?
In my case I wonder if I should create an Object o and then pass it as a constructor argument to several other, later created objects.
It does work, but I wonder if this should in general rather not be done?
Does someone have an answer?
Yes of course. It is normal case in the world of oop. One of simple examples of such usage is dependency injection. For more information see https://en.m.wikipedia.org/wiki/Dependency_injection
Besides, many design patterns use such approach : strategy, observer etc.

hints for naming methods

We all know that classes/objects should represent things
while methods/messages should represent operations (verbs).
But how to pick the right verb?
I've heard one "rule for method naming" is to
imagine some completely different implementations and then
simply pick name that is general for all of them.
EDIT: I also know that methods should be named as closest to domain as possible.
(means after intention not after implementation)
What others do you know?
I like to try to name things so that a given line of code would make as much grammatical and syntactic sense as possible, even to a non-programmer. (One of the reasons why I love lambda so much in my .NET code. It's like making new words from Latin roots, you just keep chaining things together.)
There's a pretty good article here on naming classes and methods when building a repository, for example.

First step in OOD?

What is the frst step in OOD?
There are no steps, it's not a process.
The answer is..
(source: headfirstlabs.com)
http://headfirstlabs.com/books/hfooad/
http://www.amazon.com/dp/0596008678/?tag=forelangstud-20
Practice, read broadly and more practice.
Especially with others to review and comment on approaches.
Reading should cover not just OOD, but also patterns to see how others have approached common problems.
It's a lot of practice. The first thing is to get your mind around the way objects work--especially if you are a procedural programmer.
Practice making many small objects--I've literally never seen a system with too many objects; it's possible but I've never seen it. It should be really obvious when you need to put many objects into one, but it's not as obvious when an object should be broken up.
Ask an object to do something, don't ask for it's data. Try to avoid getters and setters and concentrate on methods where you ask it to do something with it's data. If you EVER see code like o.a=o.b+o.c or o.setA(o.getB()+o.getC() you are doing it wrong.
Constantly try to refactor out duplication. Rewrite your code repeatedly until there is none (or as little as possible). This will probably do more for your OO design skills than any other practice. As you get more knowledgeable, try refactoring things you didn't think you could refactor before. Anything that even looks like a pattern can probably be refactored. For instance here's a very basic example--if you had lines of code that looked like this:
a = b + c * d;
g = h + i * d;
Chances are there are HUGE refactorings missing in your code even though it doesn't look like it off the bat. You probably are missing an object that would hold a,b,c and a second instance would hold g,h,i, after creating these objects a bunch of stuff would factor into your new object. Learning to recognize new opportunities like this is critical.
I've been programming for over 20 years now, over half of it has been OO at this point and it seems like every few years I think I know it all--a year later I look back and realize how ignorant I was.
the first step is object oriented analysis - its aim is to identify the objects that make up a system and how they interact; given this knowledge you can then specify the behavior of the object (the interface methods) and then the internals (what are the required data members of an object)
The design process produces a number of diagrams - these are tools that are supposed to help with working out the details of the system :
first come a set of 'use cases' - a use case is a verbal description of a scenario that is implemented by the system (one is supposed to pick the most substantial ones); these are then used to identify the main actors and concepts which are supposed to map to the classes of a system. This understanding is then refined by working out 'object interaction diagrams' 'class diagrams' and 'sequence diagrams' sometimes state charts are used to visual state machines - these diagrams are tools to gain an even better understanding of the system, as a result you have a sufficient understanding of the system to write the class header files/class definitions. There are no fixed rules which one of these diagrams come first, these are used as appropriate.
i found the following book very useful :
OBJECT-ORIENTED ANALYSIS AND DESIGN With applications (second edition) by Grady Booch
the book goes through the process of designing several example systems step by step (i think it is enough to read the design process for these example systems); One minor problem is that the notation used in this book is a bit dated : modern practice is to use the UML notation for diagrams, however the book still uses the older Booch notation. The strong point of the book is that it is always explaining each concept by working through concrete examples.
There are some preliminary steps:
Understand OOD (in general)
Understand the problem/application domain (the functional specification)
Have a high-level/architectural design: know what O/S, libraries, frameworks etc. you can use
I then use a mixture of top-down and bottom-up development:
Top-down: decide what components and what APIs (object interfaces) I would like to have in order to implement the application (and then, develope those API)
Bottom-up: decide how to add new functionality to existing APIs (object interfaces), by adding new methods and new types of object (and sometimes splitting a large object into several smaller objects).
The first step of OOD are the OOD principles. Check out The Principles of OOD.

Object Oriented Design approach to a conversion app

I think i might be being blinded by the way I've learned OO principles. Its always taught in the manner of tangible object is a descendant of another tangible object. Anyway...
I'm trying to work out an OO approach to a conversion utility, basically it will read a text file and based on the first word(s) on the line it will go off and translate / do whatever corresponds. Would this be a case of each command is a new object or...
Its probably a simple answer but I'm struggling
Take a look at the Interpreter, and the Factory Method patterns.
I did this once for a database conversion application. I had import plugins (classes) that read data into a common model. Then the export plugins (classes) read the common model and wrote it out to a database. In my case, this was a .NET application, so I used MEF to split the importers and exporters into different assemblies. Customers could plug in whatever importers and exporters they needed, or could even write their own if they wanted to.
I would not get too caught up in OO design for something like this. OO principles should be used only if they are obvious and help you with reuse. That might not be the case here. Also, this does not sound like a large app. The pitfall people fall into is making way too many classes just to get a little bit of reuse. This quickly makes things unmanageable. Just do it the old-fashioned way and if something jumps out at you where inheritance can help for example, then use it.