corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Real world examples of classes in OOP


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Real world examples of classes in OOP
gooner_47
Member

Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
7th Aug 14 at 10:36   View User's Profile U2U Member Reply With Quote

OK, so I understand the concept. Fine with that. But all the examples in courses/tutorials are for things like "shapes" or "fruit".

Anyone got any real world examples of when you might use classes (in my case in client-side Javascript)?

Ta
ed
Member

Registered: 10th Sep 03
User status: Offline
7th Aug 14 at 17:33   View User's Profile U2U Member Reply With Quote

jQuery uses objects.
DaveyLC
Member

Registered: 8th Oct 08
Location: Berkshire
User status: Offline
8th Aug 14 at 20:56   View User's Profile U2U Member Reply With Quote

Not really because JavaScript technically isn't OO.. You can define classes but its not strictly OO.

Biology is a great example of OO, inheritance and abstract classes.

First of all you need to understand Types, a type defines an object and a type can be inherited and modified to create a new Type and therefore new objects.

You could say TAnimal is a type and TMonkey inherits TAnimal..

TMonkey would have its own properties that are only relevant to TMonkey and TAnimal would have properties that are relevant to all descendants.

So to make a monkey object we would do:

monkey = new TMonkey

But we can also pass TMonkey as TAnimal (because TAnimal is an ancestor of TMonkey).

