📄 Page
1
(This page has no text content)
📄 Page
2
(This page has no text content)
📄 Page
3
Design Patterns in .NET Mastering design patterns to write dynamic and effective .NET Code Timur Yaroshenko
📄 Page
4
www.bpbonline.com
📄 Page
5
Copyright © 2024 BPB Online 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 BPB Online or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. BPB Online has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, BPB Online cannot guarantee the accuracy of this information. First published: 2024 Published by BPB Online WeWork 119 Marylebone Road London NW1 5PU UK | UAE | INDIA | SINGAPORE ISBN 978-93-55517-821 www.bpbonline.com
📄 Page
6
Dedicated to My beloved cat: Kuzja
📄 Page
7
About the Author Timur Yaroshenko, started his journey in programming at the age of eight by writing his first line of code at school. In 2003, he started his professional career in software development: developing, designing, and architecting web/distributed applications. After working for a software outsourcing company for a couple of years, he became an independent consultant and has participated in many projects. He is now an independent consultant in the Senior Software Developer position at C.T.Co Latvia. Timur is passionate about .NET Core, C#, software architecture, and the cloud. His love for sharing knowledge led him to write his first book, the one you now hold in your hands.
📄 Page
8
About the Reviewer Anton Tereshko, a seasoned, certified software developer, holds a degree in Computer Software Engineering. His extensive career, spanning over eight years in the software development sector, includes three years of demonstrated leadership. He has proficiently led and streamlined the development processes for application services across numerous large-scale businesses operating in complex sectors such as medicine, transport and logistics, and insurance, utilizing his expert knowledge of MAUI, Xamarin, .NET, Flutter, and Microsoft Azure technologies. Embracing the rise of AI/ML technologies, Anton is actively engaged in the development of applications featuring artificial intelligence. He holds a certificate in Azure Cognitive Service and continues to advance in this direction. In addition to his software development accomplishments, Anton is a serial entrepreneur, having co-founded multiple technology startups.
📄 Page
9
Acknowledgement I want to express my deepest gratitude to the owner of the computer club in which I fell in love with gaming and became interested in computer technology. I am also grateful to BPB Publications for their guidance and expertise in bringing this book to fruition. It was a long journey of revising this book, with valuable participation and collaboration of reviewers, technical experts, and editors. I would also like to acknowledge the valuable contributions of my colleagues and co-workers during many years of working in the tech industry, who have taught me so much and provided valuable feedback on my work. Finally, I would like to thank all the readers who have taken an interest in my book and for their support in making it a reality. Your encouragement has been invaluable.
📄 Page
10
Preface .NET. This small and simple word is already known in the world of software development for 20 years. Each year, the evolution of this platform has been enormous. Microsoft invested a significant effort to modernize and make this platform competitive in our rapidly changing world. Now, after 20 years, it has become one of the leading platforms for developing software in many areas, including enterprise and distributed systems, mobile, games, home pages, IoT systems, AI, etc. However, to use it efficiently, it is not enough to just learn the language and libraries. You need to start thinking from a new perspective. This book can help you take your first step on the path of writing efficient, concise code that will be understandable by your colleagues and executed quickly by your devices. Chapter by chapter, you will learn how to structure your code to make it concise. The first few chapters will cover the history, evolution, and concepts of design patterns. You will be introduced to the Gangs of Four (GoF) and SOLID approaches. Next, we will explore the most famous and usable patterns that help software developers simplify their everyday work and solve complex problems. In the end, you will see how modern .NET Core platform supports and uses many of these patterns in its standard libraries and packages. This book can give you insights into how modern software is written, the problems that software developers face in their daily work, and how they solve the design and structure of
📄 Page
11
the code. After reading this book, you will become a better developer who can communicate effectively with experienced colleagues and solve complex tasks with minimum effort.Parte superior do formulárioParte inferior do formulário Chapter 1: Main OOP Standpoints - In this chapter, we will learn about the OOP paradigm and see how .NET Core implements it. To better understand the purpose of Design patterns and principles, you need to grasp the fundamental blocks of OOP, including Inheritance, Encapsulation, and Polymorphism. Also, we will review the additional capabilities that .NET Core and C# provide to developers: generic, extension methods, and interfaces. Chapter 2: Creational Design Patterns: Factory, and Builder - This chapter explores object creation using the Factory, Abstract Factory, and Builder Design patterns. These are some of the most used design patterns. They provide the best ways to create an object without exposing the creation logic to the caller and allow access to newly created objects using a standard interface. Chapter 3: Creational Design Patterns: Singleton and Prototype - This chapter explores object creation using Singleton and Prototype Design patterns. These are some of the simplest design patterns from GoF. Chapter 4: Structural Design Patterns: Adapter, Composite, and Flyweight - This chapter explores object composition using Adapter, Composite, and Flyweight design patterns. These three patterns provide three cornerstones of structural decomposition: interface to the outside world, encapsulation of a group of objects, and sharing a common state between a group of objects.
📄 Page
12
Chapter 5: Structural Design Patterns: Object Composition - This chapter explores object composition using Proxy, Facade, Bridge, and Decorator design patterns. These patterns provide the possibility to conceal the actual implementation of the object while remaining interconnected and being able to interact with it. Chapter 6: Object Behavioral Design Patterns- This chapter explores object behaviors using the Template method, Strategy, and Chain of responsibility design patterns. These three patterns are basic for our processing pipeline. With them, we can redefine our pipelines modularly, so it will be easy to add new pipelines for different events. Chapter 7: Behavioral Design Patterns: Observer, Visitor, and State - This chapter explores object behaviors using the Observer, Visitor, and State design patterns. These design patterns provide the possibility to react and modify the state of objects in reaction to changes in other objects. Chapter 8: Behavioral Design Patterns: Mediator and Command - This chapter explores object behaviors using Mediator and Command design patterns. These two patterns provide the possibility to achieve loose coupling between objects that need to communicate with each other. Chapter 9: Behavioral Design Patterns: Interpreter, Iterator, and Memento - This chapter explores object behaviors using three different design patterns. These objects are not often used, so we will review them together. Chapter 10: The SOLID Principles - Here, we will discuss SOLID architectural principles, their history, and structure.
📄 Page
13
Chapter 11: Inversion of Control in .NET Core - In this chapter, we will see how we can use the SOLID principles in .NET Core with its dependency injection support.
📄 Page
14
Code Bundle and Coloured Images Please follow the link to download the Code Bundle and the Coloured Images of the book: https://rebrand.ly/9edcm5m The code bundle for the book is also hosted on GitHub at https://github.com/bpbpublications/Design-Patterns- in-.NET. In case there's an update to the code, it will be updated on the existing GitHub repository. We have code bundles from our rich catalogue of books and videos available at https://github.com/bpbpublications. Check them out! Errata We take immense pride in our work at BPB Publications and follow best practices to ensure the accuracy of our content to provide with an indulging reading experience to our subscribers. Our readers are our mirrors, and we use their inputs to reflect and improve upon human errors, if any, that may have occurred during the publishing processes involved. To let us maintain the quality and help us reach out to any readers who might be having difficulties due to any unforeseen errors, please write to us at : errata@bpbonline.com
📄 Page
15
Your support, suggestions and feedbacks are highly appreciated by the BPB Publications’ Family. Did you know that BPB offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.bpbonline.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at : business@bpbonline.com for more details. At www.bpbonline.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 BPB books and eBooks. Piracy If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at business@bpbonline.com with a link to the material. If you are interested in becoming an author If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit www.bpbonline.com. We have worked with thousands of developers and tech professionals, just like you, to help them share their insights with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea. Reviews Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions. We at BPB can understand what you think about our products, and our authors can see your feedback on their book. Thank you! For more information about BPB, please visit www.bpbonline.com. Join our book’s Discord space Join the book’s Discord Workspace for Latest updates, Offers, Tech happenings around the world, New Release and Sessions with the Authors:
📄 Page
16
https://discord.bpbonline.com
📄 Page
17
Table of Contents 1. Main OOP Standpoints Introduction Structure Objectives Inheritance Polymorphism Encapsulation Main OOP standpoints in .NET Interfaces Abstract classes Generics Extension methods Partial classes Conclusion 2. Creational Design Patterns: Factory, and Builder Introduction Structure Objectives Factory Designing the processing pipeline Code
📄 Page
18
Abstract Factory Designing the processing event hub Code Builder Designing the processing event hub Code Synergy of the patterns Code Conclusion 3. Creational Design Patterns: Singleton and Prototype Introduction Structure Objectives Singleton Designing the configuration provider Design Code Prototype Designing a cloneable pipeline Code Synergy of the patterns Adjusting the processing engine Code Conclusion 4. Structural Design Patterns: Adapter, Composite, and Flyweight Introduction
📄 Page
19
Structure Objectives Adapter Design interface to the outside world Code Composite Design bulk processing pipeline Code Flyweight Sharing a common state Code Adjusting the processing engine Code Conclusion 5. Structural Design Patterns: Object Composition Introduction Structure Objectives Proxy Design the interface to outside world Code Facade Code Bridge Ease of changes Code Decorator Making functionality modular
📄 Page
20
Code Synergy of the patterns Adjusting the processing pipeline Code Conclusion 6. Object Behavioral Design Patterns Introduction Structure Objectives Template method Simplify pipelines creation and design Code Strategy Functionality split Code Chain of responsibility Modularizing the pipeline Code Synergy of the patterns Conclusion 7. Behavioral Design Patterns: Observer, Visitor, and State Introduction Structure Objectives Observer System notifications mechanism Code