Table of Contents....5
About the Author....15
About the Technical Reviewer....16
Introduction....17
Part I: Introduction....21
Chapter 1: The SOLID Design Principles....22
Single Responsibility Principle....22
Open-Closed Principle....25
Liskov Substitution Principle....32
Interface Segregation Principle....34
Parameter Object....39
Dependency Inversion Principle....40
Chapter 2: The Functional Perspective....44
Function Basics....44
Functional Literals in C#....46
Storing Functions in C#....47
Functional Literals in F#....49
Composition....51
Functional-Related Language Features....52
Part II: Creational Patterns....53
Chapter 3: Builder....55
Scenario....55
Simple Builder....58
Fluent Builder....59
Static Initialization....59
Communicating Intent....60
Nested Builder and Immutability....62
Composite Builder....63
Builder Marker Interfaces....67
Stepwise Builder (Wizard)....69
Builder Parameter....74
Builder Extension with Recursive Generics....76
Lazy Functional Builder....80
Builder-Decorator....83
Scoping Builder Method....86
DSL Construction in F#....87
Summary....88
Chapter 4: Factories....90
Scenario....90
Factory Method....92
Asynchronous Factory Method....93
Factory....94
Inner Factory....95
Physical Separation....96
Abstract Factory....96
Delegate Factories in IoC....100
Object Tracking and Bulk Replacements....102
Object Tracking....102
Bulk Modifications....104
Functional Factory....107
Summary....108
Chapter 5: Prototype....110
Deep vs. Shallow Copying....110
ICloneable Is Bad....111
Deep Copying via Copy Construction....113
Note on Record Classes....114
Deep Copying with a Special Interface....114
Deep Copying and Inheritance....115
Deep Copying Guidelines....119
Trivially Copyable Types....120
Arrays....120
Common Collection Types....121
MemberwiseClone Is Not Terrible....122
Summary....123
Serialization....124
Prototype Factory....126
Source Generators....127
Summary....128
Chapter 6: Singleton....129
Singleton by Convention....129
Classic Implementation....130
Lazy Loading and Thread Safety....131
Reusable Base Class....132
The Trouble with Singleton....133
Per-Thread Singleton....137
Ambient Context....138
Uses in the .NET Framework....142
Singletons and Inversion of Control....143
Monostate....144
Multiton....145
Summary....146
Part III: Structural Patterns....147
Chapter 7: Adapter....149
Scenario....149
Adapter....151
Adapter Temporaries....152
The Problem with Hashing....156
Property Adapter (Surrogate)....158
Generic Value Adapter....160
Adapter in Dependency Injection....168
Bidirectional Adapter....171
Adapters in the .NET Framework....172
Summary....173
Chapter 8: Bridge....174
Conventional Bridge....174
Dynamic Prototyping Bridge....178
Summary....181
Chapter 9: Composite....182
Grouping Graphic Objects....182
Neural Networks....185
Shrink Wrapping the Composite....188
Composite Specification....190
Summary....193
Chapter 10: Decorator....194
The Basics of Delegation....195
Points and Lines....197
Adapter-Decorator....200
Simulating Multiple Inheritance....200
Multiple Inheritance with Interfaces....201
Multiple Inheritance with Default Interface Members....204
Dynamic Decorator Composition....206
Decorator Cycle Policies....209
Static Decorator Composition....215
Functional Decorator....216
Summary....217
Chapter 11: Façade....219
Magic Squares....220
Building a Trading Terminal....225
An Advanced Terminal....226
Where’s the Façade?....228
IoC Modules....230
Summary....232
Chapter 12: Flyweight....233
User Names....233
Text Formatting....236
Using Flyweights for Interop....239
Summary....240
Chapter 13: Proxy....241
Protection Proxy....241
Property Proxy....243
Composite Proxy: SoA/AoS....246
Composite Proxy with Array-Backed Properties....249
Virtual Proxy....251
Communication Proxy....254
Dynamic Proxy for Logging....256
Composite Proxy....259
Summary....262
Chapter 14: Value Object....264
Two-Dimensional Point....265
Percentage Value....266
Units of Measure....268
Summary....270
Part IV: Behavioral Patterns....271
Chapter 15: Chain of Responsibility....272
Scenario....272
Method Chain....273
Broker Chain....276
Functional Chain of Responsibility....281
Summary....282
Chapter 16: Command....283
Scenario....283
Implementing the Command Pattern....284
Undo Operations....286
Composite Commands (aka Macros)....289
Functional Command....293
Queries and Command-Query Separation....295
Summary....295
Chapter 17: Interpreter....297
Integer Parsing....298
Numeric Expression Evaluator....299
Lexing....300
Parsing....302
Using Lexer and Parser....306
Interpretation in the Functional Paradigm....306
Transpiler....310
Summary....312
Chapter 18: Iterator....314
Array-Backed Properties....315
Let’s Make an Iterator....318
Improved Iteration....321
Iterator Specifics....323
Iterator Adapter....324
Composite Iteration....326
Summary....328
Chapter 19: Mediator....329
Chat Room....329
Mediator with Events....334
Introduction to MediatR....338
Service Bus as Mediator....340
Summary....341
Chapter 20: Memento....342
Bank Account....342
Undo and Redo....344
Memento and Command....347
Summary....348
Chapter 21: Null Object....350
Scenario....350
Intrusive Approaches....352
Nullable Virtual Proxy....353
Null Object....354
Null Object Singleton....355
Dynamic Null Object....356
Drawbacks....357
Summary....358
Chapter 22: Observer....359
Events....359
Weak Event Pattern....361
Event Streams....363
Property Observers....367
Basic Change Notification....367
Bidirectional Bindings....369
Property Dependencies....372
Views....378
Case Study: Quadratic Equation Solver....380
Circular Recalculation Limitations....382
Observable Collections....383
Observable LINQ....384
Declarative Subscriptions in Autofac....384
Summary....388
Chapter 23: State....389
State-Driven State Transitions....390
Enum-Based State Machine....393
Switch-Based State Machine....396
Encoding Transitions with Switch Expressions....398
State Machines with Stateless....400
Types, Actions, and Ignoring Transitions....401
Reentrancy Again....402
Hierarchical States....403
More Features....403
Concurrent State Machines....404
Implicit State Machines....405
Summary....405
Chapter 24: Strategy....407
Dynamic Strategy....407
Static Strategy....410
Equality and Comparison Strategies....412
Functional Strategy....414
Declarative Strategies....415
Summary....416
Chapter 25: Template Method....417
Game Simulation....417
Template Method Mixin....419
Functional Template Method....421
Summary....422
Chapter 26: Visitor....423
Intrusive Visitor....424
Reflective Visitor....425
Extension Methods?....428
Functional Reflective Visitor (C#)....430
Functional Reflective Visitor (F#)....432
Improvements....433
What Is Dispatch?....434
Dynamic Visitor....436
Classic Visitor....438
Abstract Classes and Virtual Methods....441
Reducing Boilerplate....443
Implementing an Additional Visitor....443
Type Checks Are Unavoidable....445
Acyclic Visitor....447
Visitable Null Object....449
Visitor Adapter....453
Reductions and Transforms....456
Functional Visitor in F#....460
Summary....461
Index....463
Implement design patterns in .NET 6 using the latest versions of the C# and F# languages. This book provides a comprehensive overview of the field of design patterns as they are used in today’s developer toolbox. In addition to the functional builder, asynchronous factory method, generic value adapter, and composite proxies, this new edition introduces topics such as Decorator Cycle Policies Functional Commands, a Transformer variation of the Visitor pattern, and factories that can perform Object Tracking and Bulk Replacement.Using the C# and F# programming languages, Design Patterns in .NET 6 explores the classic design pattern implementations and discusses the applicability and relevance of specific language features for implementing patterns. You will learn by example, reviewing scenarios where patterns are applicable. Former C# MVP and patterns expert Dmitri Nesteruk demonstrates possible implementations of patterns,discusses alternatives and pattern relationships, and illustrates the way that a dedicated refactoring tool (JetBrains Rider) can be used to implement design patterns with ease.
Developers who have some experience in the C# language and want to expand their comprehension of the art of programming by leveraging design approaches to solve modern problems