Re: Problem with method overriding from base class



On 31 mar, 11:05, <Dominique.Holzwa...@xxxxxxxxxxxxxx> wrote:
Hello everyone

I have defined some sort of 'interface class' and a factory function that creates instance objects of specific classes, which implement that interface:

Interface definition:
***************************************************************************************
import GUI.webGUI as webGUI

class EditInterface(webGUI.WebGUI):
def addEntry(self, *p):
raise 'EditInterface.addEntry(): Interface must not be called directly'

You want:
raise NotImplementedError('EditInterface.addEntry():
Interface must not be called directly')

And unless you have some pretty good reason to do so (ie: template
methods in EditInterface depending on these methods), you don't even
want to bother with all this - just document which methods must be
implemented, and let Python raise an AttributeError if they are not.

(snip)


Factory:
***************************************************************************************
def factory(type, *p):
if type == common.databaseEntryTypes[0]:
return module1.Class1(*p);
elif type == common.databaseEntryTypes[1]:
return module2.Class2(*p);
elif type == common.databaseEntryTypes[2]:
return module3.Class3(*p);
elif type == common.databaseEntryTypes[3]:
return module4.Class4(*p);

The whole point of polymorphic dispatch in OO is to avoid this kind of
mess. What's wrong with instanciating classes directly ? NB : in
Python, classes are objects too, so you can pass them around as
needed. Also, instanciation is done thu a call to the class object -
which makes it just the same as a function call. IOW, you just don't
need a clumsy dedicated factory function just to make sure the client
code is not too tightly coupled to the exact implementation.


Implementing Class1:
***************************************************************************************
import editInterface

class Class1(editInterface.EditInterface):

def __init__(self, product, database):
# do something here ...

def showEntry(self, entry, statustext):
# do something here as well, return some string...
***************************************************************************************

Now, when I want to create an Instance of Class1 I do:

myClass1Instance = factory.factory(common.databaseEntryTypes[1], 'Name', databaseObj )

Which seems to work fine according to the debugger. But when I do next:

msg = myClass1Instance.show(firstEntry, '')

Then the show() method of the class 'EditInterface' is called instead of the show() method of the class 'Class1' !!

Reread your code : class Class1 have no 'show' method !-)


.



Relevant Pages

  • Re: Access base window class (FromHandlePermanent)
    ... support for versioning and compiler independence. ... The DLL exposes a pure interface, which the client can access via an exported "C" factory function. ...
    (microsoft.public.vc.mfc)
  • RE: Missing method in TLB
    ... Recompiling the project doesn't solve the problem as the repro vb6 shows. ... // typelib filename: Project1.dll ... interface _Class1: IDispatch { ...
    (microsoft.public.win32.programmer.ole)
  • Re: Separating implementation and interface: HOW?
    ... We try to separate implementation and interface defintions, ... public class Class1: IClass1 { ... 'central 'factory'. ... static field of a class (and therefore doesn't call the constructor) unless ...
    (microsoft.public.dotnet.languages.csharp)
  • Separating implementation and interface: HOW?
    ... We try to separate implementation and interface defintions, ... public class Class1: IClass1 { ... 'central 'factory'. ... static field of a class (and therefore doesn't call the constructor) unless ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How can I hide base class members that are public
    ... Can you not just make Class1 an interface and make those methods that you ... > the features of the base class. ... I basically want it to work like using interfaces, you know, expose ...
    (microsoft.public.dotnet.languages.csharp)