In C++ I would define a public interface authenticator.h, defining the Authenticator interface. Then, I would define authenticator_factory.h with two methods: CreateLocalAuthenticator() and CreateRemoteAuthenticator(). Both would return the Authenticator interface.
Then in the private authenticator_factory.cc file, I would define three classes: LocalAuthImpl, RemoteAuthImpl, and SharedAuthImpl. SharedAuthImpl would be a member variable if the former two with sets of common logic. C++ also allows freestanding functions for miscellaneous pieces of logic.
Finally, in main(), I would add an if/else branch that calls either CreateLocalAuthenticator() or CreateRemoteAuthenticator() depending on some config value. Then I pipe the returned Authenticator interface throughout the code.
This way, I can switch from remote and local with just a change in my config. The only boilerplate code is the if/else branch in main, all of the rest of the system only sees the interface. And free functions and the SharedAuthImpl in my .cc allows me to shared important between the two.
I think this is why Java has a stereotype for leading to unclean code. I can think of ways to implement the above in Java, just not anywhere near as clean. Take this with a huge grain of salt though because I’m obviously not a Java programmer.
I have a background in C/C++ and use mainly Java these days. I would argue my very hastily hashed out Java implementation is cleaner as the application code doesn't even have to have an if/else construct deciding which authenticator type to use and the application is entirely unaware of which type it is using.
The equivalent of my above solution in C++ would be the application dynamically linking to an external DLL/so library which implements standard functions. Both local and remote authenticator projects would produce binaries with the same name, then it would only be a matter of which library is present at runtime that determines whether remote or local is used.
1
u/throwaway1847384728 Jan 31 '23
In C++ I would define a public interface authenticator.h, defining the Authenticator interface. Then, I would define authenticator_factory.h with two methods: CreateLocalAuthenticator() and CreateRemoteAuthenticator(). Both would return the Authenticator interface.
Then in the private authenticator_factory.cc file, I would define three classes: LocalAuthImpl, RemoteAuthImpl, and SharedAuthImpl. SharedAuthImpl would be a member variable if the former two with sets of common logic. C++ also allows freestanding functions for miscellaneous pieces of logic.
Finally, in main(), I would add an if/else branch that calls either CreateLocalAuthenticator() or CreateRemoteAuthenticator() depending on some config value. Then I pipe the returned Authenticator interface throughout the code.
This way, I can switch from remote and local with just a change in my config. The only boilerplate code is the if/else branch in main, all of the rest of the system only sees the interface. And free functions and the SharedAuthImpl in my .cc allows me to shared important between the two.
I think this is why Java has a stereotype for leading to unclean code. I can think of ways to implement the above in Java, just not anywhere near as clean. Take this with a huge grain of salt though because I’m obviously not a Java programmer.