From: Robert Klemme Date: 2010-07-11T01:50:09+09:00 Subject: Re: from functions to classes, where should I start? On 10.07.2010 01:44, salamond wrote: > Thanks, Robert and Jay. > > I started programming with C, not the type of a natural OO programmer. > And yes, the Procedure Oriented way, or function way works fine for > me, almost all the time. The funny thing is, you can code OO even in a language like C. Of course it is much easier in a truly OO language but if you look at the C standard library you can look at it in a OO way in parts. For example look at syscalls open(), write(), read() and close(). open() returns a file descriptor (aka object id) and this is what you pass to the other three methods - pardon: functions - along with more parameters. You can see read() and write() as ordinary methods and close() as the destructor. Of course you do not have polymorphism in C but these functions are definitively a case of encapsulation: you do not really know (nor do you need to know) what your operating system's kernel stores along with the file descriptor and how it performs all those operations. > Until I'm considering adding unit tests to my own scripts. > With the mock pattern, an interface like Cat with a function > "catchRats" is a must to have, then CatImp and CatMock. > That's how mock works. > > That's where I start thinking, maybe it's time to OO. > > But as you say, it doesn't have to be OO all the time, > unless there is a specific problem better be solved that way. > > So here's what I'll try: > * I have a copy of Design Patterns by "gang of 4", seldom read, I'll start now > * I think if unit testing is what I need, I should go through unit > testing patterns, see how OO works for a real problem > * I'll definately follow your suggestions, see if there's sign for OO > in my scripts, (encapsulation, etc). Sounds like a plan. I am not sure though about the connection you are doing between unit testing and OO. Although most testing frameworks in Ruby are in fact object oriented the whole concept of unit testing also works for non OO code - and a testing framework does not necessarily need to be OO. A strategy for finding classes that I find pretty slick is CRC Cards. This does not have too much overhead and leaves out a lot of detail in the first step. IMHO that helps concentrating on abstract entities. http://en.wikipedia.org/wiki/Class-responsibility-collaboration_card Another book I usually recommend for in depth coverage of OO is OOSC: http://docs.eiffel.com/book/method/object-oriented-software-construction-2nd-edition Although it does cover a completely different programming language (Eiffel) it covers all OO concepts I am aware of in a very minute manner. > Again, many thanks to you guys. You're welcome! Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/