Posts Tagged Object Oriented
Master of your domain
Posted by cgrant in Development, Java, Technology on December 18, 2010
I’m a fan of what I’m calling domain driven development. That is start with the core domain objects and develop outwards. Develop and use core domain objects and two things will happen. First you’ll find that your code starts to simplify and second that there is more consistency and less bugs.
Wait what is a domain? It’s the content of a particular field of knowledge. Think of it as the core of what you do. So if I’m a retail business my domain encompasses customers, products, orders, etc. If I’m Santa Claus my domain might include Sleigh, Reindeer, Elves, Lists etc. These are the major items that are important in a particular area, or domain. Read the rest of this entry »
Object Oriented Thinking for the Procedural Programmer
Posted by cgrant in Development on December 7, 2009
Moving from Procedural to Object Oriented thinking
In procedural programming we typically tell the system what to do in a line by line sequence of events. The data being used was globally available to enable various functions to act on it. Many developers today are using a mix of procedural and object oriented techniques. While they may be using an object oriented language and object oriented libraries or tools, the actual code tends to be rather procedural.
Many tutorials use “Hello World” as a sample application to teach. Interestingly most of these examples also use this mixed mode.
Look at a simple Java example Read the rest of this entry »
Bite Size: Object Oriented JavaScript
Posted by cgrant in Development, Technology on May 28, 2008
JavaScript is typically thought of as a procedural scripting language, however it contains many aspects that allow for Object Oriented style scripting. Below you will find a few examples of how to script JavaScript in an Object Oriented style.
Before you start
The best way of studying this tutorial is to have Firefox with Firebug handy. This way you can test the tutorial examples immediately.
Install both of them if you haven’t already done so.
Also review the tutorial on using Firebug and additional tips on debugging with firebug
{tip}
The Firebug command line, found at the bottom of the Console tab, is no different than any command line you’ve used in UNIX, DOS, or Windows—except that it accepts commands written in JavaScript.
The command line is more than just a place for one-liners. You can expand it to become a full-size editor. When you’re working in your external editor, you can quickly copy-and-paste code into the Firebug editor to execute them immediately and how they effect the live page. Once you’ve gotten the result you’d like, copy the finished code and paste it back into your editor.
{tip}
Variables
Global variable definition
var myVariable = "myVar";
function visibilityExample(){
/* visibility: public */
this.publicVar="";
/* visibility: private */
var privateVar="";
}
Methods
Simple method with return
function MyMethod1() {
var rtnVar1 = null;
this.pubVar1 = "1";
rtnVar1 = this.pubVar1
return rtnVar1;
}
alert(MyMethod1()); // returns 1
Static Objects
Demonstrates Object with static methods foo and bar
var MyObject2 = {
foo: function() {
var rtnVarFoo = null; //private variable
this.pubVar2 = "2"; //public variable
rtnVarFoo = this.pubVar2;
return rtnVarFoo;
}, // notice the comma
bar: function() {
var rtnVarBar = "2";
return rtnVarBar;
}
}
alert(MyObject2.bar()); //returns 2
Basic Object Instantiation
Demonstrates basic object instantiation
function MyObject3(r,g,b)
{
this.Red = r;
this.Green = g;
this.Blue = b;
this.bar = function(){
var rtnVarBar = "3";
return rtnVarBar;
}
}
myNewObj3 = new MyObject3("red","green", "blue");
alert("Bar: " + myNewObj3.bar()); //returns 3
Basic Prototype Example
/* Base object */
function MyObject4() {
// constructor code goes here
this.myVariable4 = 5;
this.bar = function(){
var rtnVarBar = "4";
return rtnVarBar;
}
}
/* Adds method to Base object */
MyObject4.prototype.memberMethod = function()
{
return this.myVariable4;
}
myNewObj4 = new MyObject4();
alert(myNewObj4.memberMethod());
Basic Inheritance Example
prototype = base object type (think extends)
objects not defining a prototype are thought to extend Object
(think extends Object)
objects (objA) that have their prototype set (to objB) are thought to extend the second object (objA extends objB)
// Base Object
function Employee () {
this.name = "";
this.dept = "general";
}
// Manager extends Employee
function Manager () { // defines initial Manager constructor
this.reports = "Bob, Jane";
}
Manager.prototype = new Employee; // sets Manager as an extention of Employee
// WorkerBee extends Employee
function WorkerBee () { // defines initial WorkerBee constructor
this.projects = "SalesProj, MarketingProj";
}
WorkerBee.prototype = new Employee; // Sets WorkerBee as extention of Employee
myManagerJohn = new Manager();
myManagerJohn.name="John";
alert("Name: " + myManagerJohn.name + "\nDept: " + myManagerJohn.dept + "\nReports: " + myManagerJohn.reports);
myWorkerBeeBob = new WorkerBee();
myWorkerBeeBob.name="Bob";
myWorkerBeeBob.dept="Sales";
alert("Name: " + myWorkerBeeBob.name + "\nDept: " + myWorkerBeeBob.dept + "\nProjects: " + myWorkerBeeBob.projects);
Extending the base prototype
Adds to base object AFTER child objects have been created aka. Dynamic Inheritance
// Base Object
function Employee () {
this.name = "";
this.dept = "general";
}
// Manager extends Employee
function Manager () { // defines initial Manager constructor
this.reports = "Bob, Jane";
}
Manager.prototype = new Employee; // sets Manager as an extention of Employee
// WorkerBee extends Employee
function WorkerBee () { // defines initial WorkerBee constructor
this.projects = "SalesProj, MarketingProj";
}
WorkerBee.prototype = new Employee; // Sets WorkerBee as extention of Employee
myManagerJohn = new Manager();
myManagerJohn.name="John";
alert("Name: " + myManagerJohn.name + "\nDept: " + myManagerJohn.dept + "\nReports: " + myManagerJohn.reports);
myWorkerBeeBob = new WorkerBee();
myWorkerBeeBob.name="Bob";
myWorkerBeeBob.dept="Sales";
alert("Name: " + myWorkerBeeBob.name + "\nDept: " + myWorkerBeeBob.dept + "\nProjects: " + myWorkerBeeBob.projects);
Employee.prototype.hourlyRate = "50"; // << Dynamic Inheritance
alert("Name: " + myManagerJohn.name + "\nDept: " + myManagerJohn.dept + "\nReports: " + myManagerJohn.reports + "\nHourly Rate: " + myManagerJohn.hourlyRate);
alert("Name: " + myWorkerBeeBob.name + "\nDept: " + myWorkerBeeBob.dept + "\nProjects: " + myWorkerBeeBob.projects + "\nHourly Rate: " + myWorkerBeeBob.hourlyRate);
Working With Constructor Parameters
/**
* Base Person object with empty constructor
* @constructor
*/
function Person(){
this.name="Name: ";
}
/**
* Base Person object with 1 element constructor
* @constructor
*/
function Person(name){
this.name="Name: " + name;
}
bob = new Person("Bob");
alert(bob.name);
/**
* Extended object with constructor
*/
function Teacher(name, subject){
this.base = Person;
this.base(name); // Think super(name) from java
this.subject="Subject: " + subject;
this.toString = function(){
return this.name + "\n" + this.subject;
}
}
Teacher.prototype= new Person; // Ensures dynamic inheritance
jim = new Teacher("Jim", "Math");
alert(jim);
/**
* Using ".call" for parent rather than ".base"
* for a 'cleaner' implementation
* .call was implemented in: JavaScript 1.3
*/
function Janitor(name, shift){
Person.call(this,name); // Think super(name) from java
this.shift="Shift: " + shift;
this.toString = function(){
return this.name + "\n" + this.shift;
}
}
Janitor.prototype= new Person; // Ensures dynamic inheritance
stan = new Janitor("Stan", "Night");
alert(stan);
