| 1 | =head1 NAME | ||||||||||||
| 2 | |||||||||||||
| 3 | Class::Composite - Implements Composite patterns | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||||
| 6 | |||||||||||||
| 7 | ========================= | ||||||||||||
| 8 | Collection implementation | ||||||||||||
| 9 | ========================= | ||||||||||||
| 10 | |||||||||||||
| 11 | use Class::Composite; | ||||||||||||
| 12 | my $collection = Class::Composite::Container->new(); | ||||||||||||
| 13 | my $element = Class::Composite::Element->new(); | ||||||||||||
| 14 | $collection->addElement( $elem ); | ||||||||||||
| 15 | $elements = $collection->getElements(); | ||||||||||||
| 16 | |||||||||||||
| 17 | |||||||||||||
| 18 | ======================== | ||||||||||||
| 19 | Composite implementation | ||||||||||||
| 20 | ======================== | ||||||||||||
| 21 | |||||||||||||
| 22 | package graphicBase; # Base for graphics containers and elements | ||||||||||||
| 23 | sub display { | ||||||||||||
| 24 | my $self = shift; | ||||||||||||
| 25 | foreach my $elem (@{$self->getElements()}) { | ||||||||||||
| 26 | $elem->display(); | ||||||||||||
| 27 | } | ||||||||||||
| 28 | paint($elem); | ||||||||||||
| 29 | } | ||||||||||||
| 30 | |||||||||||||
| 31 | |||||||||||||
| 32 | package graphicElement; | ||||||||||||
| 33 | use base qw( Class::Composite::Element graphicBase ); | ||||||||||||
| 34 | |||||||||||||
| 35 | |||||||||||||
| 36 | package graphicContainer; | ||||||||||||
| 37 | use base qw( Class::Composite::Container graphicBase ); | ||||||||||||
| 38 | |||||||||||||
| 39 | |||||||||||||
| 40 | package main; | ||||||||||||
| 41 | use graphicElement; | ||||||||||||
| 42 | use graphicContainer; | ||||||||||||
| 43 | my $element = graphicElement->new(); | ||||||||||||
| 44 | my $container = graphicContainer->new(); | ||||||||||||
| 45 | $container->addElement( $element ); | ||||||||||||
| 46 | $container->display(); | ||||||||||||
| 47 | |||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||
| 49 | |||||||||||||
| 50 | C<Class::Composite> is used to provide mechanisms used by C<Class::Composite::Container> | ||||||||||||
| 51 | and C<Class::Composite::Element>. Class::Composite::* implements a Composite pattern (see OO Patterns books and http://www.uni-paderborn.de/cs/ag-schaefer/Lehre/Lehrveranstaltungen/Vorlesungen/Entwurfsmuster/WS0102/DPSA-IVb.pdf for example). | ||||||||||||
| 52 | A composite pattern is a collection implementation which provides same methods to the container and elements. | ||||||||||||
| 53 | The reason for using a Composite pattern is to have the same interface to deal with different objects and their containers (collections). | ||||||||||||
| 54 | |||||||||||||
| 55 | If you only need a collection implementation, then you can inherite from Class::Composite::Container and Class::Composite::Element directly. | ||||||||||||
| 56 | If you need specific method that should be applied to both your container and your elements (which is what the Class::Composite is made for), | ||||||||||||
| 57 | then you isolate the methods you want to apply on both elements and containers in a specific package. | ||||||||||||
| 58 | Then, you inherite from both your package and Class::Composite::Element for elements, and Class::Composite::Container for containers. | ||||||||||||
| 59 | |||||||||||||
| 60 | =head1 INHERITANCE | ||||||||||||
| 61 | |||||||||||||
| 62 | Class::Base | ||||||||||||
| 63 | |||||||||||||
| 64 | =cut | ||||||||||||
| 65 | package Class::Composite; | ||||||||||||
| 66 | |||||||||||||
| 67 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 68 | 2 | ▶ | use warnings::register; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 69 | 2 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | |||||||||||||
| 71 | 2 | ▶ | use base qw( Class::Base ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 72 | |||||||||||||
| 73 | 2 | our $VERSION = 0.2; | |||||||||||
| 74 | |||||||||||||
| 75 | |||||||||||||
| 76 | =head2 getAll() | ||||||||||||
| 77 | |||||||||||||
| 78 | Returns an array ref of all elements below, whatever their depth or type. | ||||||||||||
| 79 | |||||||||||||
| 80 | =cut | ||||||||||||
| 81 | 9 | ▶ | sub getAll : method { | ||||||||||
|
Subroutine
getAll: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 82 | 9 | my $self = shift; | |||||||||||
| 83 | 9 | my @elems = (); | |||||||||||
| 84 | 9 | foreach my $junior ( @{$self->getElements()} ) { | |||||||||||
| 85 | 8 | push @elems, $junior; | |||||||||||
| 86 | 8 | ▶ | push @elems, @{$junior->getAll} if defined($junior); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 87 | } | ||||||||||||
| 88 | 9 | \@elems; | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | |||||||||||||
| 92 | =head2 getLeaves(start, end) | ||||||||||||
| 93 | |||||||||||||
| 94 | Returns all Class::Composite::Element contained in the collection, whatever their depth. | ||||||||||||
| 95 | |||||||||||||
| 96 | =cut | ||||||||||||
| 97 | 3 | ▶ | sub getLeaves : method { | ||||||||||
|
Subroutine
getLeaves: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||
| 98 | 3 | my ($self, $start, $last) = @_; | |||||||||||
| 99 | 3 | my @elements = (); | |||||||||||
| 100 | 3 | foreach my $elem ( @{$self->getElements($start, $last)} ) { | |||||||||||
| 101 | 10 | ▶ | defined $elem or next; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 10 | ▶ | if ($elem->isa('Class::Composite::Element')) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 9 | push @elements, $elem; | |||||||||||
| 104 | } | ||||||||||||
| 105 | else { | ||||||||||||
| 106 | 1 | ▶ | my $subElems = $elem->getLeaves() || []; | ||||||||||
|
Condition50%1/2
$elem->getLeaves || []
MC/DC0%0/1
$elem->getLeaves || []
$elem->getLeaves
Truth table50%1/2
A
A= $elem->getLeaves
| |||||||||||||
| 107 | 1 | ▶ | push @elements, @$subElems if (@$subElems); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 108 | } | ||||||||||||
| 109 | } | ||||||||||||
| 110 | 3 | \@elements; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | |||||||||||||
| 114 | =head2 getElements() | ||||||||||||
| 115 | |||||||||||||
| 116 | Returns the elements just below the current object. | ||||||||||||
| 117 | Returns [] | ||||||||||||
| 118 | must probably be overriden by child classes. | ||||||||||||
| 119 | |||||||||||||
| 120 | =cut | ||||||||||||
| 121 | 7 | ▶ | sub getElements () : method { [] } | ||||||||||
|
Subroutine
getElements: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 122 | |||||||||||||
| 123 | |||||||||||||
| 124 | =head2 getElement() | ||||||||||||
| 125 | |||||||||||||
| 126 | Returns undef | ||||||||||||
| 127 | must probably be overriden by child classes | ||||||||||||
| 128 | |||||||||||||
| 129 | =cut | ||||||||||||
| 130 | sub getElement () : method { undef } | ||||||||||||
| 131 | |||||||||||||
| 132 | |||||||||||||
| 133 | =head2 nOfElements() | ||||||||||||
| 134 | |||||||||||||
| 135 | Returns undef, to be overriden by child class | ||||||||||||
| 136 | |||||||||||||
| 137 | =cut | ||||||||||||
| 138 | 1 | ▶ | sub nOfElements { } | ||||||||||
|
Subroutine
nOfElements: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 139 | |||||||||||||
| 140 | |||||||||||||
| 141 | =head2 elementType() | ||||||||||||
| 142 | |||||||||||||
| 143 | Returns the class the element must belongs to, default is | ||||||||||||
| 144 | Class::Composite. | ||||||||||||
| 145 | Sets it to undef if you don't want any checking to occur. | ||||||||||||
| 146 | To be overriden in Child class. | ||||||||||||
| 147 | |||||||||||||
| 148 | =cut | ||||||||||||
| 149 | sub elementType () : method { __PACKAGE__ } | ||||||||||||
| 150 | |||||||||||||
| 151 | |||||||||||||
| 152 | =head2 applyToAll( $sub ) | ||||||||||||
| 153 | |||||||||||||
| 154 | Applies the subroutine $sub to all elements. | ||||||||||||
| 155 | The subroutine will receive a collection element as a parameter. | ||||||||||||
| 156 | |||||||||||||
| 157 | =cut | ||||||||||||
| 158 | 0 | ▶ | sub applyToAll : method { | ||||||||||
|
Subroutine
applyToAll: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 159 | 0 | my ($self, $sub) = @_; | |||||||||||
| 160 | 0 | $sub->( $_ ) foreach ( @{$self->getElements} ); | |||||||||||
| 161 | 0 | $self; | |||||||||||
| 162 | } | ||||||||||||
| 163 | |||||||||||||
| 164 | |||||||||||||
| 165 | ## | ||||||||||||
| 166 | ## Helper method | ||||||||||||
| 167 | ## | ||||||||||||
| 168 | 1 | ▶ | sub _warn { | ||||||||||
|
Subroutine
_warn: called CC=1 SCAR=0.0
| |||||||||||||
| 169 | 1 | warn $_[1].' - '.caller(1)." " . caller(2) . "\n"; | |||||||||||
| 170 | 1 | undef; | |||||||||||
| 171 | } | ||||||||||||
| 172 | |||||||||||||
| 173 | |||||||||||||
| 174 | 2 | 1; | |||||||||||
| 175 | |||||||||||||
| 176 | |||||||||||||
| 177 | __END__ | ||||||||||||