Sujet : Re: My hang-up about OOP (snit)
De : rich (at) *nospam* example.invalid (Rich)
Groupes : comp.lang.tclDate : 25. Jun 2024, 23:29:46
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5fgcq$1oiln$2@dont-email.me>
References : 1
User-Agent : tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Luc <
luc@sep.invalid> wrote:
It's hard for me to accept OOP because as hard as I try I really can't
not think that OOP is the Emperor's new clothes.
Example from snit:
snit::type dog {
method {tail wag} {} {return "Wag, wag"}
method {tail droop} {} {return "Droop, droop"}
}
dog spot
puts [spot tail wag]
puts [spot tail droop]
Why exactly is that any better than this:
proc dog {args} {
if {$args == "tail wag"} {return "Wag, wag"}
if {$args == "tail droop"} {return "Droop, droop"}
}
puts [dog tail wag]
puts [dog tail droop]
If you only *ever* have one dog, either are essentially the same.
But, if at some point you want to manage spot, frank, sam, donut, and
alexa, all of which are different dogs, then you can do:
snit::type dog {
method {tail wag} {} {return "Wag, wag"}
method {tail droop} {} {return "Droop, droop"}
}
Once to create a generic "dog" framework.
And then later do, all in the same single program instance:
dog spot
dog frank
dog sam
dog donut
dog alexa
And have five, fully independent dogs (each with their own independent
"data") that you can manipulate, without having to do anything special
at the time you create them.
I.e., you can wag spot's tail, give food to sam, and label alexa with
"last vet visit was 2024-06-21" (assuming you had mentods for 'feed'
and for 'last-vet-visit' in the generic framework.
To do the same with the "proc" you have to rewrite the code to
be something like:
proc dog-spot ...
proc dog-frank ...
proc dog-sam ...
Or else you have to modify the dog proc to use a global variable to
store "different dogs" and take a parameter of "dog-name" so it knows
which dog to manipulate.
The "objects" give you that "data separation" as part of the OO
framework, without you having to build it in manually.