Problem: your program has 30 classes in an unstructured mess.
Solution: gather related classes into sealed "Pyramids", forcing strict structure upon your unruly code.
- Organises your classes into privately scoped groups "Pyramids"
- Nothing outside a Pyramid may access anything inside the Pyramid, the Pyramids are loosely coupled.
- Each Pyramid publishes selected methods so that other Pyramids may subscribe to them.
- One line of code per class, and one line per Pyramid.
Here we have an "Pyramid Object" that represents the constructors for a group of classes.
Each null is replaced with the real class constructor when it is declared.
Our "controllers" Pyramid Object is composed of the class constructors for Clicks, Controller and Events.
This organises our classes into 2 "Pyramid Object" packages, "models" and "controllers". It not only documents the design, it will encapsulate them in stone. Nothing in "models" may reference anything in "controllers", and vice versa.
Our Model class instantiates itself and the other member of the Pyramid Object. The last act of the constructor is to delete itself from the "models" object. Since there is an anonymous closure around Model is is now cemented shut. Nothing can reference it at all.
All the classes in the "models" object delete themselves from the object.
Now the Pyramid object is empty:
So nothing can reference anything outside its own class. Which isn't much good, so we use a Publish–subscribe pattern "radio.js" so the occupants of the pyramids can call each other.
The classes here use the jsface.js library, it doesn't matter how you implement classes here.
As shown, you can only have one instance of each class. If you want several instances of one class, leave out the "delete" in the classes constructor and clear the Pyramid object later on: