• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Wurst] Dependency Injection

Status
Not open for further replies.
Level 10
Joined
Sep 19, 2011
Messages
527
What?
http://en.wikipedia.org/wiki/Dependency_injection

Requirement
Implement this interface

Wurst:
package DIObject

/**
 * Implement this interface in your class so it can be injected
 */
public interface DIObject
In classes where dependency injection is wanted.

Code
Wurst:
package DI

import HashMap
import DIObject

public class DI
    private static DI instance = new DI
    
    private HashMap<int, DIObject> dependencies = new HashMap<int, DIObject>
    
    private construct()
        skip
    
    /**
     *
     * @returns DI
     */    
    static function getInstance() returns DI
        return instance
    
    /**
     * Register a dependendency by a name
     *
     * @param string name
     * @param DIObject dependency
     */    
    function set(string name, DIObject dependency)
        dependencies.put(StringHash(name), dependency)
    
    /**
     * Gets a dependency by a name
     *
     * @param string name
     * @returns DIObject|null
     */    
    function get(string name) returns DIObject
        DIObject dependency = dependencies.get(StringHash(name))
        
        if (dependency == null)
            print("Dependency " + name + " is not registered")
        
        return dependency


Example
Wurst:
package Example

import DI
import DIObject

// ----------------------------------------------------------------

// Interface

abstract class Implementation implements DIObject
    abstract function greeting()

// ----------------------------------------------------------------

// Implementations

class ImplementationOne extends Implementation
    override function greeting()
        BJDebugMsg("Hi there")

class ImplementationTwo extends Implementation
    override function greeting()
        BJDebugMsg("I greet you")

// ----------------------------------------------------------------
    
init
    DI.getInstance().set("implementation", new ImplementationOne)
    
    DIObject dependency = DI.getInstance().get("implementation")
    Implementation implementation = dependency castTo Implementation
    
    implementation.greeting() // Hi there
    
    // -----------------------------------------------------------
    
    DI.getInstance().set("implementation", new ImplementationTwo)
    
    dependency = DI.getInstance().get("implementation")
    implementation = dependency castTo Implementation
    
    implementation.greeting() // I greet you
 
Last edited:

Deleted member 219079

D

Deleted member 219079

How's this useful in wc3? I mean what kind of possibilities this adds >_< I can't make up any
 
Status
Not open for further replies.
Top