#Conditional class declaration

1 messages · Page 1 of 1 (latest)

bitter tulip
#

Hey, I'd like to define a class depending on a boolean

Theres two implementation of the same object "Object", implementation A and implementation B.

If boolean is true, Object is defined as implementation A, otherwise it is defined as implementation B
Example of a working python code

class Hello:
    def __init__(self):
        self.greeting = "Hello!"
class Salut:
    def __init__(self):
        self.greeting = "Salut!"
if my_switch:
    Hello = Salut

# Pseudo code equivalent:
if my_switch:
    class Hello:
        def __init__(self):
            self.greeting = "Hello!"
else:
    class Hello:
        def __init__(self):
            self.greeting = "Salut!"

In java, implementation A is public class RegionFile extends Thread, B is public class RegionFile implements AutoCloseable, and the implementation is very different.
I cannot change the code of the project according to this single declaration, so i'd like to change this class according to a boolean.
How can i do that ?

mortal gladeBOT
#

This post has been reserved for your question.

Hey @bitter tulip! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

bitter tulip
#

Conditional class declaration

stone shadow
#

That reminds me of the factory pattern, but not sure I understand what you want to do

bitter tulip
#

I've got a whole project using RegionFile default implementation (which is A).
We've come with a newer implementation of this RegionFile object (B implementation). (which is faster and very different)
We'd like to allow users to choose the implementation they want (boolean).
If that boolean if true, the implementation is A, otherwise its B.

Pseudocode:

legacy_implementation = get_user_config_file() # The user want to use the old implementation ?
if legacy_implementation:
    class RegionFile:
        #... Here come the legacy RegionFile code
else:
    class RegionFile:
        #... Here come the new RegionFile code
stone shadow
#

Can you create an Interface that both implementations A and B implement?

#

Or do they already implement an interface?

bitter tulip
#

One already implement an interface, the other extends an object

stone shadow
bitter tulip
#

I've read about factory pattern with your first comment, do you think this will work ?
Or should i "tryandsee" ?

class LegacyRegionFile implements RegionFile, AutoCloseable{
    // Legacy implementation code
}

class NewRegionFile extends Thread implements RegionFile {
    // New implementation code
}

class RegionFileFactory {
    public static RegionFile getRegionFile(boolean useLegacy) {
        if (useLegacy) {
            return new LegacyRegionFile();
        } else {
            return new NewRegionFile();
        }
    }
}
#

I don't know if i should change other parts of the project, I'd like to keep it the same

#

Meaning that my code don't use getRegionFile at all, so i should recode some parts of it

stone shadow
#

I guess i would try, it looks good so far imho. Maybe you also need to implement AutoCloseable in NewRegionFile?