Search code examples
delphidesign-patternsdelphi-xe2

What is the name for this design pattern?


I often find myself writing classes that are used like this:

  • Create an object of the class
  • Call some "Run" or "Execute" method on that object
  • Destroy the object

This adds some overhead to the calling code, for example:

var
  Foo: TFoo;
begin
  Foo := TFoo.Create(...);
  try
    Foo.Run(...);
  finally
    Foo.Free;
  end;
end;

This could really be written much shorter as:

begin
  TFoo.Run(...);
end;

In this case the unit containing the TFoo class would look like this:

type
  TFoo = class
  private
    FBar: TBar;
    procedure InternalRun;
  public
    class procedure Run(ABar: TBar); static;
  end;

class procedure TFoo.Run(ABar: TBar);
var
  Foo: TFoo;
begin
  Foo := TFoo.Create;
  try
    Foo.FBar := ABar;
    Foo.InternalRun;
  finally
    Foo.Free;
  end;
end;

The overhead is moved from the calling code to the TFoo class.

What is the name of this design pattern?


Solution

  • If I have a quick look at the Portland Pattern Repository, the first part of your question resembles the MethodObject pattern pretty close.

    But since you're looking for the name of the latter bit, it's called a class method, and it's not a pattern, it's a language construct.