详述PHP适配器模式(附代码示例)

wufei123 发布于 2023-04-30 阅读(1570)

本篇文章给大家带来了关于php的相关知识,其中主要跟大家聊一聊PHP适配器模式,还有代码示例,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。

详述PHP适配器模式(附代码示例)

PHP 适配器模式讲解和代码示例

适配器是一种结构型设计模式, 它能使不兼容的对象能够相互合作。

适配器可担任两个对象间的封装器, 它会接收对于一个对象的调用, 并将其转换为另一个对象可识别的格式和接口。

复杂度:******

流行度:******

使用示例: 适配器模式在 PHP 代码中很常见。 基于一些遗留代码的系统常常会使用该模式。 在这种情况下, 适配器让遗留代码与现代的类得以相互合作。

识别方法: 适配器可以通过以不同抽象或接口类型实例为参数的构造函数来识别。 当适配器的任何方法被调用时, 它会将参数转换为合适的格式, 然后将调用定向到其封装对象中的一个或多个方法。

  • 真实世界示例

适配器允许你使用第三方或遗留系统的类, 即使它们与你的代码不兼容。 例如, 你可以创建一系列特殊的封装器, 来让应用所发出的调用与第三方类所要求的接口与格式适配, 而无需重写应用的通知接口以使其支持每一个第三方服务 (如钉钉、 微信、 短信或其他任何服务)。

index.php: 真实世界示例

1

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

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

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

 

namespace RefactoringGuru\Adapter\RealWorld;

 

/**

 * The Target interface represents the interface that your application's classes

 * already follow.

 */

interface Notification

{

    public function send(string $title, string $message);

}

 

/**

 * Here's an example of the existing class that follows the Target interface.

 *

 * The truth is that many real apps may not have this interface clearly defined.

 * If you're in that boat, your best bet would be to extend the Adapter from one

 * of your application's existing classes. If that's awkward (for instance,

 * SlackNotification doesn't feel like a subclass of EmailNotification), then

 * extracting an interface should be your first step.

 */

class EmailNotification implements Notification

{

    private $adminEmail;

 

    public function __construct(string $adminEmail)

    {

        $this->adminEmail = $adminEmail;

    }

 

    public function send(string $title, string $message): void

    {

        mail($this->adminEmail, $title, $message);

        echo "Sent email with title '$title' to '{$this->adminEmail}' that says '$message'.";

    }

}

 

/**

 * The Adaptee is some useful class, incompatible with the Target interface. You

 * can't just go in and change the code of the class to follow the Target

 * interface, since the code might be provided by a 3rd-party library.

 */

class SlackApi

{

    private $login;

    private $apiKey;

 

    public function __construct(string $login, string $apiKey)

    {

        $this->login = $login;

        $this->apiKey = $apiKey;

    }

 

    public function logIn(): void

    {

        // Send authentication request to Slack web service.

        echo "Logged in to a slack account '{$this->login}'.\n";

    }

 

    public function sendMessage(string $chatId, string $message): void

    {

        // Send message post request to Slack web service.

        echo "Posted following message into the '$chatId' chat: '$message'.\n";

    }

}

 

/**

 * The Adapter is a class that links the Target interface and the Adaptee class.

 * In this case, it allows the application to send notifications using Slack

 * API.

 */

class SlackNotification implements Notification

{

    private $slack;

    private $chatId;

 

    public function __construct(SlackApi $slack, string $chatId)

    {

        $this->slack = $slack;

        $this->chatId = $chatId;

    }

 

    /**

     * An Adapter is not only capable of adapting interfaces, but it can also

     * convert incoming data to the format required by the Adaptee.

     */

    public function send(string $title, string $message): void

    {

        $slackMessage = "#" . $title . "# " . strip_tags($message);

        $this->slack->logIn();

        $this->slack->sendMessage($this->chatId, $slackMessage);

    }

}

 

/**

 * The client code can work with any class that follows the Target interface.

 */

function clientCode(Notification $notification)

{

    // ...

 

    echo $notification->send("Website is down!",

        "Alert! " .

        "Our website is not responding. Call admins and bring it up!");

 

    // ...

}

 

echo "Client code is designed correctly and works with email notifications:\n";

$notification = new EmailNotification("developers@example.com");

clientCode($notification);

echo "\n\n";

 

 

echo "The same client code can work with other classes via adapter:\n";

$slackApi = new SlackApi("example.com", "XXXXXXXX");

$notification = new SlackNotification($slackApi, "Example.com Developers");

clientCode($notification);

Output.txt: 执行结果

1

2

3

4

5

Client code is designed correctly and works with email notifications:

Sent email with title 'Website is down!' to 'developers@example.com' that says 'color:red;font-size: 50px;'>Alert! Our website is not responding. Call admins and bring it up!'.

The same client code can work with other classes via adapter:

Logged in to a slack account 'example.com'.

Posted following message into the 'Example.com Developers' chat: '#Website is down!# Alert! Our website is not responding. Call admins and bring it up!'.

  • 概念示例

本例说明了适配器设计模式的结构并重点回答了下面的问题:

  • 它由哪些类组成?

  • 这些类扮演了哪些角色?

  • 模式中的各个元素会以何种方式相互关联?

了解该模式的结构后, 你可以更容易地理解下面基于真实世界的 PHP 应用案例。

index.php: 概念示例

1

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

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

 

namespace RefactoringGuru\Adapter\Conceptual;

 

/**

 * The Target defines the domain-specific interface used by the client code.

 */

class Target

{

    public function request(): string

    {

        return "Target: The default target's behavior.";

    }

}

 

/**

 * The Adaptee contains some useful behavior, but its interface is incompatible

 * with the existing client code. The Adaptee needs some adaptation before the

 * client code can use it.

 */

class Adaptee

{

    public function specificRequest(): string

    {

        return ".eetpadA eht fo roivaheb laicepS";

    }

}

 

/**

 * The Adapter makes the Adaptee's interface compatible with the Target's

 * interface.

 */

class Adapter extends Target

{

    private $adaptee;

 

    public function __construct(Adaptee $adaptee)

    {

        $this->adaptee = $adaptee;

    }

 

    public function request(): string

    {

        return "Adapter: (TRANSLATED) " . strrev($this->adaptee->specificRequest());

    }

}

 

/**

 * The client code supports all classes that follow the Target interface.

 */

function clientCode(Target $target)

{

    echo $target->request();

}

 

echo "Client: I can work just fine with the Target objects:\n";

$target = new Target();

clientCode($target);

echo "\n\n";

 

$adaptee = new Adaptee();

echo "Client: The Adaptee class has a weird interface. See, I don't understand it:\n";

echo "Adaptee: " . $adaptee->specificRequest();

echo "\n\n";

 

echo "Client: But I can work with it via the Adapter:\n";

$adapter = new Adapter($adaptee);

clientCode($adapter);

Output.txt: 执行结果

1

2

3

4

5

6

7

8

Client: I can work just fine with the Target objects:

Target: The default target's behavior.

 

Client: The Adaptee class has a weird interface. See, I don't understand it:

Adaptee: .eetpadA eht fo roivaheb laicepS

 

Client: But I can work with it via the Adapter:

Adapter: (TRANSLATED) Special behavior of the Adaptee.


以上就是详述PHP适配器模式(附代码示例)的详细内容


标签:  PHP 免费教程 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。