Concern in Managing Platform-Specific Features

53 Views Asked by At

I'm wondering how to address a concern I have. I have a "foo" and multiple conditional platform-specific "foos" (e.g., winFoo, LinuxFoo, or AndroidFoo), where all the platform-specific ones depend on the base "foo." I'm struggling with deciding whether to create a platform-agnostic "foo" and add platform-specific functionality conditionally within it. This approach would offer flexibility, but it means that if I include the platform-specific "foo," it won't have the base "foo" functionality, and attempting to add it could result in an unsightly circular dependency.

Alternatively, I'm considering having the platform-specific "foos" use the base agnostic "foo" and conditionally include them using "if-else" statements in all the places where they are needed. While this approach may work, it's not as flexible as I'd like, and I'm trying to find a balanced solution that offers flexibility while maintaining a smooth and elegant structure. What recommendations do you have?

1

There are 1 best solutions below

5
On

I would do it in following way:

  1. Create common interface for all platform specific modules. Define there classes and methods which all platform specific modules should implement.

  2. In platform specific modules implement logic following defined interface module.

  3. To avoid circular dependency, implement builder module where core "foo" module is used together with platform specific module, but only using interface(you can use template method as design pattern). To avoid if-elses, you can select proper implementation of interface based on some argument.

Forgive me, if I understood something wrong, we can adjust it in comments together.