Board index » delphi » OOP isn't so hard

OOP isn't so hard

OOP should be extensible.  By using TJob and TJobs,
when you can get a job as a technical writer, you
can just add it to TJob and you are done.

TJob = (jTroll, jProgrammer, jBurgerFlipper, jTechWriter);

I would have made the JobCount a function call
on its own.

function GetJobCount: Integer;
var
   i: TJob;
begin
   Result := 0;
   for i := Low(TJob) to High(TJob) do
      if (x in fJobs) then Inc(Result);
end;

Quote
piddy wrote:
> Alessandro Federici wrote:

>> "piddy" <piddy...@veggienetcom.net> wrote in message
>> news:3e6f813a@newsgroups.borland.com...

>> TJob = (jTroll, jProgrammer, jBurgerFlipper);
>> TJobs = set of TJob;

>> TPiddyJob = class
>> private
>>    fJobs : TJobs;
>>    fJobCount : integer;

>>   procedure SetJobs(Value : TJobs);
>> public
>>   property JobCount : integer read fJobCount;
>>   property Jobs : TJobs read fJobs write SetJobs;
>> end;

>> procedure TPiddyJob.SetJobs(Value : TJobs);
>> var i : TJob;
>> begin
>>   fJobCount := 0;
>>   fJobs := Value;

>>   for i := Low(TJob) to High(TJob) do
>>     if (x in Value) then Inc(fJobCount);
>> end;

>> MUCH BETTER ;-)

> It's unreadable. If you want unreadable use C++.

> Hope this helps!

> piddy

--
Thomas Miller
Delphi Client/Server Certified Developer
BSS Accounting & Distribution Software
BSS Enterprise Accounting FrameWork

http://www.bss-software.com
http://sourceforge.net/projects/dbexpressplus

 

Re:OOP isn't so hard


Quote
"piddy" <piddy...@veggienetcom.net> wrote in message

news:3e6f8e59@newsgroups.borland.com...

[...]

Quote
> It's unreadable. If you want unreadable use C++.

How long have you been programming?  Alessandro used a perfectly
straightforward construct that is easily extensible.

--

 A: Top-posters.
 Q: What is the most annoying thing on Usenet?

Re:OOP isn't so hard


Quote
"Thomas Miller" <tmil...@bss-software.com> wrote in message

news:3e6f9857$1@newsgroups.borland.com...

Hi Tomas,

Quote
> I would have made the JobCount a function call on its own.

Given the context, that is just redundant IMO and you are forcing a
calculations everytime
you read the property for no reason.

Try putting that in a for i := 0 to 100.000  loop and see the difference ;-)

--
Best regards,
  Alessandro Federici

RemObjects Software, Inc.

www.remobjects.com
a...@remobjects.com

Re:OOP isn't so hard


Quote
"Alessandro Federici" wrote:
> > It's unreadable. If you want unreadable use C++.

> Ask yourself a few questions before making comments like this:

I think he was referring to your formatting.

--
Dave Nottage (TeamB)

Re:OOP isn't so hard


Quote
> I would have made the JobCount a function call
> on its own.

Me too,  but it depends on the use of JobCount.  If you know that there is a
possibility that JobCount will be referenced many times, it maybe a good
thing to save as a separate variable.

Re:OOP isn't so hard


"Dave Nottage (TeamB)" <dav...@nospamola.smartchat.net.au> wrote in message
news:3e6f9a21@newsgroups.borland.com...

Quote
> I think he was referring to your formatting.

That is why I asked question (4).

Re:OOP isn't so hard


How about:

TJob = class
public
  function GetJobName : string: virtual; abstract;
end;

TTroll = class(TJob)
public
  function GetJobName: string; override; // returns "Troll"
end;

TProgrammer = class(TJob)
public
  function GetJobName: string; override; // returns "Programmer"
end;

TBurgerFlipper = class(TJob)
public
  function GetJobName: string; override; // returns "BurgerFlipper"
end;

TPiddyJob = class
private
   FJobList : TObjectList; // assume created in constructor, freed in
destructor <lazy>
  function GetJobCount : integer; // return FJobList.Count
public
  property JobCount : integer read GetJobCount;
  procedure SetJobs( Jobs : array of TJob);
  function GetJobNames: string;
end;

procedure TPiddyJob.SetJobs( Jobs : array of TJob );
var i : integer;
begin
   FJobList.Clear;
   for i := Low(Jobs) to High(Jobs) do
        FJobList.Add(Jobs[i]);
end;

function TPiddyJob.GetJobNames : string;
var i : integer;
begin
   Result := '';
   for i := 0 to FJobList.Count-1 do
        Result := Result + TJob(FJobList[i]).GetJobName() + ',';
   Delete(Result, Length(Result)-1,1);
end;

--
Deepak Shenoy (TeamB)
Agni Software
http://www.agnisoft.com

Re:OOP isn't so hard


"Deepak Shenoy \(TeamB\)" <shenoy.donotspam@agnisoftdotcom> wrote in
news:3e6f9cb1@newsgroups.borland.com:

Quote
> How about:

> TJob = class
> public
>   function GetJobName : string: virtual; abstract;
> end;

If I went the route I might do something like include

TJobClass = class of TJob;

TPiddyJob = class
private
   FJobList : TObjectList; // assume created in constructor, freed in
 destructor <lazy>
   function GetJobCount : integer; // return FJobList.Count
 public
   property JobCount : integer read GetJobCount;
   procedure SetJobs( Jobs : array of TJobClass);
   function GetJobNames: string;
 end;

