(Ebook) Design Patterns in .NET Core 3: Reusable Approaches in C# and F# for Object-Oriented Software Design by Dmitri Nesteruk ISBN 9781484261798, 9781484261804, 9781501171772, 1484261798, 1501171771, 1484261801 download
(Ebook) Design Patterns in .NET Core 3: Reusable Approaches in C# and F# for Object-Oriented Software Design by Dmitri Nesteruk ISBN 9781484261798, 9781484261804, 9781501171772, 1484261798, 1501171771, 1484261801 download
https://ebooknice.com/product/design-patterns-in-net-6-reusable-approaches-in-c-
and-f-for-object-oriented-software-design-44960808
ebooknice.com
https://ebooknice.com/product/design-patterns-in-net-reusable-approaches-in-c-
and-f-for-object-oriented-software-design-33645242
ebooknice.com
https://ebooknice.com/product/design-patterns-in-modern-c-reusable-approaches-
for-object-oriented-software-design-11077246
ebooknice.com
https://ebooknice.com/product/design-patterns-in-modern-c-20-reusable-
approaches-for-object-oriented-software-design-36127942
ebooknice.com
(Ebook) Design Patterns in Modern C++20: Reusable Approaches for Object-Oriented
Software Design by Dmitri Nesteruk ISBN 9781484272947, 1484272943
https://ebooknice.com/product/design-patterns-in-modern-c-20-reusable-
approaches-for-object-oriented-software-design-36137854
ebooknice.com
(Ebook) Biota Grow 2C gather 2C cook by Loucas, Jason; Viles, James ISBN
9781459699816, 9781743365571, 9781925268492, 1459699815, 1743365578, 1925268497
https://ebooknice.com/product/biota-grow-2c-gather-2c-cook-6661374
ebooknice.com
(Ebook) Matematik 5000+ Kurs 2c Lärobok by Lena Alfredsson, Hans Heikne, Sanna
Bodemyr ISBN 9789127456600, 9127456609
https://ebooknice.com/product/matematik-5000-kurs-2c-larobok-23848312
ebooknice.com
(Ebook) SAT II Success MATH 1C and 2C 2002 (Peterson's SAT II Success) by Peterson's
ISBN 9780768906677, 0768906679
https://ebooknice.com/product/sat-ii-success-math-1c-and-2c-2002-peterson-s-sat-
ii-success-1722018
ebooknice.com
(Ebook) Master SAT II Math 1c and 2c 4th ed (Arco Master the SAT Subject Test: Math
Levels 1 & 2) by Arco ISBN 9780768923049, 0768923042
https://ebooknice.com/product/master-sat-ii-math-1c-and-2c-4th-ed-arco-master-
the-sat-subject-test-math-levels-1-2-2326094
ebooknice.com
Dmitri Nesteruk
The publisher, the authors and the editors are safe to assume that the
advice and information in this book are believed to be true and accurate
at the date of publication. Neither the publisher nor the authors or the
editors give a warranty, expressed or implied, with respect to the
material contained herein or for any errors or omissions that may have
been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
On Code Examples
The examples in this book are all suitable for putting into production,
but a few simplifications have been made in order to aid readability:
I use public fields. This is not a coding recommendation, but rather
an attempt to save you time. In the real world, more thought should
be given to proper encapsulation, and in most cases, you probably
want to use properties instead.
I often allow too much mutability either by not using readonly or
by exposing structures in such a way that their modification can
cause threading concerns. We cover concurrency issues for a few
select patterns, but I haven’t focused on each one individually.
I don’t do any sort of parameter validation or exception handling,
again to save some space. Some very clever validation can be done
using C# 8 pattern matching, but this doesn’t have much to do with
design patterns.
You should be aware that most of the examples leverage the latest
version of C# and generally use the latest C# language features that are
available to developers. For example, I use dynamic, pattern matching,
and expression-bodied members liberally.
At certain points in time, I will be referencing other programming
languages such as C++ or Kotlin. It’s sometimes interesting to note how
designers of other languages have implemented a particular feature. C#
is no stranger to borrowing generally available ideas from other
languages, so I will mention those when we come to them.
2 Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, Design Patterns:
Elements of Reusable Object-Oriented Software (Reading, MA: Addison Wesley, 1994).
Part I
Introduction
© Dmitri Nesteruk 2020
D. Nesteruk, Design Patterns in .NET Core 3
https://doi.org/10.1007/978-1-4842-6180-4_1
SOLID is an acronym which stands for the following design principles (and their
abbreviations):
Single Responsibility Principle (SRP)
Open-Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Interface Segregation Principle (ISP)
Dependency Inversion Principle (DIP)
These principles were introduced by Robert C. Martin in the early 2000s – in
fact, they are just a selection of five principles out of dozens that are expressed in
Robert’s books and his blog. These five particular topics permeate the discussion of
patterns and software design in general, so, before we dive into design patterns (I
know you’re all eager), we’re going to do a brief recap of what the SOLID principles
are all about.
Now, you could add functionality for adding an entry to the journal, prefixed by
the entry’s ordinal number in the journal. You could also have functionality for
removing entries (implemented in a very crude way in the following). This is easy:
public void AddEntry(string text)
{
entries.Add($"{++count}: {text}");
}
It makes sense to have this method as part of the Journal class because adding
a journal entry is something the journal actually needs to do. It is the journal’s
responsibility to keep entries, so anything related to that is fair game.
Now, suppose you decide to make the journal persist by saving it to a file. You
add this code to the Journal class:
Open-Closed Principle
Suppose we have an (entirely hypothetical) range of products in a database. Each
product has a color and size and is defined as follows:
Our current approach of filtering items by color is all well and good, though of
course it could be greatly simplified with the use of Language Integrated Query
(LINQ). So, our code goes into production but, unfortunately, some time later, the
boss comes in and asks us to implement filtering by size, too. So we jump back into
ProductFilter.cs, add the following code, and recompile:
This feels like outright duplication, doesn’t it? Why don’t we just write a general
method that takes a predicate (i.e., a Predicate<T>)? Well, one reason could be
that different forms of filtering can be done in different ways: for example, some
record types might be indexed and need to be searched in a specific way; some data
types are amenable to search on a Graphics Processing Unit (GPU), while others are
not.
Furthermore, you might want to restrict the criteria one can filter on. For
example, if you look at Amazon or a similar online store, you are only allowed to
perform filtering on a finite set of criteria. Those criteria can be added or removed
by Amazon if they find that, say, sorting by number of reviews interferes with the
bottom line.
Okay, so our code goes into production but, once again, the boss comes back and
tells us that now there’s a need to search by both size and color. So what are we to
do but add another function?
Armed with this specification, and given a list of products, we can now filter
them as follows:
And now, you are free to create composite conditions on the basis of simpler
ISpecifications. Reusing the green specification we made earlier, finding
something green and big is now as simple as
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com