We are now at the fourth video of this section Abstract Factory method that is factory of factories.
In the previous video we learned how to create and use factory methods.
In this video we will reuse the factory we created in the build a design pattern.
We will see the similarities to solve the same problem using a different approach so that you can see
the strengths and weaknesses of each approach.
Finally we are going to create a new factory to create shipment orders.
So here are the acceptance criteria for using the vehicle objects factory method.
We must retrieve a vehicle object using a factory returned by the Abstract Factory.
The vehicle must be a concrete implementation of a motorbike or a car that implements both interfaces
that is vehicle and car or vehicle and motorbike.
Let us take the unit test.
This is going to be a long example so pay attention please.
We will have these entities which you see on your screen vehicle which is the interface that all objects
in our factories must implement.
Next is motorbike an interface for motorbikes of the types sport one seat and Cruze for two seats.
The third one is car an interface for cars of types luxury with autos and family with five doors will
also have vehicle factory an interface or rather the Abstract Factory to retrieve factories that implement
the vehicle factory method.
Then comes the motorbike factory which is a factory that implements the vehicle factory interface to
return vehicle that implements the vehicle and motorbike interfaces and the last one is car factory.
Another factory that implements the vehicle factory interface to return vehicles that implement the
vehicle and car interfaces for clarity.
We are going to separate each entity into a different file.
We will start with the vehicle interface which will be in the vehicle dot go file I'll show you the
one I have created.
So within the go design patterns package I have created another package and named it abstract underscore
factory.
And here are all those files which will be using and you also need to create these files.
The first one is the vehicle Dot.
Go file let me open this.
It should have the get wheels and get seats in the vehicle interface.
Now the car and motorbike interfaces will be in the car.
Dot go and motorbike dot go files respectively.
Let me show you both.
So this is the car dot go interface.
And this is the motorbike.
Dot go interface.
We have one last interface the one that each factory must implement.
This is in the vehicle underscore factory.
Dot go file and here's the interface.
We add to it.
So now we are going to declare the car factory.
It must implement the vehicle factory interface defined previously to return vehicles instances.
We do this in the car underscore.
Factory don't go file.
We have defined two types of cars luxury and family the car factory will have to return cars that implement
the car and vehicle interfaces.
So we need two concrete implementations.
We have added these implementations in the luxury underscore car don't go.
And the second one is in the family underscore.
Car doors go file that's all for cars.
Now we need the motorbike factory which like the car factory must implement the vehicle factory interface.
So is the motorbike underscored battery not go file for the motorbike factory.
We have also defined two types of motorbikes using the concept keywords which are sport motorbike type
and Cruze motorbike type.
We will switch over to the argument in the build method to know which type shall be returned.
Let's write the two concrete motorbikes.
That is the sports motorbike.
Don't go and the crew's motorbike don't go to finish.
We need the abstract factory itself which we put in the previously created vehicle underscore factory
not go file.
We're going to write enough tests to make a reliable check as the scope of this course does not cover
100 percent of the statements.
It will be a good exercise for you to finish these tests.
So we first create a file named vehicle underscore.
Factory underscore test not go where we add a motorbike factory test.
We use the package method yet vehicle factory to retrieve a motorbike factory passing the motorbike
factory type in the parameters and check if we get any error then already with the motorbike factory
we ask for a vehicle of the type sport motorbike type and check for errors again with the return vehicle
we can ask for methods and the vehicle interface get wheels and get seats.
We know that it is a motorbike but we cannot ask for the type of motorbike without using the type assertion
we use the type assertion on the vehicle to retrieve the motorbike that the motorbike vehicle represents
in the code line.
Sport bike bound equals Motorbike Vehicle dot motorbike and we must check that the type we have received
is correct.
Finally now we have a motorbike instance.
We can ask for the bike type by using the get motorbike type method.
Now we write a test that checks the car factory in the same manner.
Again we use the get vehicle factory method to retrieve a car factory by using the car factory type
in the parameters.
With this factory we want to car the luxury type so that it returns a vehicle instance.
We again do the type assertion to point to a car instance so that we can ask for the number of doors
using the get to method.
Now move on to the terminal and let's run the unit tests done.
It can't recognize any factory as their implementation is still not done.
Now let's move on to implementation.
The implementation of every factory is already done for the sake of brevity.
They are very similar to the factory method with the only difference being that in the factory method
we don't use an instance of the factory method because we use the package functions directly.
Let me show you the implementation of the vehicle factory is what you need to add to the code file.
Let us save the file and go back to terminal like in any factory.
We switched between the factory possibilities to return the one that was demanded as we have already
implemented all concrete vehicles.
The tests must run too.
So let us run the test now.
As you can see all of them passed.
Take a close look and note that we have used the hyphen cover flag when running the tests to return
a coverage percentage of the package fifty eight point three percent.
This tells us that fifty eight point three percent of the lines are covered by the tests we have written
but forty one point seven percent are still not under the tests.
This is because we haven't covered the crews motorbike and the family car with the tests.
If you write those tests the results should rise to around seventy point eight percent type assertion
is also known as casting.
In other languages when you have an interface instance which is essentially a pointer to a struct you
just have access to the interface methods with type assertion.
You can tell the compiler the type of the pointed struct so you can access the entire struct fields
and methods cool.
In this video we have learned how to write a factory of factories that provides us with a very generic
Object of vehicle type in the next video we'll be learning about prototype design pattern.