As part of my working life I happy to say I use COBOL and for better or worse it is here to stay. With this in mind it annoys me I here/see saying things such as
COBOL is a old language that naturally prohibits you from using modern design patterns.
rubbish I say… COBOL can be used in good ways and bad ways.
I will try and show you that COBOL can be used in a good way… lets take the “Singleton pattern“, as the first example.
First.. lets start off my creating a singleton class in csharp… so here it is:
using System.Collections;
public sealed class MySingleton {
private static readonly Hashtable sharedHashtable = new Hashtable();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static MySingleton() {
}
private MySingleton() {
}
public static Hashtable Singleton {
get {
return sharedHashtable;
}
}
}
Not too shabby.. but lets see what we can do in COBOL…
$set ilusing"System.Collections"
class-id. "MySingleton".
01 shared-hashtable type "Hashtable"
static property as "Singleton" with no set.
method-id. "New" static.
set shared-hashtable to new type "Hashtable"
end method "New".
What… COBOL is smaller… that can’t be true… sorry but it is…
To complete the example… lets use it…
*> Add two items to the single hashtable
invoke type "MySingleton"::"Singleton"::
"Add"("01234567","Ian")
invoke type "MySingleton"::"Singleton"::
"Add"("987654321","Stephen")
*> Now get one of the items of the singleton
display "Account 01234567 - Contains: "
type "MySingleton"::"Singleton"::"Item"("01234567")
And I am sure some people… will say sure… this is really true… it is… here is it running..
c:temp> cobol MySingleton.cbl ilgen(sub);
c:temp> cobol UseSingleton.cbl ilgen ilref"MySingleton.dll";
c:temp> UseSingleton.exe
Account 01234567 - Contains: Ian
c:temp> csc MySingleton.cs /target:library
c:temp> UseSingleton.exe
Account 01234567 - Contains: Ian
Now it seems to me that COBOL is being under rated… perhaps someone should shout about it! 🙂
[ad#Google Adsense-3]