Golang Design Patterns – Abstract Factory. More than a Factory

Tram Ho

I. Abstract Factory – Creational Patterns

If we already know what the Factory design pattern is, today I would like to introduce to you a more comprehensive design pattern, which is the Abstract Factory.

The main purpose of this design pattern is to group factories into one large Factory, which can be swapped out and extended more easily. Often in the early stages of planning and defining the functionality that factories can have, it is easier to manipulate and only care about factories and abstract factories than waiting for the definition to be full. enough detail for them. But to implement this pattern, we also need to take care of the things that we will talk about in the next article

II. What does Abstract Factory bring to developers?

This pattern helps us to group related objects as the number of objects can grow beyond our control during the development phase, by creating a unique point where all objects can be created. That statue:

  • Create a new class that đóng gói Factory Methods and returns a common interface for all fatories.
  • Group common factories into a super Factory , also known as Factory of Factories.

III. Practical examples

We will return to the example in the Builder Design Pattern , but for a few more special requirements, we go to a car store to buy a motorbike and a bicycle. This shop has the following vehicles:

  • Bicycles have 2 types: normal bike and sport bike
  • Motorcycles have 2 types: 125cc and 150cc

To implement the Abstract Factory based on the Vehicle object of the previous Factory Method pattern, we need to adhere to the following principles:

  • We always create Vehicle using factory returned from abstract factory
  • A vehicle object is always one of two objects Bicycle (implement Vehicle + Bicycle) or Motorbike (implement interface Vehicle + Motorbike).

IV. Implementation

According to the problem, we need to create the following objects:

  • Vehicle: is an interface that all factory objects need to implement:
    • Bicycle: An interface includes 2 types of bicycles NormalBicycle – normal bike and SportBicycle – sport bike
    • Motorbike: interface of motorcycles including Motor125CC and Motor150CC motorcycles
  • VehicleFactory: is the interface (abstract interface we are interested in) to create factories that implement its methods:
    • BicycleFactory: is a factory that implements VehicleFactory interface and returns vehicle object, this object must implement methods of Vehicle and Bicycle interface.
    • MotorbikeFactory: same as above, only these vehicle objects must implement methods from Vehicle and Motorbike interfaces

Let’s start with Golang ^^

We will create the entities above with separate files. The first is the Vehicle interface in the vehicle.go file

Bicycle interface and MotorBike interface will be created in the file bicyle.go and motorbike.go, respectively as follows:

We have a final interface where all factories must implement that is VehicleFactory in the file vehicle_factory.go, this interface provides a single method (regarded as the function of the car dealership, which is to get the car delivered. for guests )

Now it’s time to implement smaller objects, let’s start with BicycleFactory and MotorbikeFactory, because this section implements the same as the Factory pattern in the previous post, so readers will understand, I won’t talk in detail. With BicycleFactory:

And MotorbikeFactory:

And finally, we need to abstract the above factories, and create a unique point like the beginning of the article I mentioned, serving to create the vehicle. We implement this function in the vehicle_factory.go file created above:

Having all that, we run the program in main.go:

Result:

image.png

V. Conclusion

Through your article, you have understood how to create factories from factories, this design pattern is often used in cross-platform applications and libraries such as GUI libraries. The simplest example is button , a very generic object, and a button factory that helps you create factories on different OS like Microsoft Windows buttons or Mac OS X buttons. We do not need to care about the specific implementation on each platform, operating system, etc. Our job only needs to define a few special actions that a button must have.

The creation of objects, can be approached by many different methods. As in the article Buider Design Pattern, I created Bicycle and MotorBike from a single factory, but in this article, I have expanded it, and we can see how easy it is to customize newly added objects. much easier. However, no design pattern is more optimal than any other design pattern, it all depends on the business and yourself ^^

Thank you for viewing the article.

BECAUSE. References

Go Design Patterns (Mario Castro Contreras)

Share the news now

Source : Viblo