procedure TPiddyJob.SetJobs( Jobs : array of TJobClass );
var i : integer;
 begin
    FJobList.Clear;
    for i := Low(Jobs) to High(Jobs) do
         FJobList.Add(Jobs[i].Create);
 end;

and

add AddJob, RemoveJob, HasJob methods to TPiddyJob

Quote
> function TPiddyJob.GetJobNames : string;
> var i : integer;
> begin
>    Result := '';
>    for i := 0 to FJobList.Count-1 do
>         Result := Result + TJob(FJobList[i]).GetJobName() + ',';
>    Delete(Result, Length(Result)-1,1);
> end;

Thanks for this part.  For some reason I've always jumped through hoops
NOT to add the comma on the last item.  I've done that way so many times it
never occurred to me to add the comma and just delete the last one
afterwards.  

--
Iman

There appear to be absolutely no rules in Hollywood. It is anarchy.
Hollywood is essentially Thunder Dome with fake {*word*12}. - Peter DeWolf

Re:OOP isn't so hard


"Iman L Crawford" <ilcrawford.at.hotmail.dot.com> wrote in message
news:Xns933C993BAEEE1ilcrwfrd@207.105.83.65...

Quote
> "Deepak Shenoy \(TeamB\)" <shenoy.donotspam@agnisoftdotcom> wrote in
> news:3e6f9cb1@newsgroups.borland.com:
> If I went the route I might do something like include
> TJobClass = class of TJob;
[..]
>          FJobList.Add(Jobs[i].Create);

Only doable in Delphi unfortunately ;-)
You should use a class factory to make it language neutral.

Quote
> Thanks for this part.  For some reason I've always jumped through hoops
> NOT to add the comma on the last item.  I've done that way so many times
it
> never occurred to me to add the comma and just delete the last one
> afterwards.

ROTFLMAO!!!
I've always been struggling with that too but settled on the Delete at the
end!!!!
The Delete still bugs me anyways. I'd like to avoid it but there's no way I
could find to do it
ni a faster way. The IFs that you require would just slow the thing down too
much.

Re:OOP isn't so hard


"Alessandro Federici" <alef@[nospam]remobjects.com> wrote in
news:3e6fa1dc$1@newsgroups.borland.com:

Quote
> Only doable in Delphi unfortunately ;-)

You can do it in Python too.

Quote
> You should use a class factory to make it language neutral.

Why would I want my Delphi code to be language nuetral?

Quote
> ROTFLMAO!!!
> The IFs that you require would just slow the thing
> down too much.

They're ugly too.

--
Iman

There appear to be absolutely no rules in Hollywood. It is anarchy.
Hollywood is essentially Thunder Dome with fake {*word*12}. - Peter DeWolf

Re:OOP isn't so hard


"Iman L Crawford" <ilcrawford.at.hotmail.dot.com> wrote in message
news:Xns933C9B64A4B1Dilcrwfrd@207.105.83.65...

Quote
> You can do it in Python too.

Ah! Good to know.
There's a recent thread in the .Net newsgroups in which Shiv and I had a hot
discussion about the beauty of virtual constructors (which
for me are a thing every language should have) ;-)

Quote
> Why would I want my Delphi code to be language nuetral?

Because the thread is about OOP ;-))
Plus I wanted to say that <G>

Quote
> They're ugly too.

Hell yes!

--
Best regards,
  Alessandro Federici

RemObjects Software, Inc.

www.remobjects.com
a...@remobjects.com

Re:OOP isn't so hard


Quote
"Josh P." <jp457...@hotmail.com> wrote in message

news:3e6f8af9$1@newsgroups.borland.com...

Quote
> "Frederic Gelinas" <frederic_geli...@hotmail.com> wrote

> > procedure TPiddyJob.AddOccupations(T, P, B : boolean);
> >  begin
> >     with PiddyJobs do
> >     begin
> >       JobCount := 0;
> >       if T then Inc(JobCount);
> >       if P then Inc(JobCount);
> >       if B then Inc(JobCount);
> >     end;
> >  end;

> Thank you! Comparing booleans to constants bugs me. Where to stop?

> If ((X=False)=True)=False ...

With variable naming:

procedure TPiddyJob.AddOccupations(const aIsTroll, aIsProgrammer,
aIsBottleWasher : boolean);
begin
  with PiddyJobs do
    begin
      if aIsTroll then Inc(JobCount);
      if aIsProgrammer then Inc(JobCount);
      if aIsBottleWasher then Inc(JobCount);
    end;
end;

Of course, if JobCount isn't a variable, the call to Inc will fail on
compile.

John

Re:OOP isn't so hard


"Alessandro Federici" <alef@[nospam]remobjects.com> wrote in
news:3e6fa59f$1@newsgroups.borland.com:

Quote
> Ah! Good to know.
> There's a recent thread in the .Net newsgroups in which Shiv and I had
> a hot discussion about the beauty of virtual constructors (which
> for me are a thing every language should have) ;-)

They're not really virtual constructors as almost everthing is treated as
an object.  

Here's a simple example from Learning Python

def factory(aClass, *args):
  return apply(aClass, args)

class Spam:
  def doIt(self, message):
    print message

class Person:
  #__init__ is called when an object is created
  def __init__(self, name, job):
    self.name = name
    self.job = job

object1 = factory(Spam)
object1.doIt('Test') #prints test to the stdout

object1 = factory(Person, "Guido", "guru")
print object1.name #prints "Guido to the stdout

--
Iman

There appear to be absolutely no rules in Hollywood. It is anarchy.
Hollywood is essentially Thunder Dome with fake {*word*12}. - Peter DeWolf

Go to page: [1] [2] [3] [4] [5] [6] [7] [8]

Other Threads