The client sends a message to the head component. The component declares the interface that various parts of the graph should respect. A leaf is a concrete class that has no children. A composite is a concrete class that composes other components.
PHP Code :
/**
* Component interface.
* The Client depends only on this abstraction, whatever graph is built using
* the specializations.
*/
interface HtmlElement
{
/**
* @return string representation
*/
public function __toString();
}
/**
* Leaf sample implementation.
* Represents an <h1> element.
*/
class H1 implements HtmlElement
{
private $_text;
public function __construct($text)
{
$this->_text = $text;
}
public function __toString()
{
return "<h1>{$this->_text}</h1>";
}
}
/**
* Leaf sample implementation.
* Represents a <p> element.
*/
class P implements HtmlElement
{
private $_text;
public function __construct($text)
{
$this->_text = $text;
}
public function __toString()
{
return "<p>{$this->_text}</p>";
}
}
/**
* A Composite implementation, which accepts as children generic Components.
* These children may be H1, P or even other Divs.
*/
class Div implements HtmlElement
{
private $_children = array();
public function addChild(HtmlElement $element)
{
$this->_children[] = $element;
}
public function __toString()
{
$html = "<div>\n";
foreach ($this->_children as $child) {
$childRepresentation = (string) $child;
$childRepresentation = str_replace("\n", "\n ", $childRepresentation);
$html .= ' ' . $childRepresentation . "\n";
}
$html .= "</div>";
return $html;
}
}
// Client code
$div = new Div();
$div->addChild(new H1('Title'));
$div->addChild(new P('Lorem ipsum...'));
$sub = new Div();
$sub->addChild(new P('Dolor sit amet...'));
$div->addChild($sub);
echo $div, "\n";
Output
<div>
<h1>Title</h1>
<p>Lorem ipsum...</p>
<div>
<p>Dolor sit amet...</p>
</div>
</div>
Java Code :
import java.util.List;
import java.util.ArrayList;
/** "Component" */
interface Graphic {
//Prints the graphic.
public void print();
}
/** "Composite" */
class CompositeGraphic implements Graphic {
//Collection of child graphics.
private List mChildGraphics = new ArrayList();
//Prints the graphic.
public void print() {
for (Graphic graphic : mChildGraphics) {
graphic.print();
}
}
//Adds the graphic to the composition.
public void add(Graphic graphic) {
mChildGraphics.add(graphic);
}
//Removes the graphic from the composition.
public void remove(Graphic graphic) {
mChildGraphics.remove(graphic);
}
}
/** "Leaf" */
class Ellipse implements Graphic {
String myname = "Ellipse";
public Ellipse(int i){
this.myname = myname + ":"+i;
}
//Prints the graphic.
public void print() {
System.out.println(this.myname);
}
}
/** Client */
public class Program {
public static void main(String[] args) {
//Initialize four ellipses
Ellipse ellipse1 = new Ellipse();
Ellipse ellipse2 = new Ellipse();
Ellipse ellipse3 = new Ellipse();
Ellipse ellipse4 = new Ellipse();
//Initialize three composite graphics
CompositeGraphic graphic = new CompositeGraphic();
CompositeGraphic graphic1 = new CompositeGraphic();
CompositeGraphic graphic2 = new CompositeGraphic();
//Composes the graphics
graphic1.add(ellipse1);
graphic1.add(ellipse2);
graphic1.add(ellipse3);
graphic2.add(ellipse4);
graphic.add(graphic1);
graphic.add(graphic2);
//Prints the complete graphic (four times the string "Ellipse").
graphic.print();
}
}
OUTPUT:
Ellipse:1
Ellipse:2
Ellipse:3
Ellipse:4
No comments:
Post a Comment