Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Marcos Douglas
On Sat, Nov 28, 2015 at 9:11 AM, Juha Manninen
 wrote:
> On Sat, Nov 28, 2015 at 1:48 AM, Marcos Douglas  wrote:
>> Another tip: Factories resolve some problems but there is a cost. The
>> factory will creates your instance (object) but it know only one
>> constructor, ie, the base class constructor.
>
> No, the constructor must then be virtual.

Even using virtual constructors, the factory will know only one
signature to instantiate objects.
AFAIK you only will know constructor or methods arguments, in runtime,
using RTTI, if you put the constructor/method as published. But, even
if you put it as published, you do not know the right context to call
the right constructor -- considering you have many constructors with
different arguments, of course.

> Inheritance is OK if all public virtual methods can be defined in an
> abstract base class. If works as an interface then (without
> "interface" keyword).

Inheritance breaks encapsulation. This was written even in GoF Patterns.
There are many post, on the web, explaining why inheritance is evil.
I searched on Google right now:
http://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/
http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html

> It does not work when you need TDog.Bark but it would work with
> abstract function TAnimal.Says: String, which is implemented as "
> Return 'wuff' " for TDoc and " Return 'miou' " for TCat.

Completing things is the source of complexity, ie, completing
(abstract or virtual) methods. To complete methods in a sub class, you
need to know how super class works and this violate the (first tier
of) encapsulation.

Best regards,
Marcos Douglas

PS: Perhaps this video could be interesting
http://www.infoq.com/presentations/Simple-Made-Easy
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Graeme Geldenhuys
Hi Marcos,

On 2015-11-27 23:48, Marcos Douglas wrote:
> Why do you have a factory to create different types of animals
> (classes)? Doesn't make sense.

Maybe in Luciano's simple example a factory is not the best use case,
but the Simple Factory design pattern can be very useful in other cases.

eg: tiOPF uses the Simple Factory extensively in many areas. eg:
encryption. tiOPF defines a base class with abstract methods for using
encryption. We then have various descendant classes that implement
different encryption. Each of the descendants register themselves with
the factory (tiOPF likes to use the uses clause and Initialization
section for this). In your own code you then request an encryption type
and get a instance object back. You use that instance via the API
(interface) defined by the abstract methods.


> Objects should be immutable at first place, only if you have a good
> reason for don't use immutability. So, if they should be immutable,

This has nothing to do with Luciano's problem.


> Another tip: use interfaces, not inheritance. Inheritance is evil.

Indeed, Interfaces might be a more elegant solution to Luciano's problem
- for what he wants to achieve. Inheritance is NOT evil. It's not that
black and white. Both Interfaces and Inheritance have their pros and
cons - you just need to study your problem and then decide which is the
better fit.

@Luciano,
What you described is the Simple Factory design pattern. I suggest you
also study the Factory Method design pattern, to see the difference and
where you can use them.

Two [I have many more suggestions too] Design Pattern books I highly
recommend are:

  * Design Patterns - Elements Of Reusable Object Oriented Software.
Also known as the GoF (Gang of Four) book.

  * Head First - Design Patterns
This one has a very different writing style, but I found this fun
take on design patterns works very well.


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Serguei TARASSOV

Hello,

What is an explanation why the first code is not good but the second one 
is good?

At least, the first code is three times as shorter and clear.
Second code seems to be taken from Delphi 7 where the first one doesn't 
works.


On 28/11/2015 12:00, fpc-pascal-requ...@lists.freepascal.org wrote:

>if Animal is IBarkable then (Animal as IBarkable).Bark;

This is not good usage of Interfaces. Use the interface variable instead.

var
   intf: IBarkable
begin
   if Supports(Animal, IBarbable, intf) then
   begin
 intf.Bark;
 // you can continue here using intf further without casting
   end;

--
Regards,
Serguei
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Marcos Douglas
On Sat, Nov 28, 2015 at 7:32 AM, Graeme Geldenhuys
 wrote:
>
> Maybe in Luciano's simple example a factory is not the best use case,
> but the Simple Factory design pattern can be very useful in other cases.

I agree in both affirmations. But as I said before, Factories resolve
some problems but there is a cost.

Thank for explanation about tiOPF.

>> Objects should be immutable at first place, only if you have a good
>> reason for don't use immutability. So, if they should be immutable,
>
> This has nothing to do with Luciano's problem.

I know. He is my friend a long time and so I wrote more information, just that.

Regars,

Marcos Douglas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Graeme Geldenhuys
Hi Anthony,

On 2015-11-28 00:16, Anthony Walter wrote:
> type
>   IBarkable = interface(IInterface)
>   ['{B241068F-2ED9-43C7-066B-778B94CB58F9}']
> procedure Bark;
>   end;
> ...snip...

That is a much better solution for what Luciano wants to accomplish.


> and later ...
> 
> if Animal is IBarkable then (Animal as IBarkable).Bark;

This is not good usage of Interfaces. Use the interface variable instead.

var
  intf: IBarkable
begin
  if Supports(Animal, IBarbable, intf) then
  begin
intf.Bark;
// you can continue here using intf further without casting
  end;



Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Juha Manninen
On Sat, Nov 28, 2015 at 1:48 AM, Marcos Douglas  wrote:
> Another tip: Factories resolve some problems but there is a cost. The
> factory will creates your instance (object) but it know only one
> constructor, ie, the base class constructor.

No, the constructor must then be virtual.
Inheritance is OK if all public virtual methods can be defined in an
abstract base class. If works as an interface then (without
"interface" keyword).
It does not work when you need TDog.Bark but it would work with
abstract function TAnimal.Says: String, which is implemented as "
Return 'wuff' " for TDoc and " Return 'miou' " for TCat.

But yes, the other answers are better for more complex cases.

Juha
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Marco van de Voort
In our previous episode, Marcos Douglas said:
> 
> Inheritance breaks encapsulation. This was written even in GoF Patterns.
> There are many post, on the web, explaining why inheritance is evil.
> I searched on Google right now:
> http://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/
> http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html

Such rants are in direct opposition of GoF. GoF never meant to push a set of
unbendable rules. They just wanted to standarize jargon, and describe
somethings they encountered (without any claim that everybody should use
it).

Aside from the fact that interfaces is still inheritance.

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] mips linux cross compiled application error

2015-11-28 Thread czd
Congratulations for the new fpc compiler and the developers, soon I achieved
to compile a simple test program for mips/linux platform. As I wrote before
my simple device is onion/omega and here is the cpuinfo and compiled
application(attached). It complains with the Illegal instruction replied..
Thanks.
test.gz
  
system type: Atheros AR9330 rev 1
machine: Onion Omega
processor: 0
cpu model: MIPS 24Kc V7.4
BogoMIPS: 265.42
wait instruction: yes
microsecond timers: yes
tlb_entries: 16
extra interrupt vector: yes
hardware watchpoint: yes, count: 4, address/irw mask: [0x0ffc, 0x0ffc,
0x0ffb, 0x0ffb]
isa: mips1 mips2 mips32r1 mips32r2
ASEs implemented: mips16
shadow register sets: 1
kscratch registers: 0
package: 0
core: 0
VCED exceptions: not available
VCEI exceptions: not available



--
View this message in context: 
http://free-pascal-general.1045716.n5.nabble.com/mips-linux-cross-compiled-application-error-tp5723048p5723192.html
Sent from the Free Pascal - General mailing list archive at Nabble.com.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Juha Manninen
On Sat, Nov 28, 2015 at 2:07 PM, Marcos Douglas  wrote:
> Even using virtual constructors, the factory will know only one
> signature to instantiate objects.

Yes and often that is enough.
A constructor is then part of the API. (Lets call it API now to avoid
confusion with the "interface" keyword.)

> Completing things is the source of complexity, ie, completing
> (abstract or virtual) methods. To complete methods in a sub class, you
> need to know how super class works and this violate the (first tier
> of) encapsulation.

Yes, if the super class does something useful by itself, then often
your class design is wrong.
My example was about an abstact base class that only provides an API,
the derived classes implement it. The concept is similar to an
interface definition, but often the implementation is more compact and
easier to read.
Interface can define only methods. An abstract base class can have
also variables and protected helper methods needed by the
implementation code.

We are partly talking about different things here. You and the articly
you linked advocate the "composition over inheritance" principle. Yes,
it is a good principle. I also advocate it, but I also say that using
abstract base classes for APIs is often a cleaner and easier way
compared to interface types.
Look at the Lazarus API defined in IDEIntf package. It uses abstract
base classes and works well.

Abstract base classes do not work in a multiple inheritance case. Then
an interface type must be used.
However it is very rarely needed when the "composition over
inheritance" principle is followed.

Juha
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Michael Van Canneyt



On Sat, 28 Nov 2015, Marcos Douglas wrote:


Inheritance breaks encapsulation. This was written even in GoF Patterns.
There are many post, on the web, explaining why inheritance is evil.
I searched on Google right now:
http://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/
http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html



A perfect example why 90% of internet (and in particular blogs) are a waste of 
bandwidth.


From the silly-example-department: (a close cousin of the "ministry of silly 
walks")


A dog *is* an animal, a dog does not *have* an animal.

However (and now it gets tricky): 
A human can have an animal (e.g. a dog), but she *is* also an animal.


On top of that, (s)he can inherit things from his ancestors. 
(for example AIDS or some other nasty disease. Or several millions of dollars)


!! OMG !! Maybe we just discovered a new form of multiple inheritance... 
Definite proof we should all use C++, or maybe it is time for C+++ !!


Please...

There is no golden rule which encapsulates (sic) all situations.

People should use their brain and choose a pattern or methodology judiciously, 
instead of wasting other people's time with stupid rants on blogs.


Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Building kernel modules using fpc (for fun)

2015-11-28 Thread Paul Breneman

On 01/23/2011 08:15 AM, Lukasz Sokol wrote:

Lukasz Sokol  writes:

I decided to put the result of my fiddling back onto the wiki
http://wiki.freepascal.org/linux/kernel/module_development  as the pastebin
seems to have disappeared mysteriously.

Enjoy!

Lukasz


I couldn't find anything but the above wiki page which is almost 5 years 
old.  I might want to make an example for this wiki page:

  http://wiki.freepascal.org/Small_Virtual_Machines

Any suggestions or links?

Regards,
Paul
www.ControlPascal.com

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Graeme Geldenhuys
On 2015-11-28 19:34, Michael Van Canneyt wrote:
> !! OMG !! Maybe we just discovered a new form of multiple inheritance... 
> Definite proof we should all use C++, or maybe it is time for C+++ !!

ROFL - you just made my day. :-)


> There is no golden rule which encapsulates (sic) all situations.

+1


Regards,
  - Graeme -

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Marcos Douglas
On Sat, Nov 28, 2015 at 3:31 PM, Marco van de Voort  wrote:
> In our previous episode, Marcos Douglas said:
>>
>> Inheritance breaks encapsulation. This was written even in GoF Patterns.
>> There are many post, on the web, explaining why inheritance is evil.
>> I searched on Google right now:
>> http://blog.berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/
>> http://www.javaworld.com/article/2073649/core-java/why-extends-is-evil.html
>
> Such rants are in direct opposition of GoF. GoF never meant to push a set of
> unbendable rules. They just wanted to standarize jargon, and describe
> somethings they encountered (without any claim that everybody should use
> it).

I only chose two links to put into the email. There are many other ...
I just pointed some examples because they have examples.

In my GoF book (Portuguese version), page 34, says:
...It is often said that "inheritance violates encapsulation"...
[free translation]

Page 35:
"Preferably the object composition instead of class inheritance."
[free translation]

>
> Aside from the fact that interfaces is still inheritance.

This is different. There is no behavior, only a contract.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Marcos Douglas
On Sat, Nov 28, 2015 at 5:34 PM, Michael Van Canneyt
 wrote:
>
> !! OMG !! Maybe we just discovered a new form of multiple inheritance...
> Definite proof we should all use C++, or maybe it is time for C+++ !!

You have humor, very funny. [clap clap clap]

> Please...
>
> There is no golden rule which encapsulates (sic) all situations.

I agree with you.

> People should use their brain and choose a pattern or methodology
> judiciously, instead of wasting other people's time with stupid rants on
> blogs.

As I said before, I put some links just because they have examples to
simplify what I was saying.
If you would like, I can search some links "less stupid" for you. Some
people, more important and influent than me, should have written about
it too... maybe?

Now, please...
Tell us yours arguments. Explain why I'm wrong (and many others on the web).
Inheritance is good... why?
Inheritance is better than composition... why?
What problems we have using composition or inheritance? Which is
better or simpler to use?

If I'm wrong and you, using valid arguments, explain to me where are
my mistakes, I will thank you for teaching me some new for me. But if
you have only jokes please don't waste my time to read and answer you.

Best regards,
Marcos Douglas

PS: I don't want a fight. We owe a lot to you with your work in the FPC.
I respect you as a programmer, as FPC Team member, as professional.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread patspiper

On 27/11/15 23:21, luciano de souza wrote:

...

But, if this not works, how to implement the Factory Method in Freepascal?

What about:

var
  animal: TDog;
BEGIN
  animal := TAnimalFactory.create(atDog);
  try
if animal is TDog then
  TDog(animal).bark;
  finally
animal.free;
  end;
END.

Stephano
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] Implementing Factory Method with Pascal

2015-11-28 Thread Inoussa OUEDRAOGO
> There is no golden rule which encapsulates (sic) all situations.

+1

> People should use their brain and choose a pattern or methodology
> judiciously,

+1, after all that is  what engineering is all about.


-- 
Inoussa O.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal