Why use interfaces?

tooltiperror

Super Moderator
Reaction score
232
Just wondering, why should we use interfaces? I don't get their value.
 
Function Interfaces or interfaces?

JASS:
library testLib

//method interface example

    interface Comparable
        method operator > takes Comparable c returns boolean defaults false
    endinterface

    struct String extends Comparable
        string which
        method operator > takes Comparable c returns boolean
            return StringLength(this.which) > StringLength(String(c).which) //can't quite remember how to check if c is of type String
        endmethod
    endstruct

    struct Character extends Comparable
        method operator > takes Comparable c returns boolean
            return LoadInteger(someHashtable,someKey_Character,StringHash(this.which)) > LoadInteger(someHashtable,someKey_Character,StringHash(Character(c).which))
        endmethod
    endstruct

//function interface example - imagine you don't have the operators being overloaded, they are simply all extending a common object.

    function interface Comparator takes Comparable a, Comparable b returns boolean

    function compareGreaterS2C takes Comparable str, Comparable char returns boolean
        return LoadInteger(someHashtable,someKey_Character,StringHash(SubString(str.which,0,1))) > LoadInteger(someHashtable,someKey_Character,StringHash(Character(char).which))
    endfunction

endlibrary

library anotherTestLib uses testLib

//test function interface

    function needCompare takes Comparable a, Comparable b, Comparator comp returns nothing
        if comp.evaluate(a,b) then
            call BJDebugMsg("a is greater than b")
        elseif comp.evaluate(b,a) then // some sort of typecasting should be done first
            call BJDebugMsg("a is less than b")
        else
            call BJDebugMsg("a is equal to b")
        endif
    endfunction

    call needCompare(String.create("Not Dewey Decimal"),Character.create("F"),Comparator.compareGreaterS2C)

// test method interface

    function compare takes Comparable a, Comparable b returns nothing
        if a>b then
            call BJDebugMsg("a is greater than b")
        elseif a<b then
            call BJDebugMsg("a is less than b")
        else
            call BJDebugMsg("a is equal to b")
        endif
    endfunction

    call compare(String.create("Also Not"),String.create("Dewey Decimal, as this is just an example"))

endlibrary


compare and needCompare both do essentially the same functions, but here's an example of how something would be useful. They probably wouldn't be used too often, but it is useful to have them in some scenarios, nonetheless.
 
For example my CustomInventory uses them to call functions whenever an item is being dropped/picked/used/pawned.
 
i would say function interfaces in systems are written to give the user flexibility of what should happen when x happens. A good example would be a projectile system. set a function run OnHit (Unit collision) and do whatever you want, from damaging to creating sfx.
 
If you didn't understand any of the large code I wrote, it basically shows you two ways to perform the same function.

A method interface lets you have a parent 'class' from which there are a lot of sub-classes. This lets you have a function require any one of the sub-classes, and lets you work with it, knowing it has implemented a specific set of methods.

For example, an animal is an interface, in that animals can makeNoise, breathe, and eat. A dog and a fish are two animals, however a dog barks, a fish breathes oxygen through water, and the dog eats dog food.

A function interface is only really useful if you need to use a pointer to a function. In my example, I wanted to be able to compare two structs of arbitrary type. I decided to let the user of the function decide whether I was comparing a string or character with another string or a character. Essentially, it gives you a way to pass a function and call it, and makes sure that the function takes the right arguments. (So you aren't calling [ljass] function DoNothing takes nothing returns nothing[/ljass] with [ljass]call DoNothing(5,false,"lol")[/ljass]).
 
General chit-chat
Help Users

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top