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

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



Copyright Disclaimer:
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.