hey code to the interface code to the interface a lot of us create interfaces
just to satisfy that mantra how are we supposed to be using interfaces in our object-oriented design
and in our code i'm chuck mccullough and in this video we're going to take a look at that so make
sure you hit that subscribe button and like our video tell your friends about it and let's dive
into this topic of interfaces for the point of our discussion let's take a look at a class called
circle now we could write this in any language we put it in c sharp here but you could practically
convert this to java or c plus plus in our class we have a circle and it has a radius
uh we've got a constructor function that takes a radius and a color we also have a property a
getter and center for that fill color and the circle implements two methods calculate area
and calculate perimeter now what we see happen so much of the time in bad code or bad design
is extracting an identical interface that really doesn't accomplish anything and here are some of
the symptoms of why that's a bad idea we have a class now and an interface that are identical so
every time we add a property or we add a method or we do anything to either the interface or
the class we have to make the same change to the other now another smell that we recognize is that
there's only one implementer of this interface and it's the class that it was extracted from and that
leads us to the last thing and that's where the client declares the variable of the interface type
but it news the class type now why does this happen well i think designers are thinking about
interfaces as an alternative to inheritance and or they're motivated to create interfaces for
the same reasons that they're motivated to define inheritance and inheritance is a similar solution
a similar tool but it's got a different motivation with inheritance abstractions are derived from the
concrete types we're defining interfaces from the caller's point of view the circle class shouldn't
have anything to do with defining an interface that might be used by a client now when we
create interfaces like we just did with the tool it leads us to code like this where the client
is dependent on both the class and the interface in an object-oriented design the client shouldn't
depend on the library so whoever's using the circles shouldn't have dependency on circle
in other words it needs to be inverted something we call dependency inversion
now how do we define the interface correctly well let's take a look at this client code
we've got a method called calculate total area and we're passing in an array of doubles and
in a loop we go through and we create a circle from each one of those doubles which is the
radius of the circle and then we calculate the area of the circle and sum or total it up into
a total area variable which we eventually return we have of course the class is actually creating
the concrete type and we really want to try to isolate new we need to have the dependency
which is circle we need to get rid of that and if you're newing something you're very
inflexible so the code's creating a concrete type it's inflexible because it can only calculate the
area of circles we also have the fact that this is dependent on both the interface and the concrete
type we also have a bigger problem and this is something that escapes a lot of designs our code
calculate total area is dependent on a type in this case circle and i circle that has methods
that are never used by our calculate total area we only require one method and that's calculate area
yet we are dependent on a type that has a bunch of other capabilities as well something we call fat
interfacing and it is a problem because now we require something that is more complex and more
involved than what's really necessary and that's that's really the reason that we're motivated
to create the interface so let's look at that and and what we need to do to actually correct
these problems that we talked about okay the solid design principles and we've got a video series on
that as well so take a look at the card above the i in solid interface segregation and the d
dependency inversion tells us that the client here should be defining and owning the interface
the interface is defined by the caller not the callee so before circle defined the interface we
need to have the code we just looked at the client code the caller determine the interface and we're
going to get rid of the new inside this code and have the objects passed in to the algorithm rather
than being created by the algorithm so let's look at our area calculator another way and we're going
to define an interface based on just the methods that we require and that is only to calculate area
method also notice that we're passing into our method instead of radio radii instead of radiuses
or radii we're passing in measurable objects now anything could implement measurable circles
rectangles triangles houses dogs cats anything that could return an area that can be computed
and those objects can be otherwise completely unrelated to one another we also did some fancy
stuff here in this method with the new c sharp feature called expression methods which allows us
to avoid the curly braces and the return keyword for single line methods the fat arrow here on the
left is the introducer for the expression method it looks like a lambda expression but it's not
this is the lambda expression uh the method that we're defining on the fly that we're passing into
the method sum so a little bit going on here with c sharp but the point is that calculate total area
defined its own interface and that interface measurable is owned by the development team
that it also created the calculate total area method so now we've inverted the dependency
because circle is dependent on measurable as it has to mention measurable in its definition
and it also has to implement the interface and conveniently it actually did implement
that interface already and this is what we mean by dependency inversion
now we see this technique used throughout the dot net library and this is why we know this is
a good idea because it works we see this in in all standard libraries where the library
defines an interface that has to be implemented by the algorithms now in c sharp we can define
interfaces using the keyword interface same with java in c plus plus we can still define
interfaces we use the keyword class and we just define a class that's completely pure virtual
in other words an interface now we can have default method implementations in interfaces
but that's a different topic and you can take a look at our videos on that as well we can also
in.net define something known as a delegate now in java that's called a functional interface it's
just one method rather than a bunch of methods that are together and so in the sum method
in net the link sum method accepts an interface as its parameter that's how it defines its its
input parameter as a delegate so it's not a full-blown interface with a whole bunch
of methods potentially but instead just a single method signature sum wants us to pass in a funk
funk is a delegate that's also defined as part of the donnhead library so we don't own that
it was defined by the same team of people that created the sum method
and the func is supposed to return a double and it takes a measurable as its input parameter
that's what sum calls in order to get the individual double values that needed to be added
together so the caller defined and maintains that interface the dotnet library implementers defined
and implement the interface func that is required we have to implement that so the caller defined it
and the the callee has to implement it so again that inversion that inversion of control
that dependency uh upside down that dependency inversion the lambda that we're passing in m arrow
m calculate area that is the on the fly definition of our func delegate so the application programmer
defined that there are also some interfaces used in net for handling sorted sets sorted collections
in fact the class sorted set requires either an i comparable object type or an i comparer external
uh comparison com comperator in order to assist with the sorting the donet library the caller
owns those two interfaces and the application developer will implement
that interface in order to leverage the more complex algorithm that sorted set provides
so remember interfaces are created and owned by the caller that's a completely different
motivation from inheritance where the inheritance hierarchy is typically owned by the same project
team the abstract base class is owned by the same developers that implement the concrete classes
interfaces are created and owned by the caller the callee depends on and implements the
interface hence the dependency inversion we just flipped that over we decoupled the caller
by removing that dependency and making the caller dependent on a type that they defined
themselves it genericizes the caller because now the caller algorithm can work with a large variety
of data types now in reality oftentimes these interfaces are not explicitly owned by the caller
but instead become part of a third party ownership by genericizing the caller we're no longer
restricting the implementation to a specific class or family of objects like geometric shapes
any object that implements the interface can be used so even things that aren't geometric shapes
like a house can implement i measurable because you can definitely have the area of a house but
a house doesn't fall into the geometry category quite easily so i don't think it would make a good
subclass now as we talked about inheritance throughout this uh quite a bit through this
presentation uh please take a look at our videos on that topic as well because inheritance also
has its place it's a different tool to solve a different problem there's not a better or
worse we use interfaces where we should as we discussed and we use inheritance where we should
as we discussed hey i hope you enjoyed it and like i said make sure you hit that subscribe button
and help support our channel check us out on udemy and also at our website mcculloughassociates.com
you can find the links in the description below i'm chuck mccullough have a nice day [Music]
you