The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,835 other subscribers

Delphi: the Factory Pattern with virtual Create Constructors (via: What Design Patterns do you implement in common Delphi programming? – Stack Overflow)

Posted by jpluimers on 2014/02/20

Delphi Component Design

Delphi Component Design

From long ago, but still very valid, as I recently had another question like “what design patterns does Delphi use?”.

The Delphi usage of patterns to make the VCL and your applications work is one of the reasons I like the Delphi Component Design: Danny Thorpe so much.
Do not let you scare by the book title: a lot of information in this book is much broader than designing components.
It is about why and how things are done in the RTL and VCL, and which patterns you can use yourself.

Try and git it while you can still get it. It is excellent, but rare to get as it has been out of print for a while.

Only a minority of the Delphi developers knows that every Delphi developer uses a Factory pattern (delphi.about.com has an example in “regular” Delphi), but then implemented using virtual Create constructors.

So: time to shed some light on that :-)

Virtual constructors are to classes like virtual methods are like object instances.

The whole idea of the factory pattern is that you decouple the logic that determines what kind (in this case “class”) of thing (in this case “object instance”) to create from the actual creation.

It works like this using virtual Create constructors:

TComponent has a virtual Create constructor so, which can be overridden by any descending class:

type
  TComponent = class(TPersistent, ...)
    constructor Create(AOwner: TComponent); virtual;
    ...
  end;

For instance the TDirectoryListBox.Create constructor overrides it:

type
  TDirectoryListBox = class(...)
    constructor Create(AOwner: TComponent); override;
    ...
  end;

You can store a class reference (the class analogy to an object instance reference) in a variable of type ‘class type’. For component classes, there is a predefined type TComponentClass in the Classes unit:

type
  TComponentClass = class of TComponent;

When you have a variable (or parameter) of type TComponentClass, you can do polymorphic construction, which is very very similar to the factory pattern:

var
  ClassToCreate: TComponentClass;

...

procedure SomeMethodInSomeUnit;
begin
  ClassToCreate := TButton;
end;

...

procedure AnotherMethodInAnotherUnit;
var
  CreatedComponent: TComponent;
begin
  CreatedComponent := ClassToCreate.Create(Application);
  ...
end;

The Delphi RTL uses this for instance here:

Result := TComponentClass(FindClass(ReadStr)).Create(nil);

and here:

// create another instance of this kind of grid
SubGrid := TCustomDBGrid(TComponentClass(Self.ClassType).Create(Self));

The first use in the Delphi RTL is how the whole creation process works of forms, datamodules, frames and components that are being read from a DFM file.

The form (datamodule/frame/…) classes actually have a (published) list of components that are on the form (datamodule/frame/…). That list includes for each component the instance name and the class reference.
When reading the DFM files, the Delphi RTL then:

  1. finds about the components instance name,
  2. uses that name to find the underlying class reference,
  3. then uses the class reference to dynamically create the correct object

A regular Delphi developer usually never sees that happen, but without it, the whole Delphi RAD experience would not exist.

Allen Bauer (the Chief Scientist at Embarcadero), wrote a short blog article about this topic as well.
There is also a SO question about where virtual constructors are being used.

Let me know if that was enough light on the virtual Create constructor topic :-)

–jeroen via: What Design Patterns do you implement in common Delphi programming? – Stack Overflow.

Posted in Delphi, Delphi 1, Delphi 2, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Development, Software Development | Tagged: , | 4 Comments »

HTTP protocol requires you to escape spaces (usually with %20 or with +), but web-browsers will do that for you

Posted by jpluimers on 2014/02/20

Since the time that spaces are allowed in path and file names, it has caused confusion.

I personally like the readability of  spaces, but still tend to avoid them as they usually cause more harm than the readability gains.

An interesting thread about spaces in file names is operating systems – What technical reasons exist for not using space characters in file names? – Super User.

In URLs, you there are various kinds of places where spaces can be used. You have to escape as Xah Lee wonders in does HTTP protocol require space be encoded in file path?.

The escaping is part of the URL Encoding, but the escapes depends on the position of the space. In the query part (after the first ?), you can have it escaped by both %20 and plus sign, but in the path part (before the first ? sign), it can only have a %20.

This is explained by bobince in urlencode – when to encode space to plus (+) and when to %20? – Stack Overflow.

That escaping basically makes path and file names a lot less readable when passed as a URL. It causes posts like these:

But why can you still use spaces when you type a URL in your web browser, or use it in a href, src or other HTML URL attribute?

Xah Lee rightfully earlier wondered about that in webserver – space in url; did browser got smarter or server? – Stack Overflow.

Technically, both are not allowed. But web browser manufacturers understand we humans are lazy, and accommodate for that by encoding these when putting them into the HTTP request.

You can type “https://www.google.com/search?q=foo bar” in your web browser, and depending on the browser, it gets translated into either one of these:

