Object Orientated Programming (OOP) and doors
I often use day to day things to explain Object Orientated Programming (OOP), to explain how we develop software and promised I would writea separate article about doors and OOP in a previous article "You have more knowledge than you know " so here goes …
As programmers we see the world through different eyes, as wee see things as objects; not only tangible things but also abstract things. In this simple example I’ll use a door to explain the concept of Object Orientated Programming (OOP). The door is the object or thing.
Objects have properties, methods and events that define it and can be extended, but more about that later, once we have got a grasp of objects.
Objects use classses which are an abstract layer that defined the object itself. I’m not going to explain classes in this article as it will end up been a book!
So lets take a look at our door, it’s a pretty simple door.
Our door has numerous properties (also referred to as attributes) that describe it, so let’s have a look at some of them:
Fire door: True
External door: False
As you can see, the properties describe the object; depending on what you want your object to be able to do, will determine the level of detail. To complicate things a little more, objects can be associated with other objects so you could break down the door frame, hinges, lock, door and handles as separate objects, but as this is only a theoretical abstraction, so lets not get carried away just yet.
Our door also has various methods that describe something that can be done to the object.
Once again, we’ve taken a simplistic look at our OOP door, but hopefully you get the idea? If the object had a lock you might want to add lock / unlock methods, but as this is only an internal door and it doesn’t have a lock, we’ll leave this.
But hang on, we could have some more methods, even for our simple OOP door:
Once, again methods can be added to as and when required.
Lastly, we define the events:
Events are triggered when something happens, so when you close the door the event close is triggered. An event can have one or more parameters, so the event itself looks something like this:
The above example has a single parameter, which by default is set to false, so unless you tell the event that the door is been slammed, the system will default to a standard close event. So you can see we don’t actually need the additional method suggested earlier, as we can handle this within the event parameters.
Each event contains programming that is triggered and can then execute specific tasks. Here’s an example written in pseudo code:
/* event triggered when door is closed, returns true or false */
/* execute code when door slammed */
/* execute code when door closed */
/* return value */
Good code is well commented, making it easier to write and maintain, in fact you could write an entire application in pseudo code and then hand it over to a coder to fill in the gaps. These comments are a little verbose as you would never make a comment like “execute code when door slammed” because a coder can read the code and it’s unnecessary. Comments should be added when the code itself is written using assumptions that might not be immediate obvious.
Here’s some real comments:
//SESSION MANAGEMENT HANDLED WITHIN SESSION-WARNING-AJAX.PHP
//SESSION-WARNING-AJAX.PHP FIRED BY AJAX IN MAIN.HTML HEADER (SEE APPSPEC.JS)
//SESSION LIFE TIME SET IN GLOBAL.CONFIG.PHP (SEE SESSION_LIMIT)
And here’s the comments:
//NEW CB2.6 SECURITY SETTINGS
//ERROR REPORTING (SEE http://php.net/manual/en/function.error-reporting.php)
//START :: NEW CB2.5 ERROR MESSAGING THAT NEEDS ADDING TO ALL PROJECTS
//END :: NEW CB2.5 ERROR MESSAGING THAT NEEDS ADDING TO ALL PROJECTS
//LIBS & GLOBAL VARS
//RETRIEVE MOD (OR USE DEFAULT MOD)
//SESSION LIMIT DEFINED ON GLOBAL.CONFIG.PHP
//SESSION START RESET EVERTIME PAGE IS LOADED
//CALCULATE REMAINING TIME; NOTE: INTENTION REVERSE OF TIMES IN DATEDIFF TO RETURN A NEGATIVE NUMBER (IN MINUTES)
//KILL SESSION WHEN TIME REMAINING IS ZERO OR LESS
//DON’T SHOW WHEN 5 MINUTES + REMAINING
//DISPLAY WARNING WHEN LESS THAN 5 MINUTES REMAINING
//DISPLAY ENHANCED WARNING WHEN LESS THAN 2 MINUTES REMAINING
Events can set properties, so if the doorClose() returned “True”, the status could be set to Closed.
The really cool bit about OOP is that it can be extended or inherited, so that you build up functionality making an object (or underlying class) more complex / functionality. So, you can extend our simple door into something really cool:
This is achieved by extended the object, i.e. adding to the objects properties, methods and events. But you don’t start again, you re-use the the object we created for our simple door and simple build it up from there. This means you already have the open, close methods and events written. But hang on, I hear you say, Gandalf didn’t just walk up to the entrance of Moria and open the door, he had a devil of a problem opening it!
So we would inherit the open method and event and add the additional procedures to the inherited openDoor() event.
As a programmer, you need to ensure the base class that the object uses isn’t too specific, otherwise you couldn’t extend it and you would have to create an entire new class, just to create a new door for Gandalf to open and that wouldn’t be efficient!
Wikepedia " the fountain of all knowledge " defined OOP as follows:
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A distinguishing feature of objects is that an object’s procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of “this”). In OO programming, computer programs are designed by making them out of objects that interact with one another. There is significant diversity in object-oriented programming, but most popular languages are class-based, meaning that objects are instances of classes, which typically also determines their type.
Many of the most widely used programming languages are multi-paradigm programming languages that support object-oriented programming to a greater or lesser degree, typically in combination with imperative, procedural programming. Significant object-oriented languages include Python, C++, Objective-C, Smalltalk, Delphi, Java, Swift, C#, Perl, Ruby and PHP.
At the core of our business is the philosophy of breaking down something to its simplest form, in much the same way Piet Mondrian approached his painting.The purist approach to a visual and emotional statement was the objective that the artist was trying to achieve in his deconstructive work, removing all unnecessary and distracting elements from the core message. We believe that our approach to software development should be based on the same foundations of pure function and end-usability, which is why our tag line is making complex tasks simple and why our logo pays homage to Piet Mondrian’s work.
This article isn’t aimed at programmers, but at people that want to understand what OOP is and what it can achieve, but is also a great introduction to Object Orientated Programming for students and I hope that you’ll find it interesting and informative; be great to get some feedback.
If you want to know more a good starting point would be Wikipedia, CodeBetter or OODesign.