
|
|
|||||||
What is Object Oriented Programming (OOP)?By Tony Marston2007-01-30
Practical Examples Here are some practical examples which demonstrate Encapsulation, Inheritance and Polymorphism. Encapsulation
Every application deals with a number of different entities or "things", such as "customer" "product" and "invoice", so it is common practice to create a different class for each of these entities. At runtime the software will create one or more objects from each class definition, and when it wants to do something with one of these entities it will do so by calling the relevant method on the relevant object. The data held within each object at runtime cannot remain in memory for ever, so it is written out to a persistent data store (a database) with a separate table for each entity. There are only four basic operations which can be performed on a database table (Create, Read, Update, Delete) so I shall start by creating a method for each one. class entity1 Please note the following:
Each of these classes therefore acts as a 'capsule' which contains both the data for an entity and the operations which can be performed upon that data. This is 'encapsulation'. Inheritance
After writing and testing a class to deal with 'entity1' I copied it and made it work for 'entity2'. I then compared the two classes to see what code was common and could be shared, and what code was unique and could not be shared. I then transferred all the common code into a separate class known as a 'superclass'. Firstly, to create the superclass, I changed the class name and the constructor to the following: class default This class cannot be used to instantiate a working object as it does not refer to a database table which actually exists, so it is what is known as an 'abstract' class. Secondly, I altered each table class to remove the common methods and properties, and included the keyword extends to force inheritance from the superclass.
include 'default.class.inc'; When a subclass is instantiated into an object that object will contain all the properties and methods of the superclass as well as those of the subclass. If anything has been defined in both the superclass and the subclass, then the definition from the subclass will be used. In my current development environment the superclass contains several thousand lines of code, but there is only one copy of this code which is inherited by several dozen table classes. Inheritance is therefore a powerful mechanism for making one copy of common code accessible to many objects instead of having multiple copies of that common code. Polymorphism
Polymorphism can only be employed where the same method names exist in several classes. This means that the same method can be used on different objects, but will yield different results as the implementation of that method is different within each class. For example, take a series of classes called 'Customer', 'Product' and 'Invoice'. One practice I have seen which makes polymorphism impossible is to incorporate the entity name into the method name, as in:
The problem with this approach is that the object (the controller in MVC) which communicates with each table object (the model in MVC)needs to know the method name before it can open up that channel of communication. If each model has a unique set of method names then it must have a unique set of controllers to communicate with it. My approach is to use a standard set of method names for standard operations, as in:
This is made easier as these methods are defined in the superclass and made available to each subclass through inheritance. The advantage of this is that I can have one standard controller for each standard function, and this controller can work with any table class in the system. This is far better than having a separate set of controllers for each table class. Here is some example code from one of my controllers: .... The contents of $table and $where are made available at runtime.
The significant point is that the name of the class (database table) is not hard-coded into the controller, it is passed as an argument at runtime. Only the method names are hard-coded, but as these method names exist within every table class by being inherited from the superclass they will always work. So, if the class name is 'Customer' the controller will obtain data from the 'Customer' table, if it is 'Product' it will obtain data from the 'Product' table, and so on. Tutorial Pages: » Introduction » What OOP is NOT » What is an Object Oriented language? » What OOP is » The difference between OOP and non-OOP » Practical Examples » Conclusion » References |
||||||||
| About the NetVisits, Inc Network | Write For Us | Advertise Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy. |
Visit other NetVisits, Inc. sites: |