MillionMunkeys.net

 

PiMunkey Documentation ->

What is Pi Programming?

Property-Invocation (Pi) Programming

Pi Programming is a style of programming. It shares in philosophy with JavaBeans to say that every property can only be modifed using "getter" and "setter" functions. It is also a close cousin to Implicit-Invocation Programming which allows any object to listen for a broadcast message, and lie dormant until someone broadcasts that message.

Get & Set

Pi Programming differs from JavaBeans in that you don't write your own getter and setter functions. Instead you use a standard set of functions on all objects.

  • exists: Tells you whether a property exists on an object.
  • get: Returns the stored value for that property.
  • set: Assigns a stored value to a property name.
  • remove: Removes a property from an object.

Property Invocation vs. Events

Property Invocation differs from Implicit Invocation in that you don't have to register any arbitrary events. Instead, every "get" and "set" event becomes a potential plugin point for infinite extendability. And to make it happen you only need to create one of two types of functions.

  • listener: This type of function is fired on a "set" operation, allowing the function to react to and/or modify the variable after it has been added to the object.
  • filter: This type of function is fired on a "get" operation, allowing the function to react to and/or modify the variable before it is returned to the requesting function.

Struct vs. Array Notation

Collections of data in ColdFusion are commonly stored in either structures or arrays. With Pi Programming, both are handled by the same object. This standardizes object interactions, and makes everything extensible.

  • struct notation: Named properties are added by their IDs, and can be accessed in the same way. They can be added one at a time or in batches.

    <cfset page = CreateObject("component","MillionMunkeys.PiMunkey.PiComponent") />
    <cfset page.set("id","page1") />
    <cfset page.set(title="First Page", template="firstpage.cfm", group="products") />
    <cfset page.remove("group") />
    <cfreturn page.get("title") />

  • array notation: Ordered arrays are not accessed by named attributes, but by their index within the object. You can use the "add", "insertAt", and "remove" functions to add and remove properties based on their location in the object, rather than their name.

    <cfset site = CreateObject("component","MillionMunkeys.PiMunkey.PiComponent") />
    <cfset page1 = CreateObject("component","MillionMunkeys.PiMunkey.PiComponent") />
    <cfset page2 = CreateObject("component","MillionMunkeys.PiMunkey.PiComponent") />
    <cfset index = site.add(page2) />
    <cfset site.insertAt(index,page1) />
    <cfset site.remove(2) />

 

 

Follow Pete on Twitter

Copyright ©2010, MillionMunkeys® LLC, All Rights Reserved