开发者

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 th开发者_如何学编程em.

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.


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.


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));
    }
}


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.


Using generics, you might only need one factory method like:

public <T> T getObject(java.lang.Class<T> responseType, String name)

Then the user would call:

Weapon weapon = factory.getObject(Weapon.class, "my weapon");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