Let's not implement couple of manager classes if you recall manager classes include business logic which 2
would include code to instantiate entities. 3
So they'll begin creating manager classes by writing methods that would instantiate entities that we 4
created. 5
The methods we laid will be used later from the day dustoff class which we rewrite later. 6
The manager classes we will write will be named as User Manager and bookmark manager. 7
USE IT manager will good business logic related to users. 8
Well the plant manager billing good business logic related to bookmarks. 9
One main thing to note about Manager classes is that they are stateless. 10
That is they don't mean in any instance variables so you don't need to create an instance of them and 11
instead can maintain them as static utility classes. 12
However for managers it's recommended to not use static utility classes instead of just one single instance 13
of each of the manager classes. 14
The reason for this is in some projects manager classes implement an interface and indorsing not yours 15
. 16
You cannot use tactic utility classes as static matters in such classes cannot override or hide instance 17
methods in an interface. 18
So the recommended method is to create a single instance of the manager class and this is achieved by 19
using a design pattern called Singleton partan a singleton pattern is simply a design pattern that would 20
ensure that exactly one object is created or no more. 21
And it also provides a global point to access the object store manager classes are basically singletons 22
. 23
So let's get started and see how this is done. 24
Krust let's create a new package for our managers. 25
So let's just right click here. 26
So it would be calm semantics. 27
There really are dock managers 28
let's begin with creating the user manager Plus 29
just a short note on the naming convention here. 30
Fund manager is typically the last names would end with the manager. 31
Some programmers also use the term service instead of manager as managers to define the service here 32
. 33
However it's just a matter of choice and in my opinion it's really not important whether we use the 34
term manager ourselves to end the class name only important thing to do as follow what your team is 35
doing. 36
If your team is using manager then go with manager. 37
If your team is using service then go with service. 38
OK first let's implement a singleton related code as part of implementing Singleton we to have a private 39
constructor. 40
So let's create a private constructor. 41
We know that having a private constructor implies that one cannot instantiate the class so the class 42
of user manager cannot create an instance of User Manager. 43
So that's what we want for a singleton. 44
We don't want our clients to create instances freely. 45
So for Singleton we said that only one object needs to be created. 46
Now since it's a private constructor we cannot create an instance of it. 47
Now how do you create that single instance for a singleton. 48
So for that only way to do it as since it's a private constructor the single object has to be created 49
from within this class. 50
So that's done from a method and we can. 51
The convention is to collect as get instance. 52
Now this method has to return an object of type user manager. 53
OK so that's the return type. 54
Now that needs to be public since one cannot create an instance of user manager. 55
It needs to be a static method. 56
So this is the method that would return the single instance of user manager. 57
OK so that and Ipperwash the global point of access. 58
Now you can create an instance here but let's do it here. 59
So let's create a pirate variable that type of that variable has to be user manager and it's call the 60
variable as instance. 61
And let's create the instance here. 62
Sorry 63
Now since this is an instance variable we cannot access it from here so is if we want to access it from 64
a static method. 65
It needs to be static too. 66
So that's how we create an instance or we can actually create it here also. 67
But we are creating it here in the declaration itself. 68
So over here all we do as we're done instance. 69
So now what happens is whenever the client code says User Manager Daag get instance on the very first 70
time it does that the JVM actually loads the class Lord of Lords. 71
Does the user manager class into memory at the same time it will also exit your desk. 72
The first statement and an instance will be created. 73
So that happens when the when this method is invoked for the very first time. 74
So by the time the method is invoked the instance variable is already initialized with the object. 75
OK and we are just returning the object and this is a good way of creating the singleton. 76
You know there are a couple of other minor variations also for creating singletons. 77
I mean once you are done with the course you can check out Chapter 5 and head first design patterns 78
book. 79
It's a very good book and you can check out the chapter 5 which is on specif specifically for us on 80
singletons and it explains these other minor variations also. 81
So but the approach we followed is good and you can follow this approach. 82
So that's how you create a singleton. 83
Next let's create a method for instantiating user Blake user. 84
Let's call the methods. 85
Create users since we want to create an user instance. 86
So that's how all the instance variables. 87
Let's click on this. 88
So these are the variables ID email password so id is long 89
and user type. 90
So this method will be invoked later from the dust or class 91
of. 92
So what we have. 93
And so let's go ahead and say user user 94
now a user 95
id 96
say trust me 97
you think we missed the password here or said email. 98
So Id email password first name last name 99
and gender and user name. 100
So we have all of them sent here. 101
So let's just say written user so that the User Manager class next lets create a bookmark manager and 102
managers 103
. 104
So once again thats create a singleton 105
thats the private constructor 106
its the public access matter private static. 107
So just just returns the instance. 108
Ok so thats the singleton code for creating Singleton host lets create the movie method to create a 109
movie. 110
Just open the movie Close but Mark has some valuable Speidel and profile you are in a 111
movie class as a release. 112
Your cast director's 113
gender and I am beby rating 114
. 115
So let's just go ahead and create the movie instance 116
in that same boat movie we movie 117
you know said the loose 118
release either 119
so I guess that's about it. 120
So it's a certain movie. 121
So that's how you would create a movie. 122
Similarly we need to create methods for creating both book and also grappling instances. 123
So in the interest of time I'm not going to the monsters creating those matters as that pretty identical 124
. 125
So just go ahead and create them yourself. 126
I will also do it offline and we'll come back to just show you the quarter. 127
So here is the Create book method and it's creating a book instance and setting all the feels like the 128
ID title publication your publisher authors genré iron Amazon rating. 129
OK so that's the book instance and here is the method create weblink and we are creating an instance 130
of a blank on one side inserting ID tightly and the two feel specific to Webling which are you out host 131
. 132
So those are the two manager classes. 133
Later we'll be adding more code to these two classes.