Yes, exactly, but there is another type of operational pattern, which is very similar with what we've
seen previously with the factory captain.
They have the same advantages and disadvantages, meaning that they hide very well the details of the
implementation for creating an object, but also they are very difficult to refactor your current code
base.
And so basically, you need to think about having a factory pattern from the beginning, from the beginning,
from the design phase.
And a big difference is the fact that instead of having a single object factory, which will create
all the objects for you, you have a super factory which will create a factory that will create finally
the object that you're interested in.
This is the diagram of the abstract factory pattern, so you can see we have here the abstract factory,
which is an abstract class or an interface which has a computer method.
This should be in a segmented and your classes that will extend this will need to implement this this
method.
The next phase is to have a computer factory, a generic factory, which will create a computer like
we've seen before.
But also we can have different types of factories like portable computer factory, for example, which
will create a specific type of computer that will have that will share this trait, this this feature
being being a portable one and something independent from from this structure here.
It's the factory producer which will create the factory that we are interested in, depending on the
object that you need to create.
You might need to create a portable computer factory or another type of factory.
But sticking with this example, we will see how we create an object using this design pattern.
So let's take a look in the code.
Let's start with the abstract glass here, the abstract factory glass.
As you can see, this is an abstract method which will be implemented in the concrete implementation
of this of this glass, which will return the computer.
And the method will accept parameter of type string, which will contain basically the computer type
that we need to create in our factory.
We then have two factories that we created.
So we have a computer factory like we've seen before.
It has the same the same implementation here.
But also we have a portable factory which will extend the factory and will have only the means to create
some portable computers like a laptop or a phone, for example.
So the third one, the third type of computer or other other types that are not portable will be left
out of this implementation and something that is new for specific for this design.
But is the factory producer and we can see here that it produces a factory that we need to that we need
in our code.
So basically, if we need a portable one, we will just supply this to this method.
We will just supply the billion portable set to true.
And as you can see, it will return a portable computer in this case or if this will be sent to force,
it will return a basic computer factory.
The rest of the code is still the same.
So we have an interface of type computer that has this compute method here and for each type of computer,
the computer method will return a different line in the console, as you can see here.
All right, so let's take a look at this in action, so we have this at class, there are the main method
here which will create an abstract factory of this type.
And as you can see, we we are just calling the get factory method from this factory producer class,
which is a static method.
So we just we don't need to have a factory producer object created beforehand and we supply this portable
this portable boolean value to false.
And here when we create a new computer, we just do computer, factory, computer, and we will select
an old portable one.
And here we should have the line that is specific to this implementation printed out in the console.
So let's just test that.
So, as we expected, we have this line correctly, I'll put it here.
So this is the abstract factory in a nutshell.
Of course, this is overly simplified example, but you may have seen that it's a bit complex.
So it's actually very, very difficult to refactor your code, to adapt, to adapt it to an abstract,
abstract factory or even a factory pattern, because you need to create all these classes beforehand
and you need to take into account a lot of a lot of aspects of object creation.
So it's better to think of designing your code from the beginning with an abstract factory or a factory
and address all the changes from the beginning.
That's it for for this for this type of of pattern.
Join me in the next story on where we are going to discuss about the Singleton pattern.