I have just made a small online CAD demo for fun and learning. It is working perfectly for my intentions, but now I want to improve the code and I'm not sure of how to convert the current function-oriented code to Object Oriented. This is a small part of the code I have so far:
// DATA
var lines = [[]];
var circles = [{}];
var rectangles = [{}];
// Drawing functions
function drawLine(from, to){}
function drawCircle(center, radius){}
function drawRectangle(from, size){}
// Adding elements to the render chain
function addLine(e){}
function addCircle(e){}
function addRectangle(e){}
To get it Object Oriented, I can see two different ways. The first one is to group them by domain. This way there are many functions that can be reused within each of the classes:
var lines = [[]];
...
var draw = function(){
var all() = function(){}
var lines() = function(){}
var circles() = function(){}
var rectangles() = function(){}
}
var add = function(){ ... }
...
The other one is to group them by type. This is the way I've read before about OO, since it's a representation of real world models and it's much more intuitive. The best part is that you have all the parts for the rectangle
in the same small piece of code:
var rectangle = function(){
var x, y, width, height;
var draw = function(){}
var add = function(){}
}
...
Which one follows closer the best OO patterns?