Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Techniques to expose multiple Interfaces (via static creation methods)

I am currently working on a project where I am attempting to hide as much detail about a hierarchy I have created as possible. I want to do this to minimize the amount of information the user needs to know about objects (and to control what they can do to the state of the object). In addition, I'm using the pattern to limit what kinds of objects the application can make, and limit it to creation from the factory.

The main issue I am having, however, is that there are a few different kinds of interfaces I would like to expose. Each interface is has additional functionality that I don't believe should be shared, and I would like to keep these interfaces separated. Finally, I don't know what new interfaces may come in the future, but I'd like to try and be ready for them.

Weapon:

public interface Weapon extends GameObject {
    Number attack();

    boolean addWeaponAttribute(WeaponAttribute attribute);
}

Firearm:

public interface Firearm extends Weapon {
    void reload(Number rounds);
}

My question is what would be the best way to have the factory produce objects with different interfaces? Here's what I am thinking "the best would be":

  1. The most clear to the user (it's obvious what they're asking for and what they're getting back)
  2. The best for future expansion (I am uncertain what new interfaces I will be adding to this system).

Here's what I have been thinking so far:

Create properly named methods for each interface

public static Firearm getFirearm(String firearmName) {
    ...
}

public static Weapon getWeapon(String weaponName) {
    ...
}

Do the above, but produce the factories in separately named classes

public class WeaponFactory {
    public static Weapon getWeapon(String weaponName) {
        ...
    }
}


public class FirearmFactory {    
    public static Firearm getFirearm(String firearmName) {
        ...
    }
}

Something completely different

I'm open to suggestions, and changes. This is a flexible project, so I can change as much as I want to (in terms of this portion of the project) to make a better result.

Also - As a side note, I was uncertain if this question was too open-ended or not for SO. If I made a mistake posting here, let me know and I'll move my question elsewhere.

like image 530
pseudoramble Avatar asked Aug 30 '11 00:08

pseudoramble


People also ask

Can you inherit multiple interfaces?

Components can inherit multiple interfaces, though. Inheriting multiple interfaces isn't problematic, since you're simply defining new method signatures to be implemented.

Can a class implement multiple interfaces?

Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. By convention, the implements clause follows the extends clause, if there is one.

Can an object have multiple interfaces?

A class or struct can implement multiple interfaces. A class can inherit a base class and also implement one or more interfaces.

What is static interface in Java?

Similar to Default Method in Interface, the static method in an interface can be defined in the interface, but cannot be overridden in Implementation Classes. To use a static method, Interface name should be instantiated with it, as it is a part of the Interface only.


3 Answers

What I can suggest is to make the interfaces as concise as possible and move other unrelated methods elsewhere. you might consider doing this for example:

public interface Weapon extends GameObject {
    Number attack();
}

public interface Modifiable extends GameObject {
    boolean addWeaponAttribute(WeaponAttribute attribute);
}

public class ActualWeapon implements Weapon, Modifiable {
...
}

Then you can create different factories to generate your concrete objects, as you already mentioned:

public class WeaponFactory {
    public static Weapon getWeapon(String weaponName) {
        ...
    }
}

or

public class GenericFactory<T extends GameObject> {
    public T createGameObject(Object... properties) {
        ...
    }
}
public class WeaponFactory extends GenericFactory<ActualWeapon> {
    public ActualWeapon createGameObject(Object... properties) {
        ...
    }
}

I think you can't add static methods to interfaces. I wouldn't recommend it if you even could.

like image 85
n0rm1e Avatar answered Oct 15 '22 14:10

n0rm1e


maybe just use the factory method design pattern like

interface GameObject {}
class WeaponAttribute {}
interface Weapon extends GameObject {
    Number attack();
    boolean addWeaponAttribute(WeaponAttribute attribute);
}
interface Firearm extends Weapon {
    void reload(Number rounds);
}
class WeaponBaseClass implements Weapon {
    WeaponBaseClass(WeaponName weaponName) {
        this.weaponName=weaponName;
    }
    @Override public Number attack() {
        return null;
    }
    @Override public boolean addWeaponAttribute(WeaponAttribute attribute) {
        return false;
    }
    public String toString() {
        return weaponName.toString();
    }
    final WeaponName weaponName;
}
class FirearmBaseClass extends WeaponBaseClass implements Firearm {
    public FirearmBaseClass(WeaponName weaponName) {
        super(weaponName);
    }
    @Override public void reload(Number rounds) {}
}
enum WeaponName {
    knife, sword, colt45, glock19, glock19WithLaser;
}
class WeaponCreator {
    Weapon create(WeaponName weaponName) {
        switch (weaponName) {
            case knife:
            case sword:
                return new WeaponBaseClass(weaponName);
            case colt45:
            case glock19:
                return new FirearmBaseClass(weaponName);
            default:
                return new WeaponBaseClass(weaponName);
        }
    }
}
class FancyWeaponCreator extends WeaponCreator {
    Weapon create(WeaponName weaponName) {
        Weapon weapon = null;
        switch (weaponName) {
            case glock19WithLaser:
                weapon = super.create(WeaponName.glock19);
                // whatever it needs
                return weapon;
            default:
                return new WeaponBaseClass(weaponName);
        }
    }
}
public class Main {
    public static void main(String[] args) {
        System.out.println(new WeaponCreator().create(WeaponName.knife));
        System.out.println(new WeaponCreator().create(WeaponName.colt45));
        System.out.println(new FancyWeaponCreator().create(WeaponName.glock19WithLaser));
    }
}
like image 23
Ray Tayek Avatar answered Oct 15 '22 15:10

Ray Tayek


What about a factory of factories? Each factory would implement ifactory. Ifacorty would require a method Instantiate(string type) and return your subclassed weapon instance.

like image 1
Josh C. Avatar answered Oct 15 '22 14:10

Josh C.