Why register for free on WebRadiance? |
Why register for free on WebRadiance? |
![]() |
Welcome to WebRadiance forums! WebRadiance is a place for discussion and help on a large range of web design and development related topics such as HTML, CSS, ASP & .NET, PHP and SEO. It is provided for free, without ads and is a friendly place for beginners and professionals alike. By joining us today you can take part in our rapidly growing community. Registering enables you to:
|
![]() ![]() |
Dec 17 2006, 03:50 AM
Post
#1
|
|
![]() Codesmith Group: Administrators Posts: 992 Joined: 4-April 06 From: San Diego Member No.: 3 |
I'm not going to attempt to give an in-depth tutorial on Objected Oriented Programming (OOP). There's plenty of great resources out there and the syntax varies a bit by language. Instead we'll do a general overview and look at the example I'm going to use for the rest of the posts. At the end of this post I'll put some links to OOP tutorials for common languages. The syntax I use in the examples will be a mix that hopefully gets the concept across. "DB: something" will represent telling the database to do something.
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. CODE 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? CODE 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: CODE class User { int ID string Name int Age string Sex function Approve() {...} function Delete() [...] } Now in our code we can create multiple users easily. CODE 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.cafeaulait.org/course/week3/ http://www.javaworld.com/javaworld/jw-04-2...06-java101.html http://en.wikipedia.org/wiki/Object-oriented_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. |
|
|
|
Dec 17 2006, 04:34 AM
Post
#2
|
|
![]() Loose bits sink chips Group: Administrators Posts: 2,101 Joined: 4-April 06 From: Kent, UK Member No.: 2 |
Cat, you have a small spelling mistake 'So, there's a lot to OOP. Inhertiance', but good stuff so far!
OOP really is a life-saver on larger projects as I'm sure many of us will agree and I think it can be utilised even on smaller projects. Of course it takes a little additional time, but as Catalyst has pointed out, it can make your project so much easier to extend in the future. -------------------- |
|
|
|
Dec 17 2006, 05:01 AM
Post
#3
|
|
![]() Codesmith Group: Administrators Posts: 992 Joined: 4-April 06 From: San Diego Member No.: 3 |
Corrected, thanks.
|
|
|
|
Dec 17 2006, 11:48 AM
Post
#4
|
|
![]() The buddy system:never fails Group: Administrators Posts: 1,796 Joined: 4-April 06 Member No.: 1 |
Great article, and i look forward too seeing the rest in your series!
-------------------- |
|
|
|
![]() ![]() |
|
Lo-Fi Version | Time is now: 3rd July 2009 - 06:02 PM |