function detectAnimal(animal: TAnimal)
{
if (animal is TMonkey) print('ITS A MONKEY');
else if (animal is TMouse) print('ITS A MOUSE');
else if (animal is TElephant) print('ITS A ELEPHANT;
else print('no fucking idea!');
}


Your objects can have infinite levels of inheritance.

TCat could inherit TAnimal but TDomesticCat could inherit TCat and so on..


I've probably just confused you even more now

[Edited on 08-08-2014 by DaveyLC]
gooner_47
Member

Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
10th Aug 14 at 09:24   View User's Profile U2U Member Reply With Quote

Thanks. Nah I get that completely, I do understand inheritance.

My point was, when is a web application you're writing ever going to deal with animals and biology?

I was just looking for some more realistic scenarios for using inheritance in an average website.
DaveyLC
Member

Registered: 8th Oct 08
Location: Berkshire
User status: Offline
10th Aug 14 at 12:38   View User's Profile U2U Member Reply With Quote

Its an example.. Your objects and classes will be subjective depending on what YOU want to achieve.
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Online
10th Aug 14 at 14:07   View Garage View User's Profile U2U Member Reply With Quote

See this is why I hate OO
DaveyLC
Member

Registered: 8th Oct 08
Location: Berkshire
User status: Offline
10th Aug 14 at 14:18   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by gooner_47
I was just looking for some more realistic scenarios for using inheritance in an average website.


Well a 'user' object would be one example if you are using any sort of authentication.. then 'session'.

Laney
Member

Registered: 6th May 03
Location: Leeds
User status: Offline
11th Aug 14 at 11:03   View User's Profile U2U Member Reply With Quote

Would something like this be a bit more useful?

code:

class User
def login
...
return "Login successful"
end
end

class AdminUser < User
...
def is_admin
return true
end
end

class ClientAdminUser < AdminUser
...
def can_edit
return false
end
end

class SuperAdminUser < AdminUser
...
def can_edit
return true
end
end

@client = ClientAdminUser.new
puts @client.login # "Login successful"
puts @client.can_edit? # false

@super_admin = SuperAdminUser.new
puts @super_admin.login # "Login successful"
puts @super_admin.can_edit? # true

AndyKent
Member

Registered: 3rd Sep 05
User status: Offline
11th Aug 14 at 18:24   View User's Profile U2U Member Reply With Quote

I've never used it for web apps.

For programs where you are handling larger data sets it is useful to break down the information into manageable chunks.

For example, in the past I wrote an application to handle betting via betfair.

When looking at a particular match I would take the data they issue via their API and parse it into my own types to streamline data handled internally within the app.

For example, a type being a football match, a sub type being data about what has happened in the match etc.

I can't see the same need to handle large amounts of data in a web app (since it will be stored by name in a database)
ed
Member

Registered: 10th Sep 03
User status: Offline
11th Aug 14 at 19:31   View User's Profile U2U Member Reply With Quote

If you're doing MVC style programming then you'd be doing some proper OOP. Using the biology example, you'd probably have an Animal model.

MVC in JS is pretty specialist stuff though, and a bit of a pain in the arse.
DaveyLC
Member

Registered: 8th Oct 08
Location: Berkshire
User status: Offline
11th Aug 14 at 19:36   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Laney
Would something like this be a bit more useful?

code:

class User
def login
...
return "Login successful"
end
end

class AdminUser < User
...
def is_admin
return true
end
end

class ClientAdminUser < AdminUser
...
def can_edit
return false
end
end

class SuperAdminUser < AdminUser
...
def can_edit
return true
end
end

@client = ClientAdminUser.new
puts @client.login # "Login successful"
puts @client.can_edit? # false

@super_admin = SuperAdminUser.new
puts @super_admin.login # "Login successful"
puts @super_admin.can_edit? # true




That's a really complex way of doing it.. TUser.Privilages would be better.. No need to get carried away with inheritance when normalisation will suffice.
Laney
Member

Registered: 6th May 03
Location: Leeds
User status: Offline
12th Aug 14 at 20:23   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by DaveyLC
That's a really complex way of doing it.. TUser.Privilages would be better.. No need to get carried away with inheritance when normalisation will suffice.


I apologise.
DaveyLC
Member

Registered: 8th Oct 08
Location: Berkshire
User status: Offline
13th Aug 14 at 07:52   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by Laney
quote:
Originally posted by DaveyLC
That's a really complex way of doing it.. TUser.Privilages would be better.. No need to get carried away with inheritance when normalisation will suffice.


I apologise.


Dont get me wrong I'm not trying to be an arse, its refreshing to see people actually considering objects these days so many programmers are just happy working with events and settings it makes baby Jesus cry.
gooner_47
Member

Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
13th Aug 14 at 11:46   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by ed
If you're doing MVC style programming then you'd be doing some proper OOP. Using the biology example, you'd probably have an Animal model.

MVC in JS is pretty specialist stuff though, and a bit of a pain in the arse.


I'm just getting to this now... I've been following this "course":

http://javascriptissexy.com/how-to-learn-javascript-properly/

Down in Week 7 it says to learn Backbone.js. I haven't actually gone through that section of it yet, but just reading articles about that and other MVC frameworks, tricky concept for me to get my head around. I'm sure it'll become clearer as I actually work through examples.
ed
Member

Registered: 10th Sep 03
User status: Offline
13th Aug 14 at 16:29   View User's Profile U2U Member Reply With Quote

MVC in JavaScript seems pretty complex - I've been trying to get something up and running use Ember and it seems way more complicated than any of the PHP stuff I do.
A2H GO
Member

Registered: 14th Sep 04
Location: Stoke
User status: Offline
13th Aug 14 at 19:20   View User's Profile U2U Member Reply With Quote

I'm building a web app for the company I work for using Ionic and AngularJS at the moment. Never written Javascript before or used this platform but pretty straightforward tbf (ps. Javascript sucks )

[Edited on 13-08-2014 by A2H GO]
gooner_47
Member

Registered: 20th Jul 04
Location: Bexhill/Croydon
User status: Offline
13th Aug 14 at 23:30   View User's Profile U2U Member Reply With Quote

Why does it suck?
DaveyLC
Member

Registered: 8th Oct 08
Location: Berkshire
User status: Offline
14th Aug 14 at 07:30   View User's Profile U2U Member Reply With Quote

JavaScript sucks because its not really object or pointer based.. For example when using an 'event' function it literally passes the code of the function so everything else is out of scope.

BUT for what it does its brilliant, what else is so cross-platform?

[Edited on 14-08-2014 by DaveyLC]
Rob_Quads
Member

Registered: 29th Mar 01
Location: southampton
User status: Offline
14th Aug 14 at 09:38   View User's Profile U2U Member Reply With Quote

Javascript bugs me because it doesn't have anything to do with Java lol
DaveyLC
Member

Registered: 8th Oct 08
Location: Berkshire
User status: Offline
14th Aug 14 at 09:43   View User's Profile U2U Member Reply With Quote

Other than having the same syntax?
A2H GO
Member

Registered: 14th Sep 04
Location: Stoke
User status: Offline
14th Aug 14 at 11:13   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by gooner_47
Why does it suck?


It sucks because I like Objective-C so much.
DaveyLC
Member

Registered: 8th Oct 08
Location: Berkshire
User status: Offline
14th Aug 14 at 11:56   View User's Profile U2U Member Reply With Quote

quote:
Originally posted by A2H GO
quote:
Originally posted by gooner_47
Why does it suck?


It sucks because I like Objective-C so much.


Odd-bod

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Matrix Revolutions discussion (spoilers possible!) kerzo General Chat 24 1698
8th Nov 03 at 12:43
by John_C
 
More help needed again Ryan L General Chat 2 250
26th Mar 04 at 17:12
by Ryan L
 
Random As F*ck... Paul_J Geek Day 14 1001
4th Aug 08 at 11:35
by Jamescorsa97
 
Php help? (mysql_num_rows) Whittie Geek Day 17 1270
15th Jan 10 at 14:38
by Dom
 
Tempted with a Golf...pro's and con's? Gazdaman General Chat 58 1303
26th Apr 10 at 08:26
by Corsa_Sport21
 

Corsa Sport » Message Board » Off Day » Geek Day » Real world examples of classes in OOP 28 database queries in 0.0145311 seconds