class Post
{
private $title;
private $content;
//additional properties
public function filter()
{
//do necessary processing
$this->content = $filtered_content;
$this->title = $filtered_title;
}
public function getContent()
{
return $this->content;
}
//additional methods
}
?>
class Comment
{
private $date;
private $content;
//additional properties
public function filter()
{
//do necessary processing
$this->content = $filtered_content;
}
public function getContent()
{
return $this->content;
}
//additional methods
}
?>
Design Patterns
[ 90 ]
Now we create two Decorator objects, which can parse the BBCode and
Emoticon respectively:
class BBCodeParser
{
private $post;
public function __construct($object)
{
$this->post = $object;
}
public function getContent()
{
//parse bbcode
$post->filter();
$content = $this->parseBBCode($post->getContent());
return $content;
}
private function parseBBCode($content)
{
//process BB code in the content and return it
}
}
?>
And here comes the emoticon parser:
class EmoticonParser
{
private $post;
public function __construct($object)
{
$this->post = $object;
}
public function getContent()
{
//parse bbcode
$post->filter();
$content = $this->parseEmoticon($post->getContent());
return $content;
}
private function parseEmoticon($content)
{
Chapter 4
[ 91 ]
//process Emoticon code in the content and return it
}
}
?>
These Decorator objects just add the BBCode and EmoticonCode parsing capability
to the existing objects without touching them.
Pages:
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109