Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why make a class with only 1 static function?

Tags:

delphi

Recently I ran into the following code:

interface

TSomeClass=Class
public
  class function SomeFunction(sMyString: string) : ISomeInterface;
end;

implementation

TSomeClass.SomeFunction(sMyString: string) : ISomeInterface;
begin
  ...Get some dependency.
end;

Basically a class with 1 class function in it.

What's the benefit of this construct over just having the function in a unit without it being part of a class?

like:

interface
function SomeFunction(sMyString: string) : ISomeInterface;

implementation

SomeFunction(sMyString: string) : ISomeInterface;
begin
  ...Get some dependency.
end;
like image 200
Pieter B Avatar asked Jul 16 '14 09:07

Pieter B


People also ask

What is the benefit of making a class static?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

What is main purpose of using static in a class?

In Java, static keyword is mainly used for memory management. It can be used with variables, methods, blocks and nested classes. It is a keyword which is used to share the same variable or method of a given class. Basically, static is used for a constant variable or a method that is same for every instance of a class.

Is it allowed to create a class having only static methods?

As long as it has no state, you're fine. Static classes are OK, just don't forget to add a private constructor.

Why You Should Avoid static classes?

Static classes have several limitations compared to non-static ones: A static class cannot be inherited from another class. A static class cannot be a base class for another static or non-static class. Static classes do not support virtual methods.


1 Answers

It largely comes down to personal choice.

However, one benefit that the class function provides is a means to fake namespaces. Suppose that the function name that you wanted to use was quite short and general. In that situation it might collide with another symbol having the same name, defined in a different unit. At which point you might be subject to the vagaries of unit use order, and may need to fully qualify the name. By using a class function, you force the user to qualify the function name with its class.

Another point to make here is that the two alternatives that you present have a potentially significant difference. The class function in your question has a Self pointer. Unlike for an instance method, this refers to the class rather than an instance. To make the two functions completely equivalent you would declare the class function to be static.

class function SomeFunction(sMyString: string): ISomeInterface; static;

Of course, one thing that can be done with a non-static class function, is that it can be used where an of object method type is required.

like image 59
David Heffernan Avatar answered Sep 22 '22 03:09

David Heffernan