Shared function in an interface

olsonpm

Well-known member
Joined
Aug 24, 2010
Messages
46
Programming Experience
Beginner
My base problem is that I would like to have an interface define a shared function, although through vb (the CLR from what i've read) does not allow for it. After reading, I don't think there is a single solution to this problem, so let me give you some context.

I am trying to implement MVC with a program that interprets logs, and spits it out in a format that will allow the users to possibly debug, or at least know more about what happened in case something goes wrong. My view handles the gui, which will soon change to a web interface. My controller handles the input, and then my model handles the raw data. I then have seperate classes handle the business logic, since they might change by themselves later on - this is where I would like to enforce the shared function. One of the functions I need from the business logic is to interpret any given line of a log. I could name this class 'LogConverter'. Since it has no state, I would like it to have the method 'public shared function ConvertLog(byval log as string) as string'. Later, I was assuming someone might want to implement the converter differently, so I figured this method would be defined and implemented through ILogConverter - but obviously I can't do this.

according to this read Shared functions with interface, a singleton pattern is the way to go. However, the singleton in this case seems as much a hack as simply leaving it to the instance itself - which was going to be my solution. Is there any clean way of accomplishing my goal?

thank you,
Phil
 

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,052
Programming Experience
10+
static functions in an interface are not logical - an interface ensures that an instance of a particular object can be treated as a certain type, but statics have no instance - there's only one morph of a static, so it cannot be declared as an interface because it cannot be polymorphed

Why don't you just define a base class (rather than an interface) that different converters can derive from, and provide a default implementation of your LogConverter code. Plugins can override the method if they choose or leave it alone
 
Top Bottom