agentleFS
delphicleancodedelphicleancode/delphi-spec-kit/.cursor/rules/design-patterns.md

delphi-spec-kit / rules

Cursor rule53 starsChanged 8 months agoMITView on GitHub

Design Patterns (GoF) in Delphi — Creational, Structural and Behavioral with Object Pascal

The file

We split this design-patterns.md into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use these rules when implementing design patterns in Object Pascal/Delphi.

    line 9

≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
design-patterns.md

Design Patterns GoF — Cursor Rules for Delphi

Use these rules when implementing design patterns in Object Pascal/Delphi.


🏗️ Creational Patterns

Factory Method

//Product Interface
IButton = interface
  ['{GUID}']
  procedure Render;
end;

//Abstract Creator
TButtonFactory = class abstract
public
  function CreateButton: IButton; virtual; abstract;
end;

//Concrete implementations
TVCLButton = class(TInterfacedObject, IButton)
  procedure Render;
end;

TFMXButton = class(TInterfacedObject, IButton)
  procedure Render;
end;

Abstract Factory

//Abstract factory interface
IUIFactory = interface
  ['{GUID}']
  function CreateButton: IButton;
  function CreateDialog: IDialog;
end;

//Concrete factories by platform
TVCLFactory = class(TInterfacedObject, IUIFactory)
  function CreateButton: IButton;
  function CreateDialog: IDialog;
end;

TFMXFactory = class(TInterfacedObject, IUIFactory)
  function CreateButton: IButton;
  function CreateDialog: IDialog;
end;

Singleton

//Singleton thread-safe em Delphi
TAppConfig = class
private
  class var FInstance: TAppConfig;
  constructor Create;
public
  class function GetInstance: TAppConfig;
  class procedure ReleaseInstance;
  property DatabaseUrl: string read FDatabaseUrl write FDatabaseUrl;
end;

class function TAppConfig.GetInstance: TAppConfig;
begin
  if not Assigned(FInstance) then
    FInstance := TAppConfig.Create;
  Result := FInstance;
end;

⚠️ For multi-threaded environments use TCriticalSection when creating the instance.

Builder

//Builder for building complex queries
TQueryBuilder = class
private
  FSql: TStringBuilder;
  FParams: TDictionary<string, Variant>;
public
  constructor Create;
  destructor Destroy; override;
  function Select(const AFields: string): TQueryBuilder;
  function From(const ATable: string): TQueryBuilder;
  function Where(const ACondition: string): TQueryBuilder;
  function OrderBy(const AField: string): TQueryBuilder;
  function Build: string;
end;

//Fluent use (Fluent Interface)
var LSql: string;
begin
  LSql := TQueryBuilder.Create
    .Select('id, name, email')
    .From('customers')
    .Where('active = 1')
    .OrderBy('name')
    .Build;
end;

⚠️ Always try..finally Builder.Free for manually created builder instances.

Prototype

//Clonable via interface
IClonable = interface
  ['{GUID}']
  function Clone: TObject;
end;

TCustomer = class(TInterfacedObject, IClonable)
private
  FId: Integer;
  FName: string;
public
  function Clone: TObject;
end;

function TCustomer.Clone: TObject;
var LClone: TCustomer;
begin
  LClone := TCustomer.Create;
  LClone.FId := FId;
  LClone.FName := FName;
  Result := LClone;
end;

🔧 Structural Patterns

Adapter

//Legacy system with incompatible interface
TOldPaymentGateway = class
  procedure ProcessarPagamento(AValor: Double; ACartao: string);
end;

//New interface expected by the domain
IPaymentGateway = interface
  ['{GUID}']
  procedure ProcessPayment(AAmount: Currency; const ACardToken: string);
end;

// Adapter fazendo a ponte
TPaymentGatewayAdapter = class(TInterfacedObject, IPaymentGateway)
private
  FOldGateway: TOldPaymentGateway;
public
  constructor Create(AOldGateway: TOldPaymentGateway);
  procedure ProcessPayment(AAmount: Currency; const ACardToken: string);
