[Java] Advantages of generating Abstract Factory pattern with enum

2 minute read

To explain the AbstractFactory pattern, you often see an example of passing a character string as a runtime argument or if-else judgment with a flag.

In personal development, you may not care because you know everything. However, if you provide it to other teams as a module in team development, How can a team provided with a Factory know which Factory they can create?

I think you can either check the code yourself or check with the code manager on the Factory side.

Whether the person in charge on the Factory side is for the person in charge of the code of the client, That’s because it’s a pain to answer each time I come to ask me Know that you can clearly show what can be generated by setting it to Enum.

Factroy generation method

Code example for passing class name as a string

<img alt=’’ class=’alignnone size-full wp-image-2115’src=’https://loglog.xyz/wp-content/uploads/2020/07/img_5f0ad704593e5.png’ /> I believe this has the following issues:

  • Is it possible to say that the specified character strings are not different? ・Is it possible that I am not in the wrong case? ・Isn’t it like calling a Factory that doesn’t exist in the first place?

Example of judging Factory to be generated by flag

<img alt=’’ class=’alignnone size-full wp-image-2113’src=’https://loglog.xyz/wp-content/uploads/2020/07/img_5f0ad6e43d369.png’ /> I think this too has the same problem as with strings. Also, under this judgment condition, FactroyY is generated except for 0, Includes the possibility of FactoryY being generated unexpectedly.

Example of using # Enum If you implement it using Enum, you will not explicitly show the generated Factroy and create a non-existing Factory. *The following code is my own modification of the TECH SCORE Abstract Factory pattern. Please include that it has nothing to do with TECH SCORE. 8.AbstractFactory pattern

Use.java (client)

It is generated by specifying Recipe (Factory class) with HotpotType which is an Enum. <img alt=’’ class=’alignnone size-full wp-image-2127’src=’https://loglog.xyz/wp-content/uploads/2020/07/img_5f0ae54ab7fd2.png’ />

HotpotType.java(Enum that generates Factory)

Define a Factory that can be created here. If Sukiyaki is specified, Sukiyaki Recipe will be generated. <img alt=’’ class=’alignnone size-full wp-image-2126’src=’https://loglog.xyz/wp-content/uploads/2020/07/img_5f0ae52e55a92.png’ />

Recipe.java(Factory class)

Factory generation is delegated to HotpotType, so you can concentrate on abstract implementation only. <img alt=’’ class=’alignnone size-full wp-image-2125’src=’https://loglog.xyz/wp-content/uploads/2020/07/img_5f0ae50c08524.png’ />

Advantages of generating with # Enum

  • Only Factroy existing in Enum can be generated, so incorrect Factory will not be generated.
  • In the case of team development, the code person on the client side does not need to know what the Factory class that can be generated is. If the Factory class is insufficient, you can just ask the programmer in charge of the Factory side. ・Easy to add Factory. It is not necessary to modify the if statement because only the required Factory class is added to Enum.

If you are writing the program by yourself, you don’t have to think about this, **If you work as a team and are responsible for dividing modules By thinking in this way, it is possible to increase the easiness of change and facilitate modularization. **

Class diagram

<img alt=’’ class=’alignnone size-full wp-image-2131’src=’https://loglog.xyz/wp-content/uploads/2020/07/img_5f0b9b53a2b20.png’ />


https://github.com/TakumiKondo/designpattern /tree/master/src/abstractfactory/hotpot