开发者

help with interfaces and abstract classes

I'm recently getting a bit confused with interfaces and abstract classes and I feel I dont fully grasp it like I thought I did. I think I'm using them incorrectly. I'll describe what I'm doing at the moment, the problem I have faced, and then hopefully it be clear what I'm doing wrong if anything.

I wanted to write some classes that do some parsing of xml. I have different user types that have different parsing requirements.

My logic went as follows.

All parsers share a "parse" function in common and must have at least this function so I made an Interface with this function defined named IParse;

I start out with 2 user types, user type A and user type B. User type A & B share some basic functions but user type B has slightly more functions than A so I put the functions to parse what they share in an abstract class that both will extend called "ParseBase".

So now I have

// Interface

    public interface IParser
     {
      function parse(xml:XML):void;
     }




// Base Class

public class ParseBase()

{
  public function getbasicdata():void{}

public function getmorebasicdata():void{}

}





//User type A

public class userTypeA extends ParseBase implement IParse

{
    public function parse(xml:XML):void
{
       getbasicdata()

       getmorebasicdata()

}


}

//user type B

public class userTypeB extends ParseBase implement IParse

{
    public function parse(xml:XML):void
{
       getbasicdata()

       getmorebasicdata()

}

   public function extraFunctionForB():void
{

}

  public function anotherExtraFunctionForB():void
{

}


}

The problem I have come up against now which leads me believe that I'm doing something wrong is as follows.

Lets say I want to add another function UserTypeB. I go and write a new public function in that class. Then In my implementation I use a switch to check what Usertype 开发者_开发技巧to create.

Var userParser:IParser

if(a)

{
userParser= new userTypeA(); 
}else if(b)

{
userParser= new userTypeB();
}

If i then try to access that new function I can't see it in my code hinting. The only function names I see are the functions defined in the interface.

What am I doing wrong?


You declare the new function only in userTypeB, not in IParser. Thus it is not visible via IParser's interface. Since userParser is declared as an IParser, you can't directly access userTypeB's functions via it - you need to either downcast it to userTypeB, or add the new function to IParser to achieve that.

Of course, adding a function to IParser only makes sense if that function is meaningful for all parsers, not only for userTypeB. This is a design question, which IMO can't be reasonably answered without knowing a lot more about your app. One thing you can do though, is to unite IParser and BaseParser - IMO you don't need both. You can simply define the public interface and some default implementation in a single abstract class.

Oher than that, this has nothing to do with abstract classes - consider rephrasing the title. Btw in the code you show, ParseBase does not seem to be abstract.


In order to access functions for a specific sub-type (UserTypeB, for example) you need the variable to be of that type (requires explicit casting).

The use of interfaces and abstract classes is useful when you only require the methods defined in the interface. If you build the interface correctly, this should be most of the time.


As Peter Torok says (+1), the IParser declares just one function parse(xml). When you create a variable userParser of type IParser, you will be allowed to call ony the parse() method. In order to call a function defined in the subtype, you will have to explicitly cast it into that subtype.

In that case IMO your should rethink the way you have designed your parsers, an example would be to put a declaration in your IParser (Good if you make this abstract and have common base functionality in here) that allow subtypes (parsers) to do some customization before and after parsing.

You can also have a separate BaseParser abstract type that implemnts the IParser interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