Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Design Patterns in PHP and Laravel
PREMIUM
Số trang
246
Kích thước
7.0 MB
Định dạng
PDF
Lượt xem
1846

Design Patterns in PHP and Laravel

Nội dung xem thử

Mô tả chi tiết

Design Patterns

in PHP and

Laravel —

Kelt Dockins

Design Patterns in PHP

and Laravel

Kelt Dockins

[email protected]

Design Patterns in PHP and Laravel

Kelt Dockins

Dolph, Arkansas

USA

ISBN-13 (pbk): 978-1-4842-2450-2 ISBN-13 (electronic): 978-1-4842-2451-9

DOI 10.1007/978-1-4842-2451-9

Library of Congress Control Number: 2016961807

Copyright © 2017 by Kelt Dockins

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the

material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,

broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage

and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or

hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with

every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an

editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.

The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are

not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to

proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication,

neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or

omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material

contained herein.

Managing Director: Welmoed Spahr

Acquisitions Editor: Louise Corrigan

Technical Reviewer: Martin Bean

Editorial Board: Steve Anglin, Pramila Balan, Laura Berendson, Aaron Black, Louise Corrigan,

Jonathan Gennick, Todd Green, Robert Hutchinson, Celestin Suresh John, Nikhil Karkal,

James Markham, Susan McDermott, Matthew Moodie, Natalie Pao, Gwenan Spearing

Coordinating Editor: Nancy Chen

Copy Editor: Mary Behr

Compositor: SPi Global

Indexer: SPi Global

Artist: SPi Global

Distributed to the book trade worldwide by Springer Science+Business Media New York,

233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail

[email protected], or visit www.springer.com. Apress Media, LLC is a California LLC

and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc).

SSBM Finance Inc is a Delaware corporation.

For information on translations, please e-mail [email protected], or visit www.apress.com.

Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.

eBook versions and licenses are also available for most titles. For more information, reference our Special

Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.

Any source code or other supplementary materials referenced by the author in this text are available to

readers at www.apress.com. For detailed information about how to locate your book’s source code, go to

www.apress.com/source-code/. Readers can also access source code at SpringerLink in the

Supplementary Material section for each chapter.

Printed on acid-free paper

[email protected]

iii

Contents at a Glance

About the Author �����������������������������������������������������������������������������������������������������xv

About the Technical Reviewer �������������������������������������������������������������������������������xvii

Introduction������������������������������������������������������������������������������������������������������������xix

■Chapter 1: Laravel Basics�������������������������������������������������������������������������������������� 1

■Chapter 2: Let’s Grow a SOLID Garden������������������������������������������������������������������� 9

■Chapter 3: Abstract Factory �������������������������������������������������������������������������������� 19

■Chapter 4: Builder������������������������������������������������������������������������������������������������ 25

■Chapter 5: The Factory Method ��������������������������������������������������������������������������� 33

■Chapter 6: Prototype ������������������������������������������������������������������������������������������� 37

■Chapter 7: Singleton�������������������������������������������������������������������������������������������� 45

■Chapter 8: Simple Factory Method���������������������������������������������������������������������� 51

■Chapter 9: Adapter ���������������������������������������������������������������������������������������������� 55

■Chapter 10: Bridge����������������������������������������������������������������������������������������������� 63

■Chapter 11: Composite ���������������������������������������������������������������������������������������� 71

■Chapter 12: Decorator ����������������������������������������������������������������������������������������� 77

■Chapter 13: Facade���������������������������������������������������������������������������������������������� 85

■Chapter 14: Flyweight ����������������������������������������������������������������������������������������� 93

■Chapter 15: Proxy���������������������������������������������������������������������������������������������� 103

■Chapter 16: Chain of Responsibility ������������������������������������������������������������������ 115

■Chapter 17: Command��������������������������������������������������������������������������������������� 123

■Chapter 18: Interpreter�������������������������������������������������������������������������������������� 135

[email protected]

■ Contents at a Glance

iv

■Chapter 19: Iterator������������������������������������������������������������������������������������������� 147

■Chapter 20: Mediator����������������������������������������������������������������������������������������� 157

■Chapter 21: Memento ���������������������������������������������������������������������������������������� 171

■Chapter 22: Observer����������������������������������������������������������������������������������������� 181

■Chapter 23: State����������������������������������������������������������������������������������������������� 199

■Chapter 24: Strategy ����������������������������������������������������������������������������������������� 215

■Chapter 25: Template Method���������������������������������������������������������������������������� 221

■Chapter 26: Visitor��������������������������������������������������������������������������������������������� 227

■Chapter 27: More Resources ����������������������������������������������������������������������������� 233

Index��������������������������������������������������������������������������������������������������������������������� 235

[email protected]

v

Contents

About the Author �����������������������������������������������������������������������������������������������������xv

About the Technical Reviewer �������������������������������������������������������������������������������xvii

Introduction������������������������������������������������������������������������������������������������������������xix

■Chapter 1: Laravel Basics�������������������������������������������������������������������������������������� 1

What Is Composer? ���������������������������������������������������������������������������������������������������������� 2

Meta Information�������������������������������������������������������������������������������������������������������������� 3

Dependency Management������������������������������������������������������������������������������������������������ 3

Autoloading ���������������������������������������������������������������������������������������������������������������������� 4

Lifecycle Hooks/Scripts���������������������������������������������������������������������������������������������������� 4

Stability ���������������������������������������������������������������������������������������������������������������������������� 6

Running Composer����������������������������������������������������������������������������������������������������������� 6

Setting Up Your Environment Variable������������������������������������������������������������������������������ 7

■Chapter 2: Let’s Grow a SOLID Garden������������������������������������������������������������������� 9

Single Responsibility Principle ����������������������������������������������������������������������������������������� 9

Open/Closed Principle ���������������������������������������������������������������������������������������������������� 10

Liskov Substitution Principle������������������������������������������������������������������������������������������ 12

Interface Segregation Principle�������������������������������������������������������������������������������������� 14

Dependency Inversion Principle ������������������������������������������������������������������������������������� 15

Conclusion���������������������������������������������������������������������������������������������������������������������� 17

Single Responsibility Principle ������������������������������������������������������������������������������������������������������������� 17

Open/Closed Principle �������������������������������������������������������������������������������������������������������������������������� 17

Listov Substitution Principle����������������������������������������������������������������������������������������������������������������� 17

[email protected]

■ Contents

vi

Interface Segregation Principle ������������������������������������������������������������������������������������������������������������ 17

Dependency Inversion Principle ����������������������������������������������������������������������������������������������������������� 17

Encapsulate What Varies ���������������������������������������������������������������������������������������������������������������������� 17

■Chapter 3: Abstract Factory �������������������������������������������������������������������������������� 19

Intent������������������������������������������������������������������������������������������������������������������������������ 19

Applications�������������������������������������������������������������������������������������������������������������������� 19

Abstract Structure ���������������������������������������������������������������������������������������������������������� 20

Example�������������������������������������������������������������������������������������������������������������������������� 21

Example Structure���������������������������������������������������������������������������������������������������������� 21

Implementation �������������������������������������������������������������������������������������������������������������� 22

Conclusion���������������������������������������������������������������������������������������������������������������������� 24

■Chapter 4: Builder������������������������������������������������������������������������������������������������ 25

Intent������������������������������������������������������������������������������������������������������������������������������ 25

Applications�������������������������������������������������������������������������������������������������������������������� 25

Abstract Structure ���������������������������������������������������������������������������������������������������������� 26

Example�������������������������������������������������������������������������������������������������������������������������� 26

Example Structure���������������������������������������������������������������������������������������������������������� 27

Implementation �������������������������������������������������������������������������������������������������������������� 27

Conclusion���������������������������������������������������������������������������������������������������������������������� 32

■Chapter 5: The Factory Method ��������������������������������������������������������������������������� 33

Intent������������������������������������������������������������������������������������������������������������������������������ 33

Applications�������������������������������������������������������������������������������������������������������������������� 33

Abstract Structure ���������������������������������������������������������������������������������������������������������� 34

Example�������������������������������������������������������������������������������������������������������������������������� 34

Example Structure���������������������������������������������������������������������������������������������������������� 34

Implementation �������������������������������������������������������������������������������������������������������������� 35

Conclusion���������������������������������������������������������������������������������������������������������������������� 36

[email protected]

■ Contents

vii

■Chapter 6: Prototype ������������������������������������������������������������������������������������������� 37

Intent������������������������������������������������������������������������������������������������������������������������������ 37

Applications�������������������������������������������������������������������������������������������������������������������� 37

Abstract Structure ���������������������������������������������������������������������������������������������������������� 37

Example�������������������������������������������������������������������������������������������������������������������������� 38

Example Structure���������������������������������������������������������������������������������������������������������� 38

Implementation �������������������������������������������������������������������������������������������������������������� 39

Conclusion���������������������������������������������������������������������������������������������������������������������� 43

■Chapter 7: Singleton�������������������������������������������������������������������������������������������� 45

Intent������������������������������������������������������������������������������������������������������������������������������ 45

Applications�������������������������������������������������������������������������������������������������������������������� 45

Abstract Structure ���������������������������������������������������������������������������������������������������������� 45

Example�������������������������������������������������������������������������������������������������������������������������� 46

Implementation �������������������������������������������������������������������������������������������������������������� 46

Conclusion���������������������������������������������������������������������������������������������������������������������� 49

■Chapter 8: Simple Factory Method���������������������������������������������������������������������� 51

Intent������������������������������������������������������������������������������������������������������������������������������ 51

Applications�������������������������������������������������������������������������������������������������������������������� 51

Abstract Structure ���������������������������������������������������������������������������������������������������������� 51

Example Scenario����������������������������������������������������������������������������������������������������������� 52

Implementation �������������������������������������������������������������������������������������������������������������� 52

Conclusion���������������������������������������������������������������������������������������������������������������������� 53

■Chapter 9: Adapter ���������������������������������������������������������������������������������������������� 55

Intent������������������������������������������������������������������������������������������������������������������������������ 55

Applications�������������������������������������������������������������������������������������������������������������������� 55

Abstract Structure ���������������������������������������������������������������������������������������������������������� 56

Example Scenario����������������������������������������������������������������������������������������������������������� 57

[email protected]

■ Contents

viii

Example Structure���������������������������������������������������������������������������������������������������������� 57

Implementation �������������������������������������������������������������������������������������������������������������� 58

Conclusion���������������������������������������������������������������������������������������������������������������������� 61

■Chapter 10: Bridge����������������������������������������������������������������������������������������������� 63

Intent������������������������������������������������������������������������������������������������������������������������������ 63

Applications�������������������������������������������������������������������������������������������������������������������� 63

Abstract Structure ���������������������������������������������������������������������������������������������������������� 63

Example�������������������������������������������������������������������������������������������������������������������������� 64

Example Structure���������������������������������������������������������������������������������������������������������� 64

Implementation �������������������������������������������������������������������������������������������������������������� 65

Second Example (Sending Messages)�������������������������������������������������������������������������������������������������� 66

Conclusion���������������������������������������������������������������������������������������������������������������������� 69

■Chapter 11: Composite ���������������������������������������������������������������������������������������� 71

Intent������������������������������������������������������������������������������������������������������������������������������ 71

Applications�������������������������������������������������������������������������������������������������������������������� 71

Abstract Structure ���������������������������������������������������������������������������������������������������������� 71

Example�������������������������������������������������������������������������������������������������������������������������� 72

Example Structure���������������������������������������������������������������������������������������������������������� 72

Implementation �������������������������������������������������������������������������������������������������������������� 73

Conclusion���������������������������������������������������������������������������������������������������������������������� 76

■Chapter 12: Decorator ����������������������������������������������������������������������������������������� 77

Intent������������������������������������������������������������������������������������������������������������������������������ 77

Applications�������������������������������������������������������������������������������������������������������������������� 77

Abstract Structure ���������������������������������������������������������������������������������������������������������� 77

Example�������������������������������������������������������������������������������������������������������������������������� 78

Example Structure���������������������������������������������������������������������������������������������������������� 78

Implementation �������������������������������������������������������������������������������������������������������������� 79

Presenters �������������������������������������������������������������������������������������������������������������������������������������������� 82

Conclusion���������������������������������������������������������������������������������������������������������������������� 83

[email protected]

■ Contents

ix

■Chapter 13: Facade���������������������������������������������������������������������������������������������� 85

Intent������������������������������������������������������������������������������������������������������������������������������ 85

Applications�������������������������������������������������������������������������������������������������������������������� 85

Laravel Facades ������������������������������������������������������������������������������������������������������������� 85

Abstract Structure ���������������������������������������������������������������������������������������������������������� 86

Example�������������������������������������������������������������������������������������������������������������������������� 87

Example Structure���������������������������������������������������������������������������������������������������������� 88

Implementation �������������������������������������������������������������������������������������������������������������� 89

Are Facades the Same as Adapters?���������������������������������������������������������������������������������������������������� 91

Conclusion���������������������������������������������������������������������������������������������������������������������� 91

■Chapter 14: Flyweight ����������������������������������������������������������������������������������������� 93

Intent������������������������������������������������������������������������������������������������������������������������������ 93

Applications�������������������������������������������������������������������������������������������������������������������� 93

Abstract Structure ���������������������������������������������������������������������������������������������������������� 94

Example�������������������������������������������������������������������������������������������������������������������������� 95

Example Structure���������������������������������������������������������������������������������������������������������� 95

Implementation �������������������������������������������������������������������������������������������������������������� 96

The Flyweight Implementation ����������������������������������������������������������������������������������������������������������� 100

Conclusion�������������������������������������������������������������������������������������������������������������������� 101

■Chapter 15: Proxy���������������������������������������������������������������������������������������������� 103

Intent���������������������������������������������������������������������������������������������������������������������������� 103

Applications������������������������������������������������������������������������������������������������������������������ 103

Abstract Structure �������������������������������������������������������������������������������������������������������� 104

Example������������������������������������������������������������������������������������������������������������������������ 104

Implementation ������������������������������������������������������������������������������������������������������������ 107

Virtual Proxy (Experiment 1 and 2) ����������������������������������������������������������������������������������������������������� 107

Remote Proxy (Experiment 3) ������������������������������������������������������������������������������������������������������������� 109

[email protected]

■ Contents

x

Protection Proxy (Experiment 4) ��������������������������������������������������������������������������������������������������������� 111

Smart Proxy ���������������������������������������������������������������������������������������������������������������������������������������� 113

Conclusion�������������������������������������������������������������������������������������������������������������������� 114

■Chapter 16: Chain of Responsibility ������������������������������������������������������������������ 115

Intent���������������������������������������������������������������������������������������������������������������������������� 115

Applications������������������������������������������������������������������������������������������������������������������ 115

Abstract Structure �������������������������������������������������������������������������������������������������������� 115

Example������������������������������������������������������������������������������������������������������������������������ 116

Example Structure�������������������������������������������������������������������������������������������������������� 117

Implementation ������������������������������������������������������������������������������������������������������������ 117

Conclusion�������������������������������������������������������������������������������������������������������������������� 121

