Let's start with the interface that is basically the base of this this pattern, so we can see that
we have this computer interface with a single method to compute.
And the three objects, the three concrete objects that will implement this interface will implement
this method as well, of course, but the implementation will be slightly different for each and every
one.
So for the first one, for the laptop class, we will print laptop computers for phone, phone computers
and for smart TV.
You will have this result in the console alongside.
We have the computer factory, which will have basically get the computer method that will return the
type of computer that we want to create.
So if we apply in this method, a laptop as well as the computer that we want to create, then this
statement will be will be executed and a new chapter will be returned and so on and so forth for the
rest of the objects that we can create in this factory class, in this computer factory class, if we
will supply an unknown parameter, the factory will return.
No, of course you will.
You can change this.
You can change its behavior.
We can put switchgear statements.
You can put whatever you want.
But also you can treat this case as well.
We can have a default computer that you may want to return.
But for the sake of simplicity, let's stick with with this and in this class, do you see the factory
pattern in action?
You need to create first a computer factory, of course, and then we can create this type of object,
but we won't initialize it because it's an interface.
And we can we can invoke the computer factory object to get a letter for us.
And now the computer object will be equal to a laptop.
Then we do the same with different objects, of course, for the rest of the computers here for phone
and smart TV.
So basically you will see in the console laptop computers, phone computers, multiple countries.
But let's see this in action.
Actually, let's let's execute this code.
And you can see we have the expected output here.
So this is how it works.
It's a bit it can be a bit daunting to create a factory pattern because, of course, this is an overly
simplified example.
But if you will have a much more complex structure of objects, then it will be a bit difficult.
And also you will need to think about this from the beginning.
So this is actually a decision that needs to be made before you start coding.
So that's it for the factory.
Better join me in the next iteration where we are going to discuss about Singleton pattern.