Learn C# Quickly: A Complete Beginner’s Guide to Learning C#, Even If You’re New to Programming (Crash Course With Hands-On Project Book 2) by Quickly Code
Author:Quickly, Code [Quickly, Code]
Language: eng
Format: epub, pdf
Published: 2020-05-27T16:00:00+00:00
In this example, we have an interface called IShape, and inside this interface, there are two methods known as Draw() and Stop(). Below that interface, we have one class that implements the IShape interface - Rectangle class. This class has the implementation of the two methods from the interface.
In the Draw() method, the program will print that it is drawing the rectangle, while the Stop() method prints that the program stopped the drawing of the shape. The way the Rectangle class works is the same way the Circle class works. There is an implementation of both methods of the interface. The Draw() method prints that the program is drawing circles, and the Stop() method prints that the program stopped the drawing. So, these classes represent both the drawing of shapes and the stopping of drawing shapes. It describes the control on when to start drawing and when to stop drawing. Below these classes, there is a ShapeManager class, which actually does the whole thing about the dependency inversion principle. In this class, we have one private field _shape , which is of IShape type. We assign the value to this private field in the constructor. This constructor takes one parameter, one IShape type object, and sets it to the private field value. This is called dependency injection . There are a few types of injection; they will be all covered in the Dependency Injection chapter later. For now, it is good to know that this is the constructor injection. In the run-time, when the object is created, we inject the value through the constructor. It is not known which kind of object will be set here until the injection.
In this case, two possible object types could be injected. The _shape field could become either a Rectangle object or the Circle object. Those two classes are currently implementing the IShape interface. After the constructor, there is a method called DrawShape(), whose function is to execute the Draw() method of the _shape variable. While looking at this method, we realize that it is not possible to know which implementation of the Draw() method will happen since we do not know the object that will be injected into the _shape variable. It could do the drawing of the Rectangle and the Circle.
Below DrawShape() method, there is also a StopDrawing() method, which calls the Stop() method of the _shape variable. It works on the same principle as the DrawShape() method. The implementation of the StopDrawing() depends on the injected object inside the variable _shape. Let us put all of these together. In the Program class, in the Main method, we are creating an instance of the Rectangle() class and putting it into the IShape variable type rectangle . Next, we instantiate an object of ShapeManager class, passing the rectangle variable to the creation. At this moment, the injection happens, and the rectangle object value, which is a Rectangle class object, is assigned to the _shape field inside the ShapeManager class object. After that, we then call the DrawShape() method over the shapeManager object.
Download
Learn C# Quickly: A Complete Beginner’s Guide to Learning C#, Even If You’re New to Programming (Crash Course With Hands-On Project Book 2) by Quickly Code.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
NET | C & C++ Windows Programming |
SQL Server | VBA |
Visual Basic |
Deep Learning with Python by François Chollet(12556)
Hello! Python by Anthony Briggs(9904)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9785)
The Mikado Method by Ola Ellnestam Daniel Brolund(9769)
Dependency Injection in .NET by Mark Seemann(9329)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8282)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7756)
Grails in Action by Glen Smith Peter Ledbrook(7686)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7550)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7009)
Microservices with Go by Alexander Shuiskov(6777)
Practical Design Patterns for Java Developers by Miroslav Wengner(6688)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6631)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6400)
Angular Projects - Third Edition by Aristeidis Bampakos(6037)
The Art of Crafting User Stories by The Art of Crafting User Stories(5569)
NetSuite for Consultants - Second Edition by Peter Ries(5500)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5304)
Kotlin in Action by Dmitry Jemerov(5048)
