I'm new to Python and need some advice implementing the scenario below.
I have two classes for managing domains at two different registrars. Both have the same interface, e.g.
class RegistrarA:
__init__(self, domain):
self.domain = domain
def lookup(self):
...
def register(self, info):
...
and
class RegistrarB:
__init__(self, domain):
self.domain = domain
def lookup(self):
...
def register(self, info):
...
I would like to create a Domain class that, given a domain name, loads the correct registrar class based on the extension, e.g.
com = Domain('test.com') #load RegistrarA
com.lookup()
biz = Domain('test.biz') #load RegistrarB
biz.lookup()
I know this can be accomplished using a factory function (see below), but is this the best way of doing it or is there a better way using OOP features?
def factory(domain):
if ...:
return RegistrarA(domain)
else:
return RegistrarB(domain)