Many books and articles on OOP use examples that have nothing to do with a real world web programming problem, or at not something I usually come across. For that reason I think we'll try to look at something useful to us, like a user management system for a website including different permission levels for the users.
I'll leave it to you to actually build it in your language of choice if you choose, but if you do you'll find it easy to insert into any project which is one of the goals we're trying to accomplish with our development approach.
Ok, so we're going to setup a system for our website where users (Users) can register, be approved/edited/deleted by an admin, view different parts of a site based on their permissions (which we'll call Roles). Users can edit their own info. Admins can make new roles and adjust which User has what Role(s). An example of different Roles would be: Public, Special, Admin.
(If you're familiar with OOP you can skip the rest of this post.)
So let's look at a user, me... My name "Catalyst", my age "34", sex "Male", I can be Approved, or Deleted, etc. Since we'll be storing my data in a database let's give me a unique ID of 77.
A non-OO approach to dealing with me in code would be to create some variable and functions to handle my data and needs.
strName = "Catalyst"
intAge = 34
strSex = "Male"
intID = 77
boolApproved = TRUE
function Approve() {
// DB: Update a record in the database, changing Approved to TRUE for ID = 77
boolApproved = TRUE
}
function Delete() {
// DB: Delete database record for ID = 77
strName = ""
intAge = 0
strSex = ""
boolApproved = FALSE
}
etc...
Ok, so this example works fine. But what if we need to deal with more than one User at a time?
strName1 = "Catalyst"
strName2 = "Karl"
strSex1 = "Male"
strSex2 = "Male"
....
function DeleteCatalyst() {
// DB: Delete database record for ID = 77
strName1 = ""
intAge1 = 0
strSex1 = ""
boolApproved1 = FALSE
}
function DeleteKarl() {
// DB: Delete database record for ID = 55
strName2 = ""
intAge2 = 0
strSex2 = ""
boolApproved2 = FALSE
}
You can see the problem we start running into. Lots of code and variables to keep track of. Objects can help us simplify things.
When you think about it, those variables and functions are all about representing someone (a User) and the things a User can do. Using variables and functions is an abstract data centered way of thinking that we learn to do as programmers. Thinking of a User as a thing instead of this collection of data will help us build our code in more useful way.
So at it's core, Object Oriented Programming is about thinking about the data in our site as the real objects they represent, Users, Roles, Cars, Books, whatever. OOP has its own terminology for things, the basics are classes, properties, and methods.
A class is a generic template for an object, in our case we would have a User class and a Role class which are just blanks that we create instances of. Karl would be an instance of a User, as would Catalyst.
A class has properties, which are like variables to hold information related the that type of object, such as Age, Sex, Name. Methods are the functions that work with the class and properties, like Delete, Approve.
So our User class might look like:
class User {
int ID
string Name
int Age
string Sex
function Approve() {...}
function Delete() [...]
}
Now in our code we can create multiple users easily.
user1 = new User()
user1.Name = "Catalyst"
user1.ID = 77
user1.Approve()
user2 = new User()
user2.Name = "Karl"
user2.Name = 54
user2.Delete()
function PrintAName(someUser) {
Print(someUser.Name)
}
----------
PrintAName(user1)
"Catalyst"
So, there's a lot to OOP. Inheritance is one concept to read up on, also objects that contain other objects, collections of objects, and abstract and static methods and classes. There are some really good books out there on OOP, Amazon would be a good place to check for reviews. The web of course has articles, some over simplified, and many overly complex.
Here's some links to get you familiar with OOP as I work on the next part of this series.
http://www.cafeaulai...g/course/week3/
http://www.javaworld.com/javaworld/jw-04-2...06-java101.html
http://en.wikipedia....ted_programming
And of course Google your language of choice and OOP to find the specifics of using PHP, Flash, Javascript, Java, C#, vbscript, etc. If you find links to good articles please post back with them.


Help

MultiQuote











