Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4ae202a

Browse files
committed
Decorator
1 parent 7b4dcdf commit 4ae202a

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

‎Decorator/index.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* Design pattern "Decorator" (Structural)
5+
* This is demo code
6+
* See for details: http://maxsite.org/page/php-patterns
7+
*/
8+
9+
/**
10+
* Base class for Component
11+
*/
12+
abstract class ComponentAbstract
13+
{
14+
abstract public function operation();
15+
}
16+
17+
/**
18+
* Component
19+
*/
20+
class Component extends ComponentAbstract
21+
{
22+
public function operation()
23+
{
24+
echo 'Component operation <br>';
25+
}
26+
}
27+
28+
/**
29+
* Base for Decorator
30+
*/
31+
abstract class DecoratorAbstract extends ComponentAbstract
32+
{
33+
protected $component;
34+
35+
/**
36+
* accept Component
37+
*/
38+
public function __construct($component)
39+
{
40+
$this->component = $component;
41+
}
42+
}
43+
44+
/**
45+
* Decorator for Component
46+
*/
47+
class Decorator extends DecoratorAbstract
48+
{
49+
/**
50+
* New operation with Component
51+
*/
52+
public function operation()
53+
{
54+
echo 'Decorator 1 <br>';
55+
$this->component->operation();
56+
echo 'Decorator 2 <br>';
57+
}
58+
}
59+
60+
/**
61+
* demo
62+
*/
63+
64+
/**
65+
* create Decorator for Component
66+
*/
67+
$decoratedComponent = new Decorator(
68+
new Component()
69+
);
70+
71+
/**
72+
* run operation
73+
*/
74+
$decoratedComponent->operation();
75+
/*
76+
Decorator 1
77+
Component operation
78+
Decorator 2
79+
*/
80+
81+
82+
# end of file

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ See details on http://maxsite.org/page/php-patterns
1616
* "Observer" (Behavioral)
1717
* "Prototype" (Creational)
1818
* "Bridge" (Structural)
19+
* "Decorator" (Structural)
1920

2021

2122
(c) MaxSite.org, 2019, http://maxsite.org/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /