logo资料库

Go Design Patterns 无水印pdf.pdf

第1页 / 共394页
第2页 / 共394页
第3页 / 共394页
第4页 / 共394页
第5页 / 共394页
第6页 / 共394页
第7页 / 共394页
第8页 / 共394页
资料共394页,剩余部分请下载后查看
Cover
Copyright
About the Author
Table of Contents
Preface
1: Ready... Steady... Go!
A little bit of history
Installing Go
Linux
Go Linux advanced installation
Windows
Mac OS X
Setting the workspace – Linux and Apple OS X
Starting with Hello World
Integrated Development Environment – IDE
Types
Variables and constants
Operators
Flow control
The if… else statement
The switch statement
The for…range statement
Functions
What does a function look like?
What is an anonymous function?
Closures
Creating errors, handling errors and returning errors.
Function with undetermined number of parameters
Naming returned types
Arrays, slices, and maps
Arrays
Zero-initialization
Slices
Maps
Visibility
Zero-initialization
Pointers and structures
What is a pointer? Why are they good?
Structs
Interfaces
Interfaces – signing a contract
Testing and TDD
The testing package
What is TDD?
Libraries
The Go get tool
Managing JSON data
The encoding package
Go tools
The golint tool
The gofmt tool
The godoc tool
The goimport tool
Contributing to Go open source projects in GitHub
Summary
2: Creational Patterns -Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns
Singleton design pattern – having a unique instance of a type in the entire program
Description
Objectives
Example – a unique counter
Requirements and acceptance criteria
Writing unit tests first
Implementation
A few words about the Singleton design pattern
Builder design pattern – reusing an algorithm to create many implementations of an interface
Description
Objectives
Example – vehicle manufacturing
Requirements and acceptance criteria
Unit test for the vehicle builder
Implementation
Wrapping up the Builder design pattern
Factory method – delegating the creation of different types of payments
Description
Objectives
The example – a factory of payment methods for a shop
Acceptance criteria
First unit test
Implementation
Upgrading the Debitcard method to a new platform
What we learned about the Factory method
Abstract Factory – a factory of factories
Description
The objectives
The vehicle factory example, again?
Acceptance criteria
Unit test
Implementation
A few lines about the Abstract Factory method
Prototype design pattern
Description
Objective
Example
Acceptance criteria
Unit test
Implementation
What we learned about the Prototype design pattern
Summary
3: Structural Patterns - Composite, Adapter, and Bridge Design Patterns
Composite design pattern
Description
Objectives
The swimmer and the fish
Requirements and acceptance criteria
Creating compositions
Binary Tree compositions
Composite pattern versus inheritance
Final words on the Composite pattern
Adapter design pattern
Description
Objectives
Using an incompatible interface with an Adapter object
Requirements and acceptance criteria
Unit testing our Printer adapter
Implementation
Examples of the Adapter pattern in Go's source code
What the Go source code tells us about the Adapter pattern
Bridge design pattern
Description
Objectives
Two printers and two ways of printing for each
Requirements and acceptance criteria
Unit testing the Bridge pattern
Implementation
Reuse everything with the Bridge pattern
Summary
4: Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns
Proxy design pattern
Description
Objectives
Example
Acceptance criteria
Unit test
Implementation
Proxying around actions
Decorator design pattern
Description
Objectives
Example
Acceptance criteria
Unit test
Implementation
A real-life example – server middleware
Starting with the common interface, http.Handler
A few words about Go's structural typing
Summarizing the Decorator design pattern – Proxy versus Decorator
Facade design pattern
Description
Objectives
Example
Acceptance criteria
Unit test
Implementation
Library created with the Facade pattern
Flyweight design pattern
Description
Objectives
Example
Acceptance criteria
Basic structs and tests
Implementation
What's the difference between Singleton and Flyweight then?
Summary
5: Behavioral Patterns - Strategy, Chain of Responsibility, and Command Design Patterns
Strategy design pattern
Description
Objectives
Rendering images or text
Acceptance criteria
Implementation
Solving small issues in our library
Final words on the Strategy pattern
Chain of responsibility design pattern
Description
Objectives
A multi-logger chain
Unit test
Implementation
What about a closure?
Putting it together
Command design pattern
Description
Objectives
A simple queue
Acceptance criteria
Implementation
More examples
Chain of responsibility of commands
Rounding-up the Command pattern up
Summary
6: Behavioral Patterns - Template, Memento, and Interpreter Design Patterns
Template design pattern
Description
Objectives
Example – a simple algorithm with a deferred step
Requirements and acceptance criteria
Unit tests for the simple algorithm
Implementing the Template pattern
Anonymous functions
How to avoid modifications on the interface
Looking for the Template pattern in Go's source code
Summarizing the Template design pattern
Memento design pattern
Description
Objectives
A simple example with strings
Requirements and acceptance criteria
Unit test
Implementing the Memento pattern
Another example using the Command and Facade patterns
Last words on the Memento pattern
Interpreter design pattern
Description
Objectives
Example – a polish notation calculator
Acceptance criteria for the calculator
Unit test of some operations
Implementation
Complexity with the Interpreter design pattern
Interpreter pattern again – now using interfaces
The power of the Interpreter pattern
Summary
7: Behavioral Patterns - Visitor, State, Mediator, and Observer Design Patterns
Visitor design pattern
Description
Objectives
A log appender
Acceptance criteria
Unit tests
Implementation of Visitor pattern
Another example
Visitors to the rescue!
State design pattern
Description
Objectives
A small guess the number game
Acceptance criteria
Implementation of State pattern
A state to win and a state to lose
The game built using the State pattern
Mediator design pattern
Description
Objectives
A calculator
Acceptance criteria
Implementation
Uncoupling two types with the Mediator
Observer design pattern
Description
Objectives
The notifier
Acceptance criteria
Unit tests
Implementation
Summary
8: Introduction to Gos Concurrency
A little bit of history and theory
Concurrency versus parallelism
CSP versus actor-based concurrency
Goroutines
Our first Goroutine
Anonymous functions launched as new Goroutines
WaitGroups
Callbacks
Callback hell
Mutexes
An example with mutexes – concurrent counter
Presenting the race detector
Channels
Our first channel
Buffered channels
Directional channels
The select statement
Ranging over channels too!
Using it all – concurrent singleton
Unit test
Implementation
Summary
9: Concurrency Patterns - Barrier, Future, and Pipeline Design Patterns
Barrier concurrency pattern
Description
Objectives
An HTTP GET aggregator
Acceptance criteria
Unit test – integration
Implementation
Waiting for responses with the Barrier design pattern
Future design pattern
Description
Objectives
A simple asynchronous requester
Acceptance criteria
Unit tests
Implementation
Putting the Future together
Pipeline design pattern
Description
Objectives
A concurrent multi-operation
Acceptance criteria
Beginning with tests
Implementation
The list generator
Raising numbers to the power of 2
Final reduce operation
Launching the Pipeline pattern
Final words on the Pipeline pattern
Summary
10: Concurrency Patterns - Workers Pool and Publish/Subscriber Design Patterns
Workers pool
Description
Objectives
A pool of pipelines
Acceptance criteria
Implementation
The dispatcher
The pipeline
An app using the workers pool
No tests?
Wrapping up the Worker pool
Concurrent Publish/Subscriber design pattern
Description
Objectives
Example – a concurrent notifier
Acceptance criteria
Unit test
Testing subscriber
Testing publisher
Implementation
Implementing the publisher
Handling channels without race conditions
A few words on the concurrent Observer pattern
Summary
Index
Go Design Patterns Learn idiomatic, efficient, clean, and extensible Go design and concurrency patterns by using TDD Mario Castro Contreras BIRMINGHAM - MUMBAI
Go Design Patterns Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: February 2017 Production reference: 1170217 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-620-4 www.packtpub.com
Credits Author Mario Castro Contreras Copy Editor Safis Editing Reviewer Shiju Varghese Project Coordinator Izzat Contractor Commissioning Editor Kunal Parikh Proofreader Safis Editing Acquisition Editor Chaitanya Nair Indexer Mariammal Chettiyar Content Development Editor Zeeyan Pinheiro Graphics Abhinash Sahu Technical Editor Pavan Ramchandani Production Coordinator Deepika Naik  
About the Author Mario Castro Contreras is a software engineer who has specialized in distributed systems and big data solutions. He works as a site reliability engineer, and now he is focused on containerized solutions and apps using most of Google Cloud suite; especially, Kubernetes. He has a wide experience in systems and solutions integration, and he has written many scalable and reliable 12 factor apps using Go and Docker. He has designed Big Data architectures for financial services and media, and he has written data processing pipelines using event-driven architectures written purely in Go. He is also very active in the open source community, and you can find him on his GitHub account with the username sayden. In the past, he has also written mobile applications and backends in Java. Mario is passionate about programming languages, and he found the best balance between fun and productivity in Go; however, recently, he enjoys writing in Rust and embedded systems in C. He is also passionate about road cycling and winter sports. I'd like to express my deep gratitude to my parents for supporting me in my journey through computers since I was 8. To Urszula, Tyrion and Tesla for their daily support and for being with me in the long nights writing this book. I'd like to thank Chaitanya, for her guidance at the beginning of the book, Zeeyan, for his patience and help on every chapter, and Pavan, for the help and explanations. But also to all the reviewers, especially to Shiju, and the entire team at Packt that made this book possible.
About the Reviewer Shiju Varghese is a solutions architect focused on building highly scalable cloud native applications with a special interest in APIs, microservices, containerized architectures, and distributed systems. He currently specializes in Go, Google Cloud, and container technologies. He is an early adopter of the Go programming language and provides consultation and training for building backend systems and microservices with Go ecosystem. He has been a mentor to various start-ups and enterprises for the technology transformation to Go. He has been a speaker at numerous technology conferences, including GopherCon India. Shiju has authored two books on Go, titled Web Development with Go and Go Recipes, both published by Apress.
www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. h t t p s ://w w w . p a c k t p u b . c o m /m a p t Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser
Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at h t t p s ://w w w . a m a z o n . c o m /d p /1786466201. If you'd like to join our team of regular reviewers, you can email us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
分享到:
收藏