1
0
mirror of synced 2024-12-14 07:06:04 +03:00

fixed some typos

This commit is contained in:
Chris Woodford 2011-05-05 21:16:40 -04:00
parent c242ab4371
commit 894b2614ed

View File

@ -5,20 +5,15 @@ Persisting the Decorator Pattern
INTRO INTRO
Let's take a quick look at a visual representation of the Decorator
pattern
DECORATOR PATTERN IMAGE
Component Component
--------- ---------
Since the Component class needs to be persisted, it's going to be a Since the ``Component`` class needs to be persisted, it's going to
Doctrine Entity. As the top of the inheritance hierarchy, it's going be an ``Entity``. As the top of the inheritance hierarchy, it's going
to have to define the persistent inheritance. For this example, we to have to define the persistent inheritance. For this example, we
will use Single Table Inheritance, but Class Table Inheritance will use Single Table Inheritance, but Class Table Inheritance
would work as well. In the discriminator map, we will define two would work as well. In the discriminator map, we will define two
concrete subclasses, ConcreteComponent and ConcreteDecorator. concrete subclasses, ``ConcreteComponent`` and ``ConcreteDecorator``.
.. code-block:: php .. code-block:: php
@ -30,8 +25,8 @@ concrete subclasses, ConcreteComponent and ConcreteDecorator.
* @Entity * @Entity
* @InheritanceType("SINGLE_TABLE") * @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="discr", type="string") * @DiscriminatorColumn(name="discr", type="string")
* @DiscriminatorMap({"cc" = "Test\Component\Concrete\Component", * @DiscriminatorMap({"cc" = "Test\Component\ConcreteComponent",
"cd" = "Test\Decorator\Concrete\Decorator"}) "cd" = "Test\Decorator\ConcreteDecorator"})
*/ */
abstract class Component abstract class Component
{ {
@ -77,9 +72,9 @@ concrete subclasses, ConcreteComponent and ConcreteDecorator.
ConcreteComponent ConcreteComponent
----------------- -----------------
The ConcreteComponent class is pretty simple and doesn't do much more The ``ConcreteComponent`` class is pretty simple and doesn't do much
than extend the abstract Component class (only for the purpose of more than extend the abstract ``Component`` class (only for the
keeping this example simple). purpose of keeping this example simple).
.. code-block:: php .. code-block:: php
@ -96,9 +91,9 @@ keeping this example simple).
Decorator Decorator
--------- ---------
The Decorator class doesn't need to be persisted, but it does need to The ``Decorator`` class doesn't need to be persisted, but it does
define an association with a persisted Entity. We can use a need to define an association with a persisted ``Entity``. We can
MappedSuperclass for this. use a ``MappedSuperclass`` for this.
.. code-block:: php .. code-block:: php
@ -111,7 +106,7 @@ MappedSuperclass for this.
{ {
/** /**
* @OneToOne(targetEntity="TestComponent", cascade={"all"}) * @OneToOne(targetEntity="Test\Component", cascade={"all"})
* @JoinColumn(name="decorates", referencedColumnName="id") * @JoinColumn(name="decorates", referencedColumnName="id")
*/ */
protected $decorates; protected $decorates;
@ -127,7 +122,7 @@ MappedSuperclass for this.
/** /**
* (non-PHPdoc) * (non-PHPdoc)
* @see ImedevacTest.Component::getName() * @see Test.Component::getName()
*/ */
public function getName() public function getName()
{ {
@ -154,31 +149,33 @@ MappedSuperclass for this.
} }
All operations on the Decorator (i.e. persist, remove, etc) will All operations on the ``Decorator`` (i.e. persist, remove, etc) will
cascade from the Decorator to the Component. This means that when we cascade from the ``Decorator`` to the ``Component``. This means that
persist a Decorator, Doctrine will take care of persisting the chain when we persist a ``Decorator``, Doctrine will take care of
of decorated objects for us. A Decorator can be treated exactly as a persisting the chain of decorated objects for us. A ``Decorator`` can
Component when it comes time to persisting it. be treated exactly as a ``Component`` when it comes time to
persisting it.
The Decorator's constructor accepts an instance of a Component, as The ``Decorator's`` constructor accepts an instance of a
defined by the Decorator pattern (using constructor injection). The ``Component``, as defined by the ``Decorator`` pattern (using
setDecorates/getDecorates methods have been defined as protected to constructor injection). The setDecorates/getDecorates methods have
hide the fact that a Decorator is decorating a Component and keeps been defined as protected to hide the fact that a ``Decorator`` is
the Component interface and the Decorator interface identical. decorating a ``Component`` and keeps the ``Component`` interface and
the ``Decorator`` interface identical.
To illustrate the purpose of the Decorator pattern, the getName() To illustrate the purpose of the ``Decorator`` pattern, the getName()
method has been overridden to append a string to the Component's method has been overridden to append a string to the ``Component's``
getName() method. getName() method.
ConcreteDecorator ConcreteDecorator
----------------- -----------------
The final class required to complete a simple implementation of the The final class required to complete a simple implementation of the
Decorator pattern is the ConcreteDecorator. In order to further Decorator pattern is the ``ConcreteDecorator``. In order to further
illustrate how the Decorator can alter data as it moves through the illustrate how the ``Decorator`` can alter data as it moves through
chain of decoration, a new field, "special", has been added to this the chain of decoration, a new field, "special", has been added to
class. The getName() has been overridden and appends the value of the this class. The getName() has been overridden and appends the value
getSpecial() method to its return value. of the getSpecial() method to its return value.
.. code-block:: php .. code-block:: php
@ -215,7 +212,7 @@ getSpecial() method to its return value.
/** /**
* (non-PHPdoc) * (non-PHPdoc)
* @see ImedevacTest.Component::getName() * @see Test.Component::getName()
*/ */
public function getName() public function getName()
{ {
@ -235,8 +232,8 @@ objects
<?php <?php
use Test\Component\Concrete\Component, use Test\Component\ConcreteComponent,
Test\Decorator\Concrete\Decorator; Test\Decorator\ConcreteDecorator;
// assumes Doctrine 2 is configured and an instance of // assumes Doctrine 2 is configured and an instance of
// an EntityManager is available as $em // an EntityManager is available as $em
@ -261,13 +258,13 @@ objects
$d = $em->find('Test\Component', 3); $d = $em->find('Test\Component', 3);
echo get_class($c); echo get_class($c);
// prints: Test\Component\Concrete\Component // prints: Test\Component\ConcreteComponent
echo $c->getName(); echo $c->getName();
// prints: Test Component 1 // prints: Test Component 1
echo get_class($d) echo get_class($d)
// prints: Test\Component\Concrete\Decorator // prints: Test\Component\ConcreteDecorator
echo $d->getName(); echo $d->getName();
// prints: [Really] Decorated Test Component 2 // prints: [Really] Decorated Test Component 2