Table Of ContentDesign
Patterns in
.NET Core 3
Reusable Approaches in C# and F#
for Object-Oriented Software Design
—
Second Edition
—
Dmitri Nesteruk
Design Patterns in
.NET Core 3
Reusable Approaches in C#
and F# for Object-Oriented
Software Design
Second Edition
Dmitri Nesteruk
Design Patterns in .NET Core 3: Reusable Approaches in C# and F# for
Object- Oriented Software Design
Dmitri Nesteruk
St. Petersburg, c.St-Petersburg, Russia
ISBN-13 (pbk): 978-1-4842-6179-8 ISBN-13 (electronic): 978-1-4842-6180-4
https://doi.org/10.1007/978-1-4842-6180-4
Copyright © 2020 by Dmitri Nesteruk
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, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Joan Murray
Development Editor: Laura Berendson
Coordinating Editor: Jill Balzano
Cover image designed by Freepik (www.freepik.com)
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 orders-ny@springer-
sbm.com, or visit www.springeronline.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 booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles 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 Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484261798. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
Table of Contents
About the Author �����������������������������������������������������������������������������������������������������xi
About the Technical Reviewer �������������������������������������������������������������������������������xiii
Introduction �������������������������������������������������������������������������������������������������������������xv
Part I: Introduction �����������������������������������������������������������������������������������������1
Chapter 1: The SOLID Design Principles �������������������������������������������������������������������3
Single Responsibility Principle ������������������������������������������������������������������������������������������������������3
Open-Closed Principle�������������������������������������������������������������������������������������������������������������������6
Liskov Substitution Principle �������������������������������������������������������������������������������������������������������13
Interface Segregation Principle ��������������������������������������������������������������������������������������������������15
Parameter Object �������������������������������������������������������������������������������������������������������������������19
Dependency Inversion Principle ��������������������������������������������������������������������������������������������������20
Chapter 2: The Functional Perspective �������������������������������������������������������������������25
Function Basics ���������������������������������������������������������������������������������������������������������������������������25
Functional Literals in C# �������������������������������������������������������������������������������������������������������������27
Storing Functions in C# ���������������������������������������������������������������������������������������������������������������28
Functional Literals in F# ��������������������������������������������������������������������������������������������������������������30
Composition ��������������������������������������������������������������������������������������������������������������������������������32
Functional-Related Language Features ��������������������������������������������������������������������������������������33
Part II: Creational Patterns ���������������������������������������������������������������������������35
Chapter 3: Builder ���������������������������������������������������������������������������������������������������37
Scenario ��������������������������������������������������������������������������������������������������������������������������������������37
Simple Builder �����������������������������������������������������������������������������������������������������������������������������39
Fluent Builder ������������������������������������������������������������������������������������������������������������������������������40
iii
Table of ConTenTs
Communicating Intent �����������������������������������������������������������������������������������������������������������������41
Composite Builder �����������������������������������������������������������������������������������������������������������������������43
Builder Parameter �����������������������������������������������������������������������������������������������������������������������46
Builder Extension with Recursive Generics ��������������������������������������������������������������������������������48
Lazy Functional Builder ���������������������������������������������������������������������������������������������������������������53
DSL Construction in F# ����������������������������������������������������������������������������������������������������������������56
Summary�������������������������������������������������������������������������������������������������������������������������������������57
Chapter 4: Factories �����������������������������������������������������������������������������������������������59
Scenario ��������������������������������������������������������������������������������������������������������������������������������������59
Factory Method ���������������������������������������������������������������������������������������������������������������������������61
Asynchronous Factory Method ���������������������������������������������������������������������������������������������������62
Factory ����������������������������������������������������������������������������������������������������������������������������������������63
Inner Factory �������������������������������������������������������������������������������������������������������������������������������64
Physical Separation ���������������������������������������������������������������������������������������������������������������65
Abstract Factory ��������������������������������������������������������������������������������������������������������������������������65
Delegate Factories in IoC ������������������������������������������������������������������������������������������������������������69
Functional Factory ����������������������������������������������������������������������������������������������������������������������71
Summary�������������������������������������������������������������������������������������������������������������������������������������72
Chapter 5: Prototype �����������������������������������������������������������������������������������������������75
Deep vs� Shallow Copying �����������������������������������������������������������������������������������������������������������75
ICloneable Is Bad ������������������������������������������������������������������������������������������������������������������������76
Deep Copying with a Special Interface ���������������������������������������������������������������������������������������77
Deep-Copying Objects �����������������������������������������������������������������������������������������������������������������78
Duplication via Copy Construction ����������������������������������������������������������������������������������������������79
Serialization ��������������������������������������������������������������������������������������������������������������������������������81
Prototype Factory ������������������������������������������������������������������������������������������������������������������������82
Summary�������������������������������������������������������������������������������������������������������������������������������������84
iv
Table of ConTenTs
Chapter 6: Singleton �����������������������������������������������������������������������������������������������87
Singleton by Convention �������������������������������������������������������������������������������������������������������������87
Classic Implementation ���������������������������������������������������������������������������������������������������������������88
Lazy Loading and Thread Safety ��������������������������������������������������������������������������������������������89
Singletons and Inversion of Control ���������������������������������������������������������������������������������������93
Summary�������������������������������������������������������������������������������������������������������������������������������������97
Part III: Structural Patterns ��������������������������������������������������������������������������99
Chapter 7: Adapter �����������������������������������������������������������������������������������������������101
Scenario ������������������������������������������������������������������������������������������������������������������������������������101
Adapter �������������������������������������������������������������������������������������������������������������������������������������103
Adapter Temporaries �����������������������������������������������������������������������������������������������������������������104
The Problem with Hashing ��������������������������������������������������������������������������������������������������������108
Property Adapter (Surrogate) ����������������������������������������������������������������������������������������������������110
Generic Value Adapter ���������������������������������������������������������������������������������������������������������������112
Adapter in Dependency Injection ����������������������������������������������������������������������������������������������120
Adapters in the �NET Framework ����������������������������������������������������������������������������������������������123
Summary�����������������������������������������������������������������������������������������������������������������������������������124
Chapter 8: Bridge ��������������������������������������������������������������������������������������������������125
Conventional Bridge ������������������������������������������������������������������������������������������������������������������125
Dynamic Prototyping Bridge �����������������������������������������������������������������������������������������������������129
Summary�����������������������������������������������������������������������������������������������������������������������������������131
Chapter 9: Composite �������������������������������������������������������������������������������������������133
Grouping Graphic Objects ���������������������������������������������������������������������������������������������������������133
Neural Networks �����������������������������������������������������������������������������������������������������������������������136
Shrink Wrapping the Composite������������������������������������������������������������������������������������������������139
Composite Specification �����������������������������������������������������������������������������������������������������������141
Summary�����������������������������������������������������������������������������������������������������������������������������������142
v
Table of ConTenTs
Chapter 10: Decorator ������������������������������������������������������������������������������������������143
Custom String Builder ���������������������������������������������������������������������������������������������������������������143
Adapter-Decorator ���������������������������������������������������������������������������������������������������������������������145
Multiple Inheritance with Interfaces �����������������������������������������������������������������������������������������146
Multiple Inheritance with Default Interface Members ��������������������������������������������������������������150
Dynamic Decorator Composition �����������������������������������������������������������������������������������������������151
Static Decorator Composition ���������������������������������������������������������������������������������������������������154
Functional Decorator �����������������������������������������������������������������������������������������������������������������156
Summary�����������������������������������������������������������������������������������������������������������������������������������157
Chapter 11: Façade �����������������������������������������������������������������������������������������������159
Magic Squares ��������������������������������������������������������������������������������������������������������������������������160
Building a Trading Terminal �������������������������������������������������������������������������������������������������������165
An Advanced Terminal ���������������������������������������������������������������������������������������������������������������166
Where’s the Façade? ����������������������������������������������������������������������������������������������������������������168
Summary�����������������������������������������������������������������������������������������������������������������������������������170
Chapter 12: Flyweight ������������������������������������������������������������������������������������������171
User Names �������������������������������������������������������������������������������������������������������������������������������171
Text Formatting �������������������������������������������������������������������������������������������������������������������������174
Summary�����������������������������������������������������������������������������������������������������������������������������������177
Chapter 13: Proxy �������������������������������������������������������������������������������������������������179
Protection Proxy ������������������������������������������������������������������������������������������������������������������������179
Property Proxy ���������������������������������������������������������������������������������������������������������������������������181
Value Proxy �������������������������������������������������������������������������������������������������������������������������������184
Composite Proxy: SoA/AoS ��������������������������������������������������������������������������������������������������������186
Composite Proxy with Array-Backed Properties �����������������������������������������������������������������������189
Virtual Proxy ������������������������������������������������������������������������������������������������������������������������������191
Communication Proxy ���������������������������������������������������������������������������������������������������������������193
Dynamic Proxy for Logging �������������������������������������������������������������������������������������������������������196
Summary�����������������������������������������������������������������������������������������������������������������������������������199
vi
Table of ConTenTs
Part IV: Behavioral Patterns������������������������������������������������������������������������201
Chapter 14: Chain of Responsibility ���������������������������������������������������������������������203
Scenario ������������������������������������������������������������������������������������������������������������������������������������203
Method Chain ����������������������������������������������������������������������������������������������������������������������������204
Broker Chain �����������������������������������������������������������������������������������������������������������������������������207
Summary�����������������������������������������������������������������������������������������������������������������������������������212
Chapter 15: Command ������������������������������������������������������������������������������������������213
Scenario ������������������������������������������������������������������������������������������������������������������������������������213
Implementing the Command Pattern �����������������������������������������������������������������������������������214
Undo Operations ������������������������������������������������������������������������������������������������������������������216
Composite Commands (a�k�a� Macros) ��������������������������������������������������������������������������������219
Functional Command �����������������������������������������������������������������������������������������������������������223
Queries and Command-Query Separation ���������������������������������������������������������������������������225
Summary�����������������������������������������������������������������������������������������������������������������������������������225
Chapter 16: Interpreter �����������������������������������������������������������������������������������������227
Numeric Expression Evaluator ��������������������������������������������������������������������������������������������������228
Lexing ����������������������������������������������������������������������������������������������������������������������������������228
Parsing ��������������������������������������������������������������������������������������������������������������������������������231
Using Lexer and Parser �������������������������������������������������������������������������������������������������������234
Interpretation in the Functional Paradigm ��������������������������������������������������������������������������������235
Summary�����������������������������������������������������������������������������������������������������������������������������������239
Chapter 17: Iterator ����������������������������������������������������������������������������������������������241
Array-Backed Properties �����������������������������������������������������������������������������������������������������������242
Let’s Make an Iterator ���������������������������������������������������������������������������������������������������������������245
Improved Iteration ���������������������������������������������������������������������������������������������������������������������248
Iterator Adapter �������������������������������������������������������������������������������������������������������������������������249
Summary�����������������������������������������������������������������������������������������������������������������������������������251
vii
Table of ConTenTs
Chapter 18: Mediator ��������������������������������������������������������������������������������������������253
Chat Room���������������������������������������������������������������������������������������������������������������������������������253
Mediator with Events ����������������������������������������������������������������������������������������������������������������258
Introduction to MediatR ������������������������������������������������������������������������������������������������������������262
Summary�����������������������������������������������������������������������������������������������������������������������������������265
Chapter 19: Memento �������������������������������������������������������������������������������������������267
Bank Account ����������������������������������������������������������������������������������������������������������������������������267
Undo and Redo ��������������������������������������������������������������������������������������������������������������������������269
Using Memento for Interop �������������������������������������������������������������������������������������������������������272
Summary�����������������������������������������������������������������������������������������������������������������������������������273
Chapter 20: Null Object �����������������������������������������������������������������������������������������275
Scenario ������������������������������������������������������������������������������������������������������������������������������������275
Intrusive Approaches ����������������������������������������������������������������������������������������������������������������276
Null Object Virtual Proxy ������������������������������������������������������������������������������������������������������277
Null Object���������������������������������������������������������������������������������������������������������������������������������278
Dynamic Null Object ������������������������������������������������������������������������������������������������������������������279
Summary�����������������������������������������������������������������������������������������������������������������������������������280
Chapter 21: Observer ��������������������������������������������������������������������������������������������283
Observer������������������������������������������������������������������������������������������������������������������������������������283
Weak Event Pattern �������������������������������������������������������������������������������������������������������������������285
Event Streams ���������������������������������������������������������������������������������������������������������������������������287
Property Observers �������������������������������������������������������������������������������������������������������������������291
Basic Change Notification ���������������������������������������������������������������������������������������������������291
Bidirectional Bindings����������������������������������������������������������������������������������������������������������293
Property Dependencies �������������������������������������������������������������������������������������������������������296
Views �����������������������������������������������������������������������������������������������������������������������������������302
Observable Collections��������������������������������������������������������������������������������������������������������������303
Observable LINQ ������������������������������������������������������������������������������������������������������������������304
Declarative Subscriptions in Autofac ����������������������������������������������������������������������������������������305
Summary�����������������������������������������������������������������������������������������������������������������������������������309
viii
Table of ConTenTs
Chapter 22: State ��������������������������������������������������������������������������������������������������311
State-Driven State Transitions ��������������������������������������������������������������������������������������������������312
Handmade State Machine ���������������������������������������������������������������������������������������������������������315
Switch-Based State Machine ����������������������������������������������������������������������������������������������������318
Encoding Transitions with Switch Expressions �������������������������������������������������������������������������320
State Machines with Stateless��������������������������������������������������������������������������������������������������322
Types, Actions, and Ignoring Transitions ������������������������������������������������������������������������������323
Reentrancy Again �����������������������������������������������������������������������������������������������������������������324
Hierarchical States ��������������������������������������������������������������������������������������������������������������325
More Features ���������������������������������������������������������������������������������������������������������������������325
Summary�����������������������������������������������������������������������������������������������������������������������������������326
Chapter 23: Strategy ���������������������������������������������������������������������������������������������329
Dynamic Strategy ����������������������������������������������������������������������������������������������������������������������329
Static Strategy ��������������������������������������������������������������������������������������������������������������������������333
Equality and Comparison Strategies �����������������������������������������������������������������������������������������334
Functional Strategy �������������������������������������������������������������������������������������������������������������������336
Summary�����������������������������������������������������������������������������������������������������������������������������������337
Chapter 24: Template Method �������������������������������������������������������������������������������339
Game Simulation �����������������������������������������������������������������������������������������������������������������������339
Functional Template Method �����������������������������������������������������������������������������������������������������341
Summary�����������������������������������������������������������������������������������������������������������������������������������343
Chapter 25: Visitor ������������������������������������������������������������������������������������������������345
Intrusive Visitor �������������������������������������������������������������������������������������������������������������������������346
Reflective Printer ����������������������������������������������������������������������������������������������������������������������347
Extension Methods? ������������������������������������������������������������������������������������������������������������349
Functional Reflective Visitor ������������������������������������������������������������������������������������������������352
Improvements ����������������������������������������������������������������������������������������������������������������������353
What Is Dispatch? ���������������������������������������������������������������������������������������������������������������������354
Dynamic Visitor �������������������������������������������������������������������������������������������������������������������������356
ix