You are browsing a version that is no longer maintained.

Persisting the Decorator Pattern

This recipe will show you a simple example of how you can use Doctrine ORM to persist an implementation of the Decorator Pattern

Component

The Component class needs to be persisted, so it's going to be an Entity. As the top of the inheritance hierarchy, it's going to have to define the persistent inheritance. For this example, we will use Single Table Inheritance, but Class Table Inheritance would work as well. In the discriminator map, we will define two concrete subclasses, ConcreteComponent and ConcreteDecorator.

1<?php namespace Test; /** * @Entity * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="discr", type="string") * @DiscriminatorMap({"cc" = "Test\Component\ConcreteComponent", "cd" = "Test\Decorator\ConcreteDecorator"}) */ abstract class Component { /** * @Id @Column(type="integer") * @GeneratedValue(strategy="AUTO") */ protected $id; /** @Column(type="string", nullable=true) */ protected $name; /** * Get id * @return integer $id */ public function getId() { return $this->id; } /** * Set name * @param string $name */ public function setName($name) { $this->name = $name; } /** * Get name * @return string $name */ public function getName() { return $this->name; } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

ConcreteComponent

The ConcreteComponent class is pretty simple and doesn't do much more than extend the abstract Component class (only for the purpose of keeping this example simple).

1<?php namespace Test\Component; use Test\Component; /** @Entity */ class ConcreteComponent extends Component {}
2
3
4
5
6
7
8
9

Decorator

The Decorator class doesn't need to be persisted, but it does need to define an association with a persisted Entity. We can use a MappedSuperclass for this.

1<?php namespace Test; /** @MappedSuperclass */ abstract class Decorator extends Component { /** * @OneToOne(targetEntity="Test\Component", cascade={"all"}) * @JoinColumn(name="decorates", referencedColumnName="id") */ protected $decorates; /** * initialize the decorator * @param Component $c */ public function __construct(Component $c) { $this->setDecorates($c); } /** * (non-PHPdoc) * @see Test.Component::getName() */ public function getName() { return 'Decorated ' . $this->getDecorates()->getName(); } /** * the component being decorated * @return Component */ protected function getDecorates() { return $this->decorates; } /** * sets the component being decorated * @param Component $c */ protected function setDecorates(Component $c) { $this->decorates = $c; } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

All operations on the Decorator (i.e. persist, remove, etc) will cascade from the Decorator to the Component. This means that when we persist a Decorator, Doctrine will take care of persisting the chain of decorated objects for us. A Decorator can be treated exactly as a Component when it comes time to persisting it.

The Decorator's constructor accepts an instance of a Component, as defined by the Decorator pattern. The setDecorates/getDecorates methods have been defined as protected to hide the fact that a Decorator is decorating a Component and keeps the Component interface and the Decorator interface identical.

To illustrate the intended result of the Decorator pattern, the getName() method has been overridden to append a string to the Component's getName() method.

ConcreteDecorator

The final class required to complete a simple implementation of the Decorator pattern is the ConcreteDecorator. In order to further illustrate how the Decorator can alter data as it moves through the chain of decoration, a new field, "special", has been added to this class. The getName() has been overridden and appends the value of the getSpecial() method to its return value.

1<?php namespace Test\Decorator; use Test\Decorator; /** @Entity */ class ConcreteDecorator extends Decorator { /** @Column(type="string", nullable=true) */ protected $special; /** * Set special * @param string $special */ public function setSpecial($special) { $this->special = $special; } /** * Get special * @return string $special */ public function getSpecial() { return $this->special; } /** * (non-PHPdoc) * @see Test.Component::getName() */ public function getName() { return '[' . $this->getSpecial() . '] ' . parent::getName(); } }
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

Examples

Here is an example of how to persist and retrieve your decorated objects

1<?php use Test\Component\ConcreteComponent, Test\Decorator\ConcreteDecorator; // assumes Doctrine ORM is configured and an instance of // an EntityManager is available as $em // create a new concrete component $c = new ConcreteComponent(); $c->setName('Test Component 1'); $em->persist($c); // assigned unique ID = 1 // create a new concrete decorator $c = new ConcreteComponent(); $c->setName('Test Component 2'); $d = new ConcreteDecorator($c); $d->setSpecial('Really'); $em->persist($d); // assigns c as unique ID = 2, and d as unique ID = 3 $em->flush(); $c = $em->find('Test\Component', 1); $d = $em->find('Test\Component', 3); echo get_class($c); // prints: Test\Component\ConcreteComponent echo $c->getName(); // prints: Test Component 1 echo get_class($d) // prints: Test\Component\ConcreteDecorator echo $d->getName(); // prints: [Really] Decorated Test Component 2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38