■Chapter 17: Command��������������������������������������������������������������������������������������� 123

Intent���������������������������������������������������������������������������������������������������������������������������� 123

Applications������������������������������������������������������������������������������������������������������������������ 123

Abstract Structure �������������������������������������������������������������������������������������������������������� 124

Example������������������������������������������������������������������������������������������������������������������������ 125

Example Structure�������������������������������������������������������������������������������������������������������� 125

Implementation ������������������������������������������������������������������������������������������������������������ 125

Television Command Pattern Example ����������������������������������������������������������������������������������������������� 130

Conclusion�������������������������������������������������������������������������������������������������������������������� 133

■Chapter 18: Interpreter�������������������������������������������������������������������������������������� 135

Intent���������������������������������������������������������������������������������������������������������������������������� 135

Applications������������������������������������������������������������������������������������������������������������������ 135

Context-Free Grammar����������������������������������������������������������������������������������������������������������������������� 136

Abstract Structure �������������������������������������������������������������������������������������������������������� 136

Example������������������������������������������������������������������������������������������������������������������������ 137

Example Structure�������������������������������������������������������������������������������������������������������� 138

[email protected]

■ Contents

xi

Implementation ������������������������������������������������������������������������������������������������������������ 139

Dude, Where’s My Parser?������������������������������������������������������������������������������������������������������������������ 143

Conclusion�������������������������������������������������������������������������������������������������������������������� 146

■Chapter 19: Iterator������������������������������������������������������������������������������������������� 147

Intent���������������������������������������������������������������������������������������������������������������������������� 147

Applications������������������������������������������������������������������������������������������������������������������ 147

Abstract Structure �������������������������������������������������������������������������������������������������������� 148

Example������������������������������������������������������������������������������������������������������������������������ 149

Example Structure�������������������������������������������������������������������������������������������������������� 150

Implementation ������������������������������������������������������������������������������������������������������������ 150

Laravel Collections ����������������������������������������������������������������������������������������������������������������������������� 153

Conclusion�������������������������������������������������������������������������������������������������������������������� 155

■Chapter 20: Mediator����������������������������������������������������������������������������������������� 157

Intent���������������������������������������������������������������������������������������������������������������������������� 157

Applications������������������������������������������������������������������������������������������������������������������ 157

Abstract Structure �������������������������������������������������������������������������������������������������������� 157

Example������������������������������������������������������������������������������������������������������������������������ 158

Example Structure�������������������������������������������������������������������������������������������������������� 159

Implementation ������������������������������������������������������������������������������������������������������������ 159

Don’t Mess with My Constructor, Man!����������������������������������������������������������������������������������������������� 166

Mediator Is Not For Me ����������������������������������������������������������������������������������������������������������������������� 166

Not a Mediator Pattern ����������������������������������������������������������������������������������������������������������������������� 167

Conclusion�������������������������������������������������������������������������������������������������������������������� 169

■Chapter 21: Memento ���������������������������������������������������������������������������������������� 171

Intent���������������������������������������������������������������������������������������������������������������������������� 171

Applications������������������������������������������������������������������������������������������������������������������ 171

Abstract Structure �������������������������������������������������������������������������������������������������������� 172

Example������������������������������������������������������������������������������������������������������������������������ 173

[email protected]

■ Contents

xii

Example Structure�������������������������������������������������������������������������������������������������������� 173

Implementation ������������������������������������������������������������������������������������������������������������ 174

Alternatives to Memento �������������������������������������������������������������������������������������������������������������������� 177

Conclusion�������������������������������������������������������������������������������������������������������������������� 180

■Chapter 22: Observer����������������������������������������������������������������������������������������� 181

Intent���������������������������������������������������������������������������������������������������������������������������� 181

Applications������������������������������������������������������������������������������������������������������������������ 181

Abstract Structure �������������������������������������������������������������������������������������������������������� 181

Example������������������������������������������������������������������������������������������������������������������������ 182

