Jump to content

Website Architecture - Part 2 : OOP and our example

Basics of OOP and our user login example

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Website Architecture - Part 2 : OOP and our example Basics of OOP and our user login example Rate Topic: ***** 1 Votes

#1 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 17 December 2006 - 03:50 AM

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.

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.
0

#2 User is offline   Karl Buckland 

  • A.K.A. Sirkent
  • Group: Administrators
  • Posts: 2,145
  • Joined: 04-April 06
  • Gender:Male
  • Location:Kent, UK

Posted 17 December 2006 - 04:34 AM

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.
QUOTE(benbramz @ Aug 17 2007, 07:44 AM) Ive noticed that quite a few people are now adding quotes from the board into their signature. I think its started an new web-radiance craze.. :P
0

#3 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 17 December 2006 - 05:01 AM

Corrected, thanks.
0

#4 User is offline   Ben Abrams 

  • The buddy system:never fails
  • Group: Administrators
  • Posts: 1,850
  • Joined: 04-April 06
  • Gender:Male

Posted 17 December 2006 - 11:48 AM

Great article, and i look forward too seeing the rest in your series!

View PostSirkent, on 21 September 2007 - 04:26 AM, said:

<monty python high-pitched female voice>I DON'T LIKE SPAM!</monty python high-pitched female voice>
0

#5 User is offline   EncoderDecoder 

  • W.R. Sergeant
  • Group: Members
  • Posts: 260
  • Joined: 01-March 11
  • Gender:Female
  • Location:Philippines

Posted 11 July 2011 - 07:52 AM

wow, this is a very helpful article.. thanks for sharing your knowledge with us..
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users