>> There's an old coworker of mine who is,
I'm assuming still to this day,
really fond of saying,
we're not launching rockets here,
and that phrase has stuck with me for my entire career.
The point that he was trying to
make when he would say that phrase,
"We're not launching rockets here,"
is that whatever it is that we're doing,
it's really not necessarily all that complex.
We're not necessarily breaking new ground,
we're not launching rockets,
and as a result,
whatever it is that we're doing,
whatever problems it is that we're trying to solve,
chances are somebody else has already been there and
they've already built some form of a solution.
One great example of that is when you're
dealing with a CRUD web application.
CRUD of course, stemming for Create,
Retrieve, Update, and Delete.
This is really a line of business application where I've got data,
I want to be able to manipulate it.
I want to be able to search for different items.
I want to be able to filter and like do all of those basic things.
I want to create records, I want to get them back.
I want to be able to update them.
I want to be able to delete them.
Basically, every Web Developer at some point
has created some form of
an application that's going to be a CRUD application.
Again, create, retrieve, update, delete.
So rather than every time
we need to create one of those types of applications,
go off and start setting up all of the code on our own.
Start setting up all of the codes to do paging,
which is a real challenge.
Let's instead use code that somebody else has already created,
and one framework that has done exactly that,
and a framework that I'm a huge fan
of is this little thing called Django.
Django is going to help make its magic happen.
Help allow me to integrate with it by using
this little thing called multiple inheritance or mixins.
I bet you're wondering when I was finally going to get to
the text that was behind me. Here we are.
So what I'm really trying to do
here is I'm trying to set the stage for
when and why it is that you
might decide to use something like this,
and quite frequently it's because we want to be
able to clip into a framework.
Well, let's define some terms to start with.
Let's go ahead and talk a little bit about multiple inheritance.
Multiple inheritance as the name implies,
means that I'm able to inherit from multiple classes.
Now, you're also going to notice that
some people will call this mixins
for reasons that we'll start to see a little bit later.
Now, one thing to note about
multiple inheritance is that it's actually a little controversial.
You're going to notice that a lot of programming languages,
Java, C-Sharp, don't support multiple inheritance.
Yes, they'll allow you to implement multiple interfaces.
It's a different conversation altogether,
but they only allow you to inherit from one class,
and the reason that they've made
that decision is because well, quite frankly,
multiple inheritance can get messy really quick,
that a lot of Developers don't necessarily use it real
well because it can get complicated.
So why have it then?
Where might we use this?
Well, it's useful in those exact types of scenarios
where what I'm looking to
do is to either take advantage of a framework.
Again, I mentioned Django at the outset,
or I'm trying to streamline
repetitious operations or repetitive operations
that I'm trying to do the same thing over and over again,
you just see where that's going with that joke.
All right. So let's highlight then this scenario.
So here's what I want to be able to create.
I want to create my own framework where I'm
going to give you the ability to connect to a database.
All that you need to do is give me
the name of the database that you want to connect to.
In addition, I also want to give you the ability to quickly
log out to the screen
or to a file system behind the scenes or wherever it is.
But I don't want you to worry about any of that.
I don't want you to have to worry
about how to write out to a file.
Or maybe we've decided
to update things that are we're going
to write out to the event log.
I don't want you to worry about that.
I want to give you the ability
instead to focus in on the code that
you're creating and not worry about the nitty-gritty.
I don't want you to worry about
the exact machinations of
how we're going to connect out to the database.
You just tell me where to connect,
we'll take care of the rest for you.
I don't want you to worry about how we're going to do logging.
Just tell me what it is that you want to log.
I'll take care of the rest for you,
and this is where something like mixins
or multiple inheritance can really shine.
Because now I can take advantage of each one of
these depending on what it is that I'm doing.
So maybe I've got some operation over here where all
that I want to be able to do is just write out to the logfile.
No database required.
Maybe I've got something going on where I want
to be able to write out to a database,
but no logging is required.
This is where mixins come into play.
So let's break all of this down with our code here.
So you're going to notice that I'm
going to create two classes here.
One that's called Loggable
and the other one that's called Connection.
With loggable, what I'm going to look
for is I'm going to look for,
in this case, a title,
and then I'm going to go ahead and login.
So because of course,
I'm trying to keep things a little bit straight forward,
I'm going to just go ahead and print it out to the screen.
So I will print out whatever it is that's inside of the title.
The other thing that I want you to notice is that I'm
going to set up a connection here,
and again, just to try and simulate
how it is that we're connecting out,
I'm just going to print out, "Hey,
we're connecting out to whatever the name of the server
is," and then right here,
I'm going to go ahead and store the name of the server.
So there's our loggable and there is our connection.
Now, the next question then becomes, well,
if I'm going to keep building this framework,
how am I going to put this to action?
Here's my really fancy framework.
So what my framework is going to do is it's going
to take whatever item it is that you give to it,
and then I'm going to look to say,
hey, did you inherit from connection?
That's what his instance is going to tell me,
and if you did now I know that I can call connect.
Then I'm going to go ahead and check to see,
did you inherit from loggable?
That's what his instance again is going to do for me,
and if you did, then I can go ahead and call log.
It's such a wonderful toy,
little obscure reference for my own entertainment there.
Now, I can go ahead and create a class here.
So I'm going to create a database class,
and my database class is going to inherit from
Connection and it's going to inherit from Loggable,
and now all I need to do to
enable the appropriate functionality is go
ahead and set up those two little properties right there,
and then if I wanted to do anything else inside of this class,
I'm absolutely able to do that.
The key thing here is the fact that if I want to enable logging,
I just simply inherit from
Loggable and then set up whatever my title is.
If I want to enable connecting to a database,
I simply inherit from connection and then set up
whatever it is that I need to set up, and then later on,
if I decide I'm going to create something
else that's not going to be loggable,
well, then just don't inherit from loggable.
That's really it. So now,
I create an instance of that class.
I call my super-duper high-end framework,
and eventually what we would
notice is the printout on the screen that it's going
to connect to the database and it's going to log our message.
Now, I'm going to get to the code in just a moment here,
but I want to close by saying this.
The vast majority of the time
that you're going to use multiple inheritance
or mixins is a when you're using an existing framework.
There's this great little thing called the code smell,
where it's really trying
to indicate the fact that something might not be right.
Something just smells wrong, something smells unpleasant,
and multiple inheritance can quite frequently be a code smell.
If you're setting up your own environment,
your own little application,
and you've got multiple inheritance going on,
I'm not going to say that you're doing the wrong thing,
but what I am going to say is that's frequently a code smell.
Chances are you're not pointed in the right direction.
Now, if you are creating a re-useable framework,
then absolutely this could be
one of the solutions that you decide to choose from.
Again, you'll see this in a lot of frameworks.
Django being my favorite.
I love Django, Django is near and dear to my heart.
So you might be using this with some additional frameworks.
You might be creating
your own framework where you're
going to simulate something like that.
But typically for most
of the applications that you're going to be using,
setting up a multiple inheritance hierarchy,
probably not what you're looking for. Code smell.
Using a framework that uses multiple inheritance,
that's something that is very common.
All that being said,
let's go ahead and take a look at our code demo.