# Instance-preceded and Method-preceded Object Oriented Wolfram Language

**URL:** <https://community.wolfram.com/t/instance-preceded-and-method-preceded-object-oriented-wolfram-language/14778>\
**Category:** Staff Picks\
**Tags:** computer-science, graphics-and-visualization, interface-construction, staff-pick\
**Created:** [September 28, 2019, 3:21pm UTC](https://community.wolfram.com/t/instance-preceded-and-method-preceded-object-oriented-wolfram-language/14778 "2019-09-28T15:21:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kobayashikorio](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/kobayashikorio/32/5875_2.png) [@kobayashikorio](https://community.wolfram.com/u/kobayashikorio)\
**Post date:** [September 28, 2019, 3:21pm UTC](https://community.wolfram.com/t/instance-preceded-and-method-preceded-object-oriented-wolfram-language/14778/1 "2019-09-28T15:21:20Z")

</div>

A few years ago, I introduced Object-Oriented-Programming environment for Wolfram language with no packages. Today, I would like to introduce a new type of Wolfram OOP. Previous one is the type of Method-preceded style, and new one is Instance-preceded style.

Key function of Method-preceded style is UpSetDelayed function that combines method to instance, and for the Instance-preceded style is SetDelayed function. There is a small deference between Method-preceded and Instance-preceded, but more easy style may be Instance-preceded one.

I already shown a fundamental class definition of Method-preceded style as,

```
class[nam_]:= Module[{local},
    localValueGetMethod[nam]^:= local;
    localValueSetMethod[nam[x_]]^:= local= x;
    ]

```

A new fundamental class definition of Instance-preceded style is as follows.

```
class[nam_]:= Module[{local},
   nam[localValueGetMethod]:= local;
    nam[localValueSetMethod[x_]]:= local= x;
    ]

```

From the class, we can make a instance as,

```
class[instance]

```

Then we can set a value to the inner-variables as,

```
instance[localValueSetMethod[10]]

```

We get a result “10”. Also, we can call the inner-variables as,

```
instance[localValueGetMethod]

```

We can get the value “10”. The construction of instance is equal to evaluate a class function with the name of instance, we can use Map to produce multiple instances as,

```
objectList = {instance1, instance2};
Map[class, objectList];

```

Each style is being applicable the Association function for the Construction and message handling for multiple objects as previously shown in my articles.

Next sample is to animate 3-dimensional objects movement using Instance-preceded OOP for Wolfram language.

```
DynamicModule[{
  n = 10,
  speed = 0.01,
  speedRange = {1., 5.},
  bg = ParametricPlot3D[{(2 + Cos[8 u]) Cos[u], (2 + Cos[8 u]) Sin[u], Sin[8 u]}, {u, 0, 2 Pi},
   PlotRangePadding -> 0.3, PlotStyle -> {Thickness[0.005]}, Axes -> None],
  objectList},
 
 CLASS DEFINITION;
 new[nam_] := Module[{u = 0, v = 1},
   nam[setv[x_]] := v = x;
   nam[step]:= (u++; s = u*v; {Specularity[White, 20],
    Sphere[{(2 + Cos[8 s]) Cos[s], (2 + Cos[8 s]) Sin[s], Sin[8 s]}, .25]})
   ];
 
 CREATE INSTANCES;
 objectList = Table[Unique[], {n}];
 Map[new[#]&, objectList];
 Map[#[setv[speed*RandomReal[speedRange]]] &, objectList];
 
 SHOW GRAPHICS;
 movingPoints3D := Graphics3D[{Map[#[step] &, objectList]}];
 Animate[t;
  Show[{bg, movingPoints3D}], {t, 0, 100, 1}, AnimationRunning -> False]
 ]

```

We can see the animation of 3D spheres as following image.

 ![3d.png](https://global.discourse-cdn.com/wolfram/original/3X/4/1/418ccb22b5ab351725b43fafaadb370c55ceabf5.png)

Enjoy, Wolfram Object Oriented Programming!

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [December 20, 2021, 9:40pm UTC](https://community.wolfram.com/t/instance-preceded-and-method-preceded-object-oriented-wolfram-language/14778/2 "2021-12-20T21:40:03Z")

</div>

![news-congrads-kkluyshnik-02-04-19.jpg](https://global.discourse-cdn.com/wolfram/original/3X/6/a/6a310c2d0904616faf4e6e148260c9f29ab9f4eb.jpeg) – you have earned _ **Featured Contributor Badge** _ ![2015-11-02_10-07-11.png](https://global.discourse-cdn.com/wolfram/original/3X/5/0/50e59c9ead62f729e7130d3ea1482555a75602b6.png)  
Your exceptional post has been selected for our editorial column _ **Staff Picks** _ [http://wolfr.am/StaffPicks](http://wolfr.am/StaffPicks) and [Your Profile](http://community.wolfram.com/web/kobayashikorio) is now distinguished by a _ **Featured Contributor Badge** _ and is displayed on the [Featured Contributor Board](https://community.wolfram.com/people). Thank you!
