Re: fyi, small note on using Mathematica for object based programming
- To: mathgroup at smc.vnet.net
- Subject: [mg125976] Re: fyi, small note on using Mathematica for object based programming
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Wed, 11 Apr 2012 18:15:59 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
method[arg] is determined according to specific DownValues for a specific
"arg" or a general pattern for objects with the Head (or other
characteristics) of "arg". The same function or method name can be used
for many objects and classes of objects without confusion. That behavior
is, I would say, inherently object-oriented.
The opposite of object oriented is when function names must include class
names, such as integerAdd[1, 2] rather than simply 1 + 2.
Judge for yourself whether the following is simpler than the OP's code,
but I don't believe it requires mimicking C++ so closely!
methods = {index, polynomial, stiffness, mass, damping, TF,
unitStepResponse, bodePlot, poles, display};
ClearAll @@ methods
plantList = {};
plant[stiff_, damp_, m_] :=
Block[{s, t}, Module[{tf, poly, self = plant[1 + Length@plantList]},
polynomial@self = 1/(m*s^2 + damp*s + stiff);
index@self = First@self;
stiffness@self = stiff;
mass@self = m;
damping@self = damp;
tf = TransferFunctionModel[polynomial@self, s];
TF@self = tf;
unitStepResponse@self =
Chop@First@OutputResponse[tf, UnitStep[t], t];
bodePlot@self = BodePlot@tf;
poles@self = TransferFunctionPoles@tf;
AppendTo[plantList, self];
self
]]
display[any_plant] := Through[(methods[[;; -2]])@any];
new = plant[1, 1, 10];
display@new
Bobby
On Sat, 07 Apr 2012 03:34:34 -0500, Andrzej Kozlowski
<akozlowski at gmail.com> wrote:
> While I completely agree with the sentiment expressed in the second
> line, I don't think the first one is true, at least not in any sense of
> "object-oriented" I am familiar with.
>
> Andrzej
>
>
> On 6 Apr 2012, at 11:58, DrMajorBob wrote:
>
>> Mathematica is already object-oriented WITHOUT all that. What a lot of
>> overhead for nothing.
>>
>> Bobby
>>
>> On Thu, 05 Apr 2012 04:49:54 -0500, Nasser M. Abbasi <nma at 12000.org>
>> wrote:
>>
>>>
>>> Just FYI,
>>>
>>> I wrote this small note on using Mathematica for object-based
>>> programming. I found that it works really well for me.
>>>
>>> Nothing too advanced, just a simple way of using Module[] but
>>> in a way to emulate object based programming that I did not know
>>> about before.
>>>
>>> http://12000.org/my_notes/object_based_in_mathematica/v1.html
>>>
>>> --Nasser
>>>
>>
>>
>> --
>> DrMajorBob at yahoo.com
>>
>
--
DrMajorBob at yahoo.com