Reply to topic  [ 5 posts ] 
Whats this OO principle called? 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
I'm trying to find some info about writing a class structure in Obj-C but I can't think of the name of the prinicple?

Basically I want a single Class called something like BasicStrategy but it will never be initialised into an Object (almost an Abstract Class?).

It will have certain methods like... sanityCheck, setState, execute, etc...

These will all do something except for the execute methods.

Then I want to create several other classes that inherit from the BasicStrategy but then override the execute method so that it does something different.

That way I can have a method inside BasicStrategy that does something like....

self.setState();
self.sanityCheck();
self.execute();

and then in (for instance) ComplexStrategy I can have the execute method containing the code for the complex strategy and it will then run that instead of whats in the BasicStrategy bit.

I hope that makes sense.

I knwo it has a name but I can't remember what it is to look it up.

Thanks all.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Sep 20, 2011 7:54 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
TBH I think I'm just overthinking it.

I think I just need to create a class and then subclass it.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Sep 20, 2011 8:36 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
I think the term is polymorphism?
ComplexStrategy would subclass BasicStrategy and override certain methods. Methods it didn't override would be called from BasicStrategy.
Both classes could be made static so they can't be instantiated, or they could be Abstract.

To have a ComplexStrategy instance you would sublass it (adding functionality). If you then called self.sanityCheck(), it would call the one from ComplexStrategy if it existed, if it doesn't exist in ComplexStrategy then it'll be called from BasicStrategy. Even then if you called something like self.isEqual it'll keep going up to NSObject, unless you've overridden it further down the line.

I've a feeling there is another way, I'll check later in my book at home.


Tue Sep 20, 2011 10:25 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
forquare1 wrote:
I think the term is polymorphism?
ComplexStrategy would subclass BasicStrategy and override certain methods. Methods it didn't override would be called from BasicStrategy.
Both classes could be made static so they can't be instantiated, or they could be Abstract.

To have a ComplexStrategy instance you would sublass it (adding functionality). If you then called self.sanityCheck(), it would call the one from ComplexStrategy if it existed, if it doesn't exist in ComplexStrategy then it'll be called from BasicStrategy. Even then if you called something like self.isEqual it'll keep going up to NSObject, unless you've overridden it further down the line.

I've a feeling there is another way, I'll check later in my book at home.

Cool, thanks!

Yes, that's the result I want.

What I want to do is put all the subclass objects of BasicStrategy into an array so that I don't care which one it is I know I can run certain methods in them and they will all work.

Thanks!

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Sep 20, 2011 10:38 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 6:36 pm
Posts: 5150
Location: /dev/tty0
Reply with quote
Cool :D

I think polymorphism is my favourite OO word 8-) :ugeek:


Tue Sep 20, 2011 3:05 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.