# OOP environment for Mathematica without additional packages

GROUPS:
 Hirokazu Kobayashi 7 Votes I'd like to introduce the OOP environment for Mathematica, with no-additional packages. Mathematica has many style of paradigm, however, the programming style of OOP is an old but not sufficient backed-up environment. Let's consider the function group UpSet. UpSet has been seemed the basic idea relating OOP and the function group being equipped from the first version, but you can see the description of the function group is so small and short in the manual. Now, the UpSet function becomes the most important function because the function group is the key parts to establish the OOP environment. With very small size pattern of CLASS constructed with SetDelayed UpSetDelayed, and Module leads to the OOP paradigm. This paradigm includes Encapsulation, inheritance, and Polymorphism in very straight forward style. OOP on the Mathematica suits for Graphics, Events, and the Dynamic directly and give us new kind of power for Mathematica programming.The method is very easy as follows. Basically, you define the CLASS which is "SetDelayed definition of Module-d UpSetDelyaed functions", you construct INSTANCES distinguished by the name having each local values, and each INSTANCE functions when you send name conbined MESSAGES defined in the CLASS. In the following code, class name is "pclass", two method named "set" and "get", object list name is "object". In my definition, OBJECT is the group name of INSTANCE created from CLASS. (*prepare class*) pclass[nam_] := Module[{local = 0}, set[nam[x_]] ^:= local = x; get[nam] ^:= local^2;] (*prepare object list*) object = Table[Unique[], {10}]; (*create instances*) Table[pclass[object[[i]]], {i, 10}]; (*send message to set local values of instances*) Table[set[object[[i]][i]], {i, 10}]; (*send message to get results of instances*) Table[get[object[[i]]], {i, 10}] {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} I hope you enjoy the new paradigm.In Google slide, you can see more detailed OOP discussion. Attachments:
1 year ago
9 Replies
 Kuba Podkalicki 1 Vote You can add something in the general topic on SE or in topics linked there:Which Object-oriented paradigm approach to use in Mathematica?
1 year ago
 Hirokazu Kobayashi 1 Vote This mind map is asking to reconsider on the Mathematica OOP, "how to" develop or/to "why to" develop it. Though, if we can achieve OOP with very small cost, the importance of "why" becomes lower one. My idea for the OOP requires very small cost, it means a few lines code is additional, so "why" will have small meaning in my OOP case. On the contrary, we can obtain large advantage from the OOP. The power of Mathematica comes from many characteristic points, and I think the various programming paradigm may be the largest one, so OOP will become the new large power source.
1 year ago
 Sam Carrettie 2 Votes It would be great I think if you would be able to explain your OOP idea in-post here or attache a notebook.
1 year ago
 Hirokazu Kobayashi 2 Votes Yes, I attached a notebook.
1 year ago
 David Fuller 1 Vote Hi,Apologies, yet I do not see how your approach promotes re-use, abstraction, encapsulation, or Typing of any sort. Despite your mention of design patterns, I do not see how it can, for example, be used to implement common patterns such as observer, visitor, or, most ubiquitously, singleton. Most importantly, I unclear how it helps me, in structuring code this way, reduce complexity or improve understanding. I did download your notebook. Unfortunately, this was not enough to actually understand what it is that you are proposing and what advantages there are in contrast to other approaches. I think much might be understandable were there a more in-depth write-up similar to that others have posted when putting forth their proposals for approaching OO in Mathematica. Regards, Dave
11 months ago
 One more Q: Why would I 'construct' a superclass specifically that I might 'subclass' it? That does not appear to make sense. I am assuming that your example is just unclear in some respect.