PHP Design Pattern Decorator Pattern code example _php Tutorial

Source: Internet
Author: User

Example of decorator pattern code in PHP design mode


This article mainly introduces the PHP design pattern of the decorator pattern code example, decorator mode is not to modify the original class code and inheritance in the case of dynamic extension of the function of the class, this article gives a code example, the need for friends can refer to the next

Defined:

Decorator mode is the ability to dynamically extend a class without modifying the original class code and inheritance. The traditional programming pattern is the subclass inherits the parent class implementation method overload, uses the adorner pattern, simply adds a new adorner object, is more flexible, avoids the class quantity and the hierarchy too much.

Role:

Component (base class for decorative objects)

Concretecomponent (specifically decorated objects)

Decorator (Decorator base class)

Contretedecorator (Specific decorator Class)

Example code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

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

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

The person who is the decorator base class

Interface Component

{

Public function operation ();

}

Decorator base class

Abstract class Decorator implements Component

{

protected $component;

Public function __construct (Component $component)

{

$this->component = $component;

}

Public Function operation ()

{

$this->component->operation ();

}

}

Concrete Decorator Class

Class Concretecomponent implements Component

{

Public Function operation ()

{

Echo ' do operation '. Php_eol;

}

}

Concrete Decoration Class A

Class Concretedecoratora extends Decorator {

Public function __construct (Component $component) {

Parent::__construct ($component);

}

Public function operation () {

Parent::operation ();

$this->addedoperationa (); Newly added operations

}

Public Function Addedoperationa () {

Echo ' Add operation A '. Php_eol;

}

}

Concrete Decoration Class B

Class Concretedecoratorb extends Decorator {

Public function __construct (Component $component) {

Parent::__construct ($component);

}

Public function operation () {

Parent::operation ();

$this->addedoperationb ();

}

Public Function addedoperationb () {

Echo ' Add operation B '. Php_eol;

}

}

Class Client {

public static function main () {

/*

Do operation

ADD Operation A

*/

$decoratorA = new Concretedecoratora (new Concretecomponent ());

$decoratorA->operation ();

/*

Do operation

ADD Operation A

ADD Operation B

*/

$decoratorB = new Concretedecoratorb ($decoratorA);

$decoratorB->operation ();

}

}

Client::main ();

http://www.bkjia.com/PHPjc/998354.html www.bkjia.com true http://www.bkjia.com/PHPjc/998354.html techarticle PHP Design Pattern Decorator Pattern code example this article mainly introduces the PHP design pattern Decorator Pattern code example, decorator mode is not to modify the original class code and inherit the case ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.