Skip to main content

Object-Oriented Patterns

This section covers practical patterns for building maintainable applications.

The this Reference

this is a direct reference to the current object.

  • Access: $this.property
  • Method Call: $this.method()

Factory Pattern

Use a factory class to decide which class to instantiate at runtime (e.g., selecting a print template based on data).

XML
<class var="ShapeFactory">
<method name="create">
<if value1="$type" value2="circle">
<new class="Circle" var="return" />
<else>
<new class="Square" var="return" />
</else>
</if>
</method>
</class>

Singleton Pattern

Combine <global> with a conditional check to ensure only one instance exists.

XML
<global var="Logger" />
<is var="Logger" type="non-array">
<new class="LoggerClass" var="Logger" />
</is>

Composition

Prefer composition over deep inheritance. Objects can contain other objects as properties.

XML
<class var="Invoice">
<constructor>
<new class="Customer" var="this.customer" />
<new class="LineItems" var="this.items" />
</constructor>
</class>