Example Structure�������������������������������������������������������������������������������������������������������� 182

Implementation ������������������������������������������������������������������������������������������������������������ 183

Generic Observers with SPL ��������������������������������������������������������������������������������������������������������������� 183

Taylor Cuts the Cheese ����������������������������������������������������������������������������������������������������������������������� 185

Eloquent Observers: Out-of-the-Box Observers ��������������������������������������������������������������������������������� 188

Conclusion�������������������������������������������������������������������������������������������������������������������� 196

Drawbacks������������������������������������������������������������������������������������������������������������������������������������������ 197

■Chapter 23: State����������������������������������������������������������������������������������������������� 199

Intent���������������������������������������������������������������������������������������������������������������������������� 199

Applications������������������������������������������������������������������������������������������������������������������ 199

Abstract Structure �������������������������������������������������������������������������������������������������������� 199

Example������������������������������������������������������������������������������������������������������������������������ 200

Example Structure�������������������������������������������������������������������������������������������������������� 201

Implementation: Example 1 - State Pattern ����������������������������������������������������������������� 202

Example 2 - Enter State Machines������������������������������������������������������������������������������� 206

Example 3��������������������������������������������������������������������������������������������������������������������� 212

Conclusion�������������������������������������������������������������������������������������������������������������������� 214

[email protected]

■ Contents

xiii

■Chapter 24: Strategy ����������������������������������������������������������������������������������������� 215

Intent���������������������������������������������������������������������������������������������������������������������������� 215

Applications������������������������������������������������������������������������������������������������������������������ 215

Abstract Structure �������������������������������������������������������������������������������������������������������� 215

Example������������������������������������������������������������������������������������������������������������������������ 216

Example Structure�������������������������������������������������������������������������������������������������������� 217

Implementation ������������������������������������������������������������������������������������������������������������ 218

Conclusion�������������������������������������������������������������������������������������������������������������������� 220

■Chapter 25: Template Method���������������������������������������������������������������������������� 221

Intent���������������������������������������������������������������������������������������������������������������������������� 221

Applications������������������������������������������������������������������������������������������������������������������ 221

Abstract Structure �������������������������������������������������������������������������������������������������������� 221

Example������������������������������������������������������������������������������������������������������������������������ 222

Example Structure�������������������������������������������������������������������������������������������������������� 222

Implementation ������������������������������������������������������������������������������������������������������������ 223

Conclusion�������������������������������������������������������������������������������������������������������������������� 225

■Chapter 26: Visitor��������������������������������������������������������������������������������������������� 227

Intent���������������������������������������������������������������������������������������������������������������������������� 227

Applications������������������������������������������������������������������������������������������������������������������ 227

Abstract Structure �������������������������������������������������������������������������������������������������������� 228

Example������������������������������������������������������������������������������������������������������������������������ 228

Example Structure�������������������������������������������������������������������������������������������������������� 229

Implementation ������������������������������������������������������������������������������������������������������������ 229

Conclusion�������������������������������������������������������������������������������������������������������������������� 232

■Chapter 27: More Resources ����������������������������������������������������������������������������� 233

Index��������������������������������������������������������������������������������������������������������������������� 235

[email protected]

xv

About the Author

Kelt Dockins is a humble narcissistic web developer. He works as a full stack freelancer. He specializes in

quick-to-market web applications and minimum viable products for start-ups. He has worked on small

projects, medium-size projects, and even a few small-medium-sized projects. He loves being a father, a

husband, and a developer. He’s worked with many programming languages such as Visual Basic 6.0, C++,

Java, Perl, bash, prolog, .NET stack, HTML, CSS, JavaScript and PHP. For the last three years, he’s been using

PHP and the ever-so-popular Laravel framework to stay at home and eat bacon while earning it at the same

time. He does love bacon.

[email protected]

Tải ngay đi em, còn do dự, trời tối mất!