Recap:

  • encode spaces in URLs as %20
  • try to avoid spaces in path and filenames

–jeroen

via:

Posted in Development, Encoding, HTML, Software Development, URL Encoding, Web Development | Leave a Comment »

XanaNews word-wrap / NNTP text settings

Posted by jpluimers on 2014/02/19

These XanaNews settings are here:

Tools — Options — Posting Settings — Maximum Line Length=72

also

Text Format=NNTP

–jeroen

via Re: xananews – dumb question….

Posted in Delphi, Development, Software Development | Leave a Comment »

Delphi: `with` dos and dont’s (more of the latter though).

Posted by jpluimers on 2014/02/18

About a year ago, I wrote about Delphi: you should avoid the with statement as it makes your code less future proof. Then I already tweeted I would follow up. Time to do it now (:

Besides my first post, these links inspired me most:

Posts about the with statement usually cause a stir: people either like or dislike it with passion.

Starting with some history and examples, this posts lists a few DOs and DON’Ts when using the with statement, shows advantages and drawbacks, and shows you tools to eliminate with statements. Read the rest of this entry »

Posted in Borland Pascal, Delphi, Delphi 1, Delphi 2005, Delphi 2006, Delphi 2007, Delphi 2009, Delphi 2010, Delphi 3, Delphi 4, Delphi 5, Delphi 6, Delphi 7, Delphi 8, Delphi x64, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Development, Pascal, Software Development, Turbo Pascal, With statement | 9 Comments »

Google Fonts

Posted by jpluimers on 2014/02/17

Nice: Google Fonts. They started their own Font CDN.

Posted in Font, Power User | Leave a Comment »

T-Mobile USA: how to keep a SIM card active if you visit the USA infrequently

Posted by jpluimers on 2014/02/17

Last time when I was in the USA, I got myself a new T-Mobile USA SIM card with a local phone number so I could call and have data.

There are a few pre-paid T-Mobile USA plans interesting, depending on how long you visit the USA:

Voice+Text+Data:

  • Pay by the day USD 3/day for Unlimited Talk/Text/Data with the first 200 megabyte of data at 4G speed and the rest at 2G speed.
  • Pay by the day USD 2/day for Unlimited Talk/Text/Data with the data at 2G speed. 2G speed is maxed at 128 kilobit/second.
  • USD 50/month for 0.5 gigabyte of high-speed data and the rest at 2G speed. (works better for > 25 days of USD2/day).
  • USD 60/month for 2.5 gigabyte of high-speed data and the rest at 2G speed. (works better for > 20 days of USD3/day).
  • USD 70/month for unlimited high-speed data. (works better for > 23 days of USD2/day)

Data only:

  • 0.5/1.0/2.5 gigabyte per day for USD 10/15/30.
  • 0.5/2.5/4.5/6.5/8.5/10.5/12.5 gigabyte per month for USD 20/30/40/50/60/70/80.

Read the rest of this entry »

Posted in About, LifeHacker, Personal, Power User, Travel | Tagged: , | Leave a Comment »

Where to watch for StackOverflow / StackExchange downtime status: @StackStatus, @TechCrunch, @HNTweets

Posted by jpluimers on 2014/02/17

Basically, stackstatus.net is not actively updated when severe things like DDoS happen. So the best way to be kept up to date is watch these links:

–jeroen

Posted in Pingback, SocialMedia, Stackoverflow | Tagged: , | Leave a Comment »

Vulcan Point Island: An island in a lake, inside an island, inside another lake inside an island inside the ocean.

Posted by jpluimers on 2014/02/17

I forgot who brought this up in one of my circles a while ago, but this is fun:

An island in a lake, inside an island, inside another lake inside an island inside the ocean.

From the outside in:

  1. Sea (South Chinese Sea/Philippine Sea/Pacific Ocean)
  2. Luzon island of the Philippines
  3. Taal Lake
  4. Volcano Island
  5. Main Crater Lake
  6. Vulcan Point Island

Isn’t the earth a magnificent planet (:

–jeroen

via: Taal Lake – Wikipedia, the free encyclopedia.

Posted in LifeHacker, Power User | Leave a Comment »

When the VideoCast Sample App on Android does not detect cast device (via: Stack Overflow)

Posted by jpluimers on 2014/02/16

Quite a few things to check when the VideoCast Sample App on Android does not detect cast device – Stack Overflow.

–jeroen

Posted in Android, Android Devices, Development, Mobile Development, Power User, Software Development | Leave a Comment »

Android Codes (via Ailton dos Reis – Google+ – Códigos Android)

Posted by jpluimers on 2014/02/16

Need to test these, looks handy for troubleshooting: Ailton dos Reis – Google+ – Códigos Android.

–jeroen

Posted in Android Devices, Power User | Leave a Comment »