In this lesson we will be connecting these commands so the drone will move accordingly.
But before we start setting the commands I want to make sure that the drone model won't be invoked multiple
times under the models folder.
We set the drone manager JPY once the drone manager is invoked.
We want all the request to go to the same drone manager object to said this.
We will be using a pattern called Singleton for many of you.
You may not know what Singleton is.
So let's go to Tools and write down what Singleton is right click on tools and choose new.
Click on python file and set the file name to single in this file.
We will be writing some sample codes to create Singleton.
Singleton is a design pattern so you don't have to memorize the entire code.
It's best that you understand how it is written set.
Class singleton.
Usually we would set object between the parentheses but in this case we will be using type.
Let's take a look at what type can do the type.
Class inherits the object in this class.
There is a special method called call by setting type.
We can use this called method to handle the class when it's invoked.
Now let's go back to the code.
Next we set instance which is a class attribute.
Then we use the call method that we just checked earlier.
Change the self to a class start an if statement if the CFS as in class is not in the CLEC instance.
In the beginning there will not be a seamless set.
Let's set to print.
Call so you will see that CLSA is not set in the beginning.
Next we set CFS instance with the CNS as a key and super set to Singleton and CFS followed by the call
method.
Here we set the arcs and K works
the class instance will be set to this instance when the method is invoked.
This class object will be created CFS will be set to the curly braces.
The second time the method is invoked.
The class already exists so that if statement will be a false and will skip printing call.
Lastly we return the CLSA instance.
Even if you didn't understand these codes once we run this code you will get a better understanding
to redefine the class.
We can do so by setting matter class to Singleton defined the init method let's set to print in it by
setting this.
The class t became a singleton.
I'll show an example of what we just created.
Set test to t followed by a parentheses we want to run this code.
So let's go on to the edit configuration button.
Currently the go button is set to the main P Y script.
Let's add a new path.
Click on the plus button and choose python.
I'll set the name to single and set the script path to the one we just made.
Good to be white Tello tools and choose single P Y and click open.
Here you will see that run with python console is set on l object this button so you can see the screen
better if you wish to leave it on.
You could do so as well.
Now hit Okay and let's run this code as we run this code you will see that the text call and in it has
been printed.
If we just do the process once it's hard to understand what is going on so I'll copy and paste the code
for one two three four and five times.
Now let's run the code again.
Again we see the text call and in it has been printed.
Next I will change what we said in the parenthesis from metal.
Class equals Singleton to a regular object as we run this new code.
You will see 5 in its printed.
This means that each test is a different class object but if we use Singleton this text will be call
in a single init printed this means once the class object T is created every time the object is invoked
the process will refer to the same object T created in the beginning so that Singleton.
Now let's quickly go through the script again.
The t gets called and it is set to Singleton type when the call method is invoked and if there is nothing
set in the instance the process will go on to super and create the class which will be set in the instances
for the second time in after the column method is invoked.
CLSA is set in the instances so the first CLSA will be returned no matter how many times the call method
is is invoked the process will return the CFS which was set for the first time.
I hope you understood how the Singleton works it.
Even if you didn't understand each code you don't have to worry.
This is called a design pattern which is also used in other programming languages.
You can always keep this code as a sample and copy paste it when you need it.
Now let's actually copy this code so we can set it in our project go to models folder and right click
choose new and hit python file name the file base and head OK paste the Singleton code you just copied
earlier.
Now go to edit configuration.
We won't be able to use a single button anymore suppress the minus button to delete single hit OK.
And we are done.