#native_company# #native_desc#
#native_cta#

OO Design: Abstract Classes Page 7

By Jonathan Griffin
on February 10, 2003

<?php

class X10_Control extends Control

{

  function 
on()

  {

    
// ...implementation code.

  
}

  function 
off()

  {

    
// ...implementation code.

  
}

  function 
getStatus()

  {

    
// ...implementation code.

  
}

  function 
dim()

  {

    
// ...implementation code.

  
}

  function 
bright()

  {

    
// ...implementation code.

  
}

}

?>



Notice the addition of methods dim() and bright(). This functionality is available in the X-10
protocol, but may not be common in other protocols. Therefore, we add it here exclusively. Now that our X-10
protocol class is complete, we want to see how this protocol and others will apply to a home device. We’ll start
out by creating a Device class. Let’s take a look.

<?php

class Device

{

  
// Member variables.

  
var $_type;

  var 
$_protocol;

  

  function 
Device($deviceID)

  {

    
// Grab device settings from storage.

    
$deviceRS DataProvider::getDevice($deviceID);

    

    
// Assign settings to members.

    
$this->_type     $deviceRS->type;      // LIGHT, APPLIANCE, AUDIO, etc.

    
$this->_protocol $deviceRS->protocol;  // "X10"

    

    // Compose pluggable control protocol through dynamic aggregation.

    
aggregate$this$this->_protocol."_Control" );

  }

  

  function 
setProtocol($protocol)

  {

    
// Clear any existing aggregation.

    
deaggregate$this$this->_protocol."_Control" );

    

    
// Update protocol.

    
$this->_protocol $protocol;

    

    
// Assign new aggregation.

    
aggregate$this$this->_protocol."_Control" );

  }

}

?>



We have declared two member variables to hold the type of device and protocol used (i.e. X10, CEBus).
In the constructor, we grab the device settings from storage, assign these settings to our instance, and
then apply the appropriate control functionality, based on the protocol specified.
On a side note, PHP supports two types of composition:  association and aggregation. The protocol
functionality is composed through aggregation. Now we can swap in and out different protocols by simply
assigning a new protocol type; even at runtime. Today your garage light is X-10 compatible; tomorrow Smart House.

1
|
2
|
3
|
4
|
5
|
6
|
7
|
8