Using the Factory Method Pattern in PHP 5, a Quick Example

Place your factory classes & interface inside a directory of your choice, like /api/YOUR-PATH-TO-CLASSES-HERE/. Just make sure to specify that path in the __autoload() method when you use the factory (see the last code example below).

Set up the Interface

<?
interface IApiGrabr
{
	public function callApi();
}
?>

Implement the Interface with a Twitter Class

This code assumes there’s a directory named ‘api’ at your server’s public root folder. Also, note, in a live example you might want to have a separate file to store some of your Twitter, etc credentials. The Twitter oAuth library I’m using can be found on github.

<?
class TwitterApiGrabr implements IApiGrabr 
{
	public function callApi(){

		require_once('/api/twitteroauth.php'); //open source class from github

		$connection = new TwitterOAuth('your-consumer-key', 
		                               'your-consumer-secret', 
		                               'your-access-token-here',
		                               'your-access-token-secret-here');

		$content = $connection->get('account/rate_limit_status');
		
		// you can now call all the methods on the twitteroauth/connection object
		$user = $connection->get('account/verify_credentials');

		$favs = $connection->get('favorites',  array(
			  'screen_name'      => 'TechCrunch',
			  'count'            => 11,
		));

		//this is FPO, in real life write some formatted display code:
                var_dump($favs);
	}
}
?>

Implement the Interface with an Instagram Class

The below code uses a handy open source Instagram oAuth class from github.

<?
class InstagramApiGrabr implements IApiGrabr 
{
	public function callApi(){

		//open source class from github
		require_once('/api/Instagram.php'); 

		$instagram = new Instagram('your-client-id-here', 'your-client-secret-here', 'your-access-token-here');
		
		//simplified call, in real life, there should be an authentication check (see Instagram.php example on github)		  
	    try {
	        $instafeed = $instagram->get('users/self/media/liked'); 
	    }catch(InstagramApiError $e) {
	        die($e->getMessage());
	    }
		
	    //FPO JSON display, in real life, you'd format this w/ HTML, probably via a for loop	
        echo '<p>&nbsp;</p>'.json_encode($instafeed);
	}
}
?>

Implement the Interface with a Tumblr Class

<?
class TumblrApiGrabr implements IApiGrabr 
{
	public function callApi(){
		//posts
		$pth2 = 'http://api.tumblr.com/v2/blog/YOUR-TUMBLR-NAME-HERE.tumblr.com/posts/photo?api_key=YOUR-API-KEY-HERE';	

		///Tumblr account's posts - http://www.tumblr.com/docs/en/api/v2#posts
		$pthresult2 = file_get_contents($pth2);
		
		//FPO to just display raw JSON response, in real life, you'd format it with HTML
		echo $pthresult2;	
	}
}
?>

Set up your Factory class

<?
class MyFactory
{ 
  private static $dictionary;

  public static function init()
    {
      self::$dictionary = array('instagram' => new InstagramApiGrabr(), 
                         'twitter' => new TwitterApiGrabr(), 
                         'tumblr' => new TumblrApiGrabr());
    }

    public static function GetApi($param='instagram')
    {
        return self::$dictionary[$param]; 
    } 
}
?>

Use Your Factory Class Like So

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>API Test</title>
</head>
<body>
<?
  //allow PHP to auto include your individual class files as needed 
  function __autoload($class_name) {
      require_once('/api/YOUR-PATH-TO-CLASSES-HERE/'.$class_name . '.php');
  }
  
  MyFactory::init();

  /// control the below factory method calls via drop down menu, page navigation inside a carousel, etc. 

  //will spit out the Instagram JSON feed
  $a = MyFactory::GetApi('instagram');
  $a->callApi(); 

  //will spit out the Twitter JSON feed
  $a = MyFactory::GetApi('twitter');
  $a->callApi();

  //will spit out the Tumblr JSON feed
  $a = MyFactory::GetApi('tumblr');
  $a->callApi();
?>
</body>
</html>

Side Note on the Code Igniter framework and PHP interfaces

The Code Igniter mvc framework for PHP doesn’t seem to support “interface” or “implements” key words as of version 2.1.3.

Advertisements

Using Interfaces with Display List Objects in AS3.0

This is an example illustration of Option 2 from a great post by Mr. Wright.

Project structure:

Create an interface to be implemented by your group of display objects. In this case it’s called IEmployee, since I’ll be using it to call an identically named method on three employees (Developer, Designer, Project Manager):

package com.interfaces
{
	import com.interfaces.ISprite;
	
	public interface IEmployee extends ISprite
	{
		function talk():String;
	}
}

Create an ISprite (or IMovieClip, or IDisplayObjet) interface, since one doesn’t exist in AS3:

package com.interfaces
{
	import flash.display.Sprite;
	
	public interface ISprite
	{
		function get view():Sprite;	
	}
}

Each of the concrete classes below – Developer, Designer, ProjectManager – contains identically named methods “view” & “talk()” which can be called by a user class which doesn’t have to know exactly which class’s instance it’s calling.

Developer class implements IEmployee:

package com.view
{
	import com.interfaces.IEmployee;
	
	import flash.display.Sprite;
	
	public class Developer extends Sprite implements IEmployee
	{
		public function Developer()
		{
			super();
		}
	
		public function get view():Sprite
		{
			return this as Sprite;
		}

		public function talk():String
		{
			return "Code, code, code! Why are the PSD layers not organized? Code, code code!";
		}
	}
}

Designer class also implements IEmployee:

package com.view
{
	import com.interfaces.IEmployee;
	
	import flash.display.Sprite;
	
	public class Designer extends Sprite implements IEmployee
	{
		public function Designer()
		{
			super();
		}
		
		public function get view():Sprite
		{
			return this as Sprite;
		}
		
		public function talk():String
		{
			return "Pantone! CMYK! RGB! What the hell is HEX? Vector Smart Object!";
		}
	}
}

and ProjectManager class implements IEmployee as well:

package com.view
{
	import com.interfaces.IEmployee;
	
	import flash.display.Sprite;
	
	public class ProjectManager extends Sprite implements IEmployee
	{
		public function ProjectManager()
		{
			super();
		}
		
		public function get view():Sprite
		{
			return this as Sprite;
		}
		
		public function talk():String
		{
			return "Meeting! Do you need an LCD spec? Meeting! Can I get an ETA on that? Meeting, meeting.";
		}
	}
}

This is a basic user class, which randomly specifies which employee to use and calls the talk() method on that employee:

package
{
	import com.interfaces.IEmployee;
	import com.view.Designer;
	import com.view.Developer;
	import com.view.ProjectManager;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.getQualifiedClassName;
	
	[SWF (width="964", height="655", backgroundColor="#000000", frameRate="30")]	
	public class DisplayListPolyMorphismByInterface extends Sprite
	{
		private var worker:IEmployee;
		private var employees:Array = [new Developer(), 
                                       new Designer(), 
                                       new ProjectManager()];
		
		public function DisplayListPolyMorphismByInterface()
		{
			initApp();				
		}		
		
		public function initApp():void
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, changeWorker, false, 0, true); 			 
			addWorker();					
		}	
		
		public function addWorker():void 
		{
			worker = employees[ randomInt(0,2) ] as IEmployee;		 			
			addChild( worker.view );
			
			talkTheTalk();			
		}
		
		public function changeWorker(e:MouseEvent):void
		{				
			removeChild( worker.view );
			worker = null; 
			
			addWorker();
		}

		public function talkTheTalk():void 
		{
			trace( "This employee is a " + getQualifiedClassName(worker.view) + " and says '" + worker.talk() + "'" );
		}
		
		public function randomInt(low:Number=0, high:Number=1):int
		{
			return Math.floor(Math.random() * (1+high-low)) + low;
		}				
	}
}

Robotlegs with LoaderMax’s XMLLoader

This is a quick modification of hubflanger’s useful Robotlegs AS3.0 Site tutorial. I’m expanding her service class to use a content loading library called LoaderMax.

Since she built hers via an interface, there’s no need to refactor the existing class. Instead use her ISiteDataService interface and create a similar class called LoaderMaxDataService, like so:

package com.hubflanger.robotlegsdemo.service
{
	import com.hubflanger.robotlegsdemo.model.vo.SectionVO;
	import com.hubflanger.robotlegsdemo.model.SiteModel;
	import com.hubflanger.robotlegsdemo.events.SystemEvent;
	
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.XMLLoader;
	
	import flash.events.Event;
	import flash.net.*;
	
	import org.robotlegs.mvcs.Actor;
		
	/**
	 * A Service class that handles loading and parsing of the site's xml data.
	 */	
	public class LoaderMaxDataService extends Actor implements ISiteDataService
	{
		/**
		 * Inject the <code>SiteModel</code> Singleton.
		 */		
		[Inject]
		public var model				:SiteModel; 
		
		//private var queue				:LoaderMax; 		

		public function LoaderMaxDataService()
		{ 			
			LoaderMax.activate([XMLLoader]);					
		}
				
		public function loadData():void 
		{			
			var xml:XMLLoader = new XMLLoader("assets/data.xml", 
											  {name:"mainXML", 
											  onComplete:loadCompleteHandler, 
											  estimatedBytes:4000});
			xml.load();						
		}
		
		/**
		 * Handler for the XMLLoader's onComplete.
		 * <p>
		 * Parses the xml data and stores the <code>SectionVO</code> objects in 
		 * the <code>SiteModel</code> Singleton.
		 * <p>
		 * Dispatches a <code>SystemEvent.INIT_VIEW</code> event.
		 * 
		 * @param event
		 */		
		private function loadCompleteHandler(e:LoaderEvent):void 
		{			
			var xml:XMLLoader = e.target as XMLLoader; 
			
			model.header = xml.content.header.text(); 
			var sections:XMLList = xml.content.sections.section; 
			
			for each (var section:XML in sections) 
			{
				var sectionVO:SectionVO = new SectionVO(section.@id,
														section.label.toString(),
														section.content.toString());				
				
				model.sectionsList.push(sectionVO);
				model.sectionsHash[sectionVO.id] = sectionVO;
			}			
			
			model.defaultSection = model.sectionsList[0].id;
			
			dispatch(new SystemEvent(SystemEvent.INIT_VIEW, false));					
		}
	}
}

Update 1 line in the ApplicationContext from this:

 
injector.mapClass(ISiteDataService, SiteDataService);

to this:

 
injector.mapClass(ISiteDataService, LoaderMaxDataService);

There’s no need to update the LoadDataCommand to use LoaderMaxDataService, instead of SiteDataService. hubflanger & Robotlegs’ (ISiteDataService) use of polymorphism allows this to continue working unaltered:

package com.hubflanger.robotlegsdemo.controller
{
	import com.hubflanger.robotlegsdemo.service.ISiteDataService;
	
	import org.robotlegs.mvcs.Command;
	
	/**
	 * Responds to the <code>ContextEvent.STARTUP_COMPLETE</code> framework event.
	 */	
	public class LoadDataCommand extends Command
	{
		/**
		 * Creates an instance of SiteDataService via dependency injection.
		 */		
		[Inject]
		public var siteDataService:ISiteDataService;
		
		/**
		 * Calls the loadData() method of the SiteDataService instance.
		 */		
		override public function execute():void
		{
			siteDataService.loadData();
		}
	}
}

Note to self: “@protocol” in Objective-C is like an implementable “interface” in AS3.0 or Java

Here’s a quick example. Perhaps, before trying to decipher the grey code boxes, scroll down a bit to this section:

  • The protocol specification is quite simple. it is basically @protocol ProtocolName (methods you must implement) @end.
  • To conform to a protocol, you put the protocols you’re conforming to in <>’s, and comma separate them. Example: @interface SomeClass <Protocol1, Protocol2, Protocol3>

Some examples of the State design pattern

If you’ve ever been sick of writing convoluted if/else statements, this design pattern is a good thing to look into.

One thing it enables you to do in Actionscript — have the same button behave differently, depending on what State the application is in. For example, check out your average Flash video player, like this YouTube video from Crossfit Chesapeake / Wilkes Weightlifting:
http://crossfitchesapeake.com/2009/09/13/wod-mon-91309.aspx

In it’s embedded form, the video has a giant PLAY button with a hit area that covers the width & height of the video. In this example, this button could have three states:

  • State 1:
    If the video is dormant (hasn’t been played since the page loaded), clicking the button plays the video. The play icon graphic is displayed in the dormant State disappears once the button is clicked.
  • State 2:
    If the video is already playing, i.e. the application is in a different State, the same button behaves differently. At this time the play button click handler takes you to the YouTube.com page that originally hosts this video and pauses the embedded video.
  • State 3:
    If you go back to the embedded version of the video which has been paused by the play button’s click handler method (the new window with opened with youtube.com, etc) and click the video (not the navbar at the bottom), the smae “click handler” function from the above functionality resumes video play.

And of course, when the same “click handler” method behaves differently based on the State of the application, it’s an example of polymorphism, one of the 4 main elements of OOP. Here’s a decent definition:

Polymorphism allows two objects to be treated identically, using the same methods, even though the objects implement these methods in quite different ways. It is this concept of “same appearance, different behavior” that gets the 0 word, polymorphism.

There’s another State pattern example in the AS3 port of an open source physics library called Box2DFlashAS3.

The Main.as file, in this case the first file that gets the application going, has a listener that calls a function called update(). Among other things, this function
a) continuously checks for an id variable (altered by the user pressing a keyboard key) and
b) sets a variable of type Test to one of the subclasses of Test, like TestRagdoll (the default example when you run the SWF).

The state machine code is essentially this part of Main.as:

/* ...	*/
switch(m_currId){
	// Ragdoll
	case 0:
		m_currTest = new TestRagdoll();
		break;
	// Compound Shapes
	case 1:
		m_currTest = new TestCompound();
		break;
	// Crank/Gears/Pulley
	case 2:
		m_currTest = new TestCrankGearsPulley();
		break;
	// Bridge
	case 3:
		m_currTest = new TestBridge();
		break;
	// Stack
	case 4:
		m_currTest = new TestStack();
		break;
	// CCD
	case 5:
		m_currTest = new TestCCD();
		break;
	// Theo Jansen
	case 6:
		m_currTest = new TestTheoJansen();
		break;
		
	/* ...	*/
}
/* ...	*/