end;

procedure TPaymentGatewayAdapter.ProcessPayment(AAmount: Currency; const ACardToken: string);
begin
  FOldGateway.ProcessarPagamento(AAmount, ACardToken);
end;

Decorator

//Adds behavior without inheritance
ILogger = interface
  ['{GUID}']
  procedure Log(const AMessage: string);
end;

TConsoleLogger = class(TInterfacedObject, ILogger)
  procedure Log(const AMessage: string);
end;

//Decorator that adds timestamp
TTimestampLoggerDecorator = class(TInterfacedObject, ILogger)
private
  FInner: ILogger;
public
  constructor Create(AInner: ILogger);
  procedure Log(const AMessage: string);
end;

procedure TTimestampLoggerDecorator.Log(const AMessage: string);
begin
  FInner.Log(Format('[%s] %s', [FormatDateTime('yyyy-mm-dd hh:nn:ss', Now), AMessage]));
end;

Facade

//Simplifies complex NF-e issuance subsystem
TNFeFacade = class
private
  FXmlBuilder: TNFeXmlBuilder;
  FSigner: TDigitalSigner;
  FTransmitter: TSefazTransmitter;
  FStorage: INFeRepository;
public
  constructor Create(ARepo: INFeRepository);
  destructor Destroy; override;
  ///<summary>Issues NF-e hiding all the complexity of the subsystems.</summary>
  procedure EmitirNFe(ANFe: TNFe);
end;

Proxy

//Controlled access proxy (authorization)
ICustomerRepository = interface ... end;

TSecureCustomerRepositoryProxy = class(TInterfacedObject, ICustomerRepository)
private
  FReal: ICustomerRepository;
  FCurrentUser: TUser;
  procedure CheckPermission(const AAction: string);
public
  constructor Create(AReal: ICustomerRepository; ACurrentUser: TUser);
  function FindById(AId: Integer): TCustomer;
  procedure Delete(AId: Integer);
end;

procedure TSecureCustomerRepositoryProxy.Delete(AId: Integer);
begin
  CheckPermission('DELETE_CUSTOMER');
  FReal.Delete(AId);
end;

Composite

//Leaf or container component — treats evenly
IMenuComponent = interface
  ['{GUID}']
  procedure Render;
  function GetLabel: string;
end;

TMenuItem = class(TInterfacedObject, IMenuComponent)   // Folha
  procedure Render;
  function GetLabel: string;
end;

TMenuGroup = class(TInterfacedObject, IMenuComponent)  //Container
private
  FChildren: TList<IMenuComponent>;
public
  constructor Create;
  destructor Destroy; override;
  procedure Add(AItem: IMenuComponent);
  procedure Render;
  function GetLabel: string;
end;

🎭 Behavioral Patterns

Strategy

//Vary the calculation algorithm without changing the context
ITaxStrategy = interface
  ['{GUID}']
  function Calculate(ABaseValue: Currency): Currency;
  function GetName: string;
end;

TSimplesTaxStrategy = class(TInterfacedObject, ITaxStrategy)
  function Calculate(ABaseValue: Currency): Currency;   // 6%
  function GetName: string;
end;

TLucroPresumidoStrategy = class(TInterfacedObject, ITaxStrategy)
  function Calculate(ABaseValue: Currency): Currency;   // 15%
  function GetName: string;
end;

//Context using strategy
TOrderCalculator = class
private
  FTaxStrategy: ITaxStrategy;
public
  constructor Create(ATaxStrategy: ITaxStrategy);
  function CalculateTotal(AOrder: TOrder): Currency;
end;

Observer

//Decoupled notification of domain events
IOrderObserver = interface
  ['{GUID}']
  procedure OnOrderPlaced(AOrder: TOrder);
end;

TOrderEventPublisher = class
private
  FObservers: TList<IOrderObserver>;
public
  constructor Create;
  destructor Destroy; override;
  procedure Subscribe(AObserver: IOrderObserver);
  procedure Unsubscribe(AObserver: IOrderObserver);
  procedure Notify(AOrder: TOrder);
end;

//Observers concretos
TEmailNotifier = class(TInterfacedObject, IOrderObserver)
  procedure OnOrderPlaced(AOrder: TOrder);  //send email
end;

TStockUpdater = class(TInterfacedObject, IOrderObserver)
  procedure OnOrderPlaced(AOrder: TOrder);  //updates stock
end;

Command

//Encapsulates actions as objects (undo/redo, queues)
ICommand = interface
  ['{GUID}']
  procedure Execute;
  procedure Undo;
end;

TCreateCustomerCommand = class(TInterfacedObject, ICommand)
private
  FRepository: ICustomerRepository;
  FCustomer: TCustomer;
  FCreatedId: Integer;
public
  constructor Create(ARepository: ICustomerRepository; ACustomer: TCustomer);
  procedure Execute;
  procedure Undo;
end;

//CommandBus / History
TCommandHistory = class
private
  FHistory: TStack<ICommand>;
public
  procedure Execute(ACommand: ICommand);
  procedure Undo;
end;

Template Method

//Defines the skeleton of the algorithm — subclasses fill in the gaps
TReportGenerator = class abstract
public
  //Template Method: defines the sequence
  procedure GenerateReport(const AFilePath: string);
protected
  procedure LoadData; virtual; abstract;
  procedure ProcessData; virtual; abstract;
  procedure WriteOutput(const AFilePath: string); virtual; abstract;
  procedure SendNotification; virtual;  //hook with default implementation
end;

procedure TReportGenerator.GenerateReport(const AFilePath: string);
begin
  LoadData;
  ProcessData;
  WriteOutput(AFilePath);
  SendNotification;
end;

TSalesReportGenerator = class(TReportGenerator)
protected
  procedure LoadData; override;
  procedure ProcessData; override;
  procedure WriteOutput(const AFilePath: string); override;
end;

Chain of Responsibility

//Pass the request through the chain until someone handles it
IRequestHandler = interface
  ['{GUID}']
  procedure SetNext(AHandler: IRequestHandler);
  function Handle(ARequest: TValidationRequest): Boolean;
end;

TBaseHandler = class(TInterfacedObject, IRequestHandler)
private
  FNext: IRequestHandler;
public
  procedure SetNext(AHandler: IRequestHandler);
  function Handle(ARequest: TValidationRequest): Boolean; virtual;
end;

TNameValidationHandler = class(TBaseHandler)
  function Handle(ARequest: TValidationRequest): Boolean; override;
end;

TCpfValidationHandler = class(TBaseHandler)
  function Handle(ARequest: TValidationRequest): Boolean; override;
end;

TAgeValidationHandler = class(TBaseHandler)
  function Handle(ARequest: TValidationRequest): Boolean; override;
end;

State

//Behavior varies depending on internal state
IOrderState = interface
  ['{GUID}']
  procedure Confirm(AOrder: TOrder);
  procedure Ship(AOrder: TOrder);
  procedure Cancel(AOrder: TOrder);
  function GetStatus: string;
end;

TNewOrderState = class(TInterfacedObject, IOrderState) ... end;
TConfirmedOrderState = class(TInterfacedObject, IOrderState) ... end;
TShippedOrderState = class(TInterfacedObject, IOrderState) ... end;
TCancelledOrderState = class(TInterfacedObject, IOrderState) ... end;

TOrder = class
private
  FState: IOrderState;
public
  procedure Confirm;  //delegation for FState.Confirm(Self)
  procedure Ship;
  procedure Cancel;
  procedure SetState(AState: IOrderState);
end;

✅ Checklist — Design Patterns in Delphi

  • Define interface before implementing (I prefix)
  • Inject dependencies via constructor (never hardcoded)
  • Use TInterfacedObject in all interface implementations
  • Creational patterns create objects — don't mix with business logic
  • Behavioral patterns prefer interfaces to inheritance
  • Avoid with, global variables and direct coupling in any pattern
  • Cover each pattern with DUnitX tests using Fakes

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.