Me... again, still boring you to death with meaningless OOP rantings.

First I would like to point out that there was a design mistake in what I
proposed in the last mail (following afterwards): the Saluter shouldn't be
an abstract class, it isn't correct to base your class hierarchy on the
ability to greet (or in the ability to do something generally speaking). If
it's done that way we'll have classes coexisting in the same branch of the
class tree, but possibly having completely different nature. Saluter should
be an interface:

 interface class Saluter {
   public function greet(Salutable $receiver);
 }

 class FormalSaluter implements Saluter {
   public function greet(Salutable $receiver) {
     echo "Hello " . $receiver->getSalutationName() . "\n";
   }
 }

Then, I'll try to translate what has been said in the spanish list.

It has been proposed that World should be a singleton, since there is only
one world... but this arguable, at metaphysic level perhaps...

Also an observer pattern could be used so anyone can hear the greetings. But
I think this should be done on the design of the greeting enviroment
abstraction (to provide other places than standard output where the greeting
can be said).

The saluters factory should use a factory method pattern or an abstract
factory pattern? I think an abstract factory is not appropiate in this case,
the creation process is not that complex to require this kind of design
pattern.

Also I proposed that decorators could be used to add styles to the greeting
for outputs that allow styling (e.g.:html). In this case maybe an abstract
factory would be appropiate, so that decorated saluters are created for the
appropiate type of output, combined with a builder pattern to create the
decorated saluters (this last part I thought it just now, I'll post it later
on the spanish list)

Well, I think that sums it all up.

2006/9/29, Martin Alterisio <[EMAIL PROTECTED]>:

What's up folks?

I just wanted to tell you about a thread that's going on in the spanish
php mailing list. A fellow software developer which had just started with
OOP was asking for "Hello World" examples using OOP. The examples of code he
had been doing was not that different from the usual Hello World example we
all know and love(?), so I thought he was missing the point of what was the
purpose of using OOP. This was my reply (I'll try to keep the translation as
accurate as possible):

I believe you have misunderstood what is the purpose of OOP, your objects
are not proper abstractions.

First and most important, you should define what is the problem to solve:
greet the world.
We build an abstraction of the problem and its main components: somebody
who greets and something to greet.
Here we're working with generalizations, since our main objective is to
build reusable objects. For that purpose is useful that our "saluter" could
be used to greet more than just the world.

Therefore we'll first define what kind of interaction we expect the
salutation receivers to have, in the following interface:

  interface Salutable {
    public function getSalutationName();
  }

In this interface we have all we need to properly greet any entity: the
name we should use when doing the salutation.

Then we create the object which represents the world:

  class World implements Salutable {
    public function getSalutationName() {
      return "World";
    }
  }

Now we're missing a saluter, but we're not sure which way of greeting
would be appropiate, so we prefer to create an abstract saluter and leave
the child implementation to decide the appropiate greeting:

  abstract class Saluter {
    abstract public function greet(Salutable $receiver);
  }

In our case we need a formal saluter as we should not disrespect the world
(saying "hey! wazzup world?" could be offensive), then:

  class FormalSaluter extends Saluter {
    public function greet(Salutable $receiver) {
      echo "Hello " . $receiver->getSalutationName() . "\n";
    }
  }

Finally we make our saluter greet the world:

  $saluter = new FormalSaluter();
  $world = new World();
  $saluter->greet($world);

----

Other things you should keep in mind:

* PHP's type hinting is preety limited, in this case we would like to
indicate that the name should be provided as a string but we can't. Maybe it
would be useful to use an object as a wrapper for native strings. EDIT: I
remembered while translating this that type hinting can only be used in
function parameters, therefore this point is useless.

* En this model it seems more appropiate that the saluter is an abstract
class, since salutation works one way, but, in the event salutations became
a two way trip, an interface would be more appropiate for the saluters.

* Here we're sending the salutation to the standard output, which is
acceptable in this case, but a more complex abstration would require us to
indicate where we should procede with the salutation, and we will have to
provide an abstraction for the possible salutation enviroments.

* A factory of saluters would be a nice feature.

------------

That's it. If you find this interesting I'll try to keep up with the
translation of following posts.

Reply via email to