我为以下问题设计了以下内容:
class Animal {
// ...
}
class Guppy extends Animal { ... }
class Pigeon extends Animal { ... }
class TailedAnimal extends Animal {
// ...
}
class Dog extends TailedAnimal { ... }
class Cat extends TailedAnimal { ... }
class HornedAnimal extends Animal {
// ...
}
class Ram extends HornedAnimal { ... }
public static void main(String[] args) {
Animal a = getSomeAnimal();
a.doSomething();
if (a instanceof TailedAnimal) {
// do something
}
if (a instanceof HornedAnimal) {
// do something else
}
}
Animal,HornedAnimal和TailedAnimal主要用作数据模型。
由于Java不支持多重继承,因此我无法创建Rhinoceros,这是一种角和尾动物。在询问后,有人建议使用组合和界面。我想出了以下内容:
class Animal {
// ...
}
class Guppy extends Animal { ... }
class Pigeon extends Animal { ... }
class Ram extends Animal implements IHorned { ... }
class Cat extends Animal implements ITailed { ... }
class Dog extends Animal implements ITailed {
BasicTail t = new BasicTail();
public Object getTail() {
return t.getTail();
}
public void setTail(Object in) {
t.setTail(in);
}
}
interface ITailed {
public Object getTail();
public void setTail(Object in);
//...
}
class BasicTail implements ITailed {
Object myTail;
public Object getTail() { return myTail; }
public void setTail(Object t) { myTail = t; }
}
interface IHorned {
// getters and setters
}
public static void main(String[] args) {
Animal a = getSomeAnimal();
a.doSomething();
// how do I check if a is horned or tailed?
}
我的界面有getter和setter。有什么方法可以避免这种情况吗?假设目前没有办法抽象Tails和Horns的行为,并且它们主要用作数据持有者。我如何确定我的动物是角还是尾?
答案 0 :(得分:8)
我在这里建议策略模式。简而言之:
interface TailedAnimal {
void moveTail();
}
interface HornedAnimal {
void hitWithHorn();
}
class Rhinoceros() implements TailedAnimal, HornedAnimal {
private TailedAnimal tail; //Instantiate it somehow e.g. constructor, setter
private HornedAnimal horn; //Instantiate is somehow e.g. constructor, setter
public void moveTail() {
tail.moveTail();
}
public void hitWithHorn() {
horn.hitWithHorn();
}
}
通过使用它,您可以在接口的具体实现中封装行为,并且可以轻松地为少数动物分享完全相同的行为,以及在运行时更改它。
答案 1 :(得分:4)
我认为你必须避免一般的制定者。如果可以,请使用不可变对象,并将其私有数据初始化为其构造函数。
为了区分动物,我使用了另一种模式,即访客。它很冗长,但您不必直接测试您正在处理的动物。
public class Animals {
private Animals() {
}
interface Animal {
void accept(final AnimalProcessor visitor);
}
interface AnimalProcessor {
void visitTailed(final TailedAnimal tailedAnimal);
void visitHorned(final HornedAnimal hornedAnimal);
}
interface TailedAnimal extends Animal {
void moveTail();
}
interface HornedAnimal extends Animal {
void hitWithHorns();
}
static class Dog implements TailedAnimal {
public void moveTail() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void accept(final AnimalProcessor visitor) {
visitor.visitTailed(this);
}
}
static class Cat implements TailedAnimal {
public void moveTail() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void accept(final AnimalProcessor visitor) {
visitor.visitTailed(this);
}
}
static class Ram implements HornedAnimal {
public void hitWithHorns() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void accept(final AnimalProcessor visitor) {
visitor.visitHorned(this);
}
}
static class Rhinoceros implements HornedAnimal, TailedAnimal {
public void hitWithHorns() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void moveTail() {
//To change body of implemented methods use File | Settings | File Templates.
}
public void accept(final AnimalProcessor visitor) {
visitor.visitTailed(this);
visitor.visitHorned(this);
}
}
public static void main(String[] args) {
Collection<Animal> animals = new ArrayList<Animal>(Arrays.asList(new Dog(), new Cat(), new Rhinoceros()));
for (final Animal animal : animals) {
animal.accept(new AnimalProcessor() {
public void visitTailed(final TailedAnimal tailedAnimal) {
// you do what you want when it's a tailed animal
}
public void visitHorned(final HornedAnimal hornedAnimal) {
// you do what you want when it's a horned animal
}
});
}
}
}
答案 2 :(得分:1)
我已经删除了我以前的答案。我想到了更好的东西。如果你很好奇,请参阅这篇文章的修订版。
使用规范模式。它非常适合这里的法案 - 比装饰师更多。你要求“检查”一只动物是否有角。装饰者模式提供透明度,而在这种情况下,你似乎要求歧视。
规范模式包含了如何评估某些标准的知识。在我们的例子中,我们想要类似的东西:
public interface Specification {
public boolean isSatisfiedBy(Animal aCriteria);
}
public class HornedAnimalSpecification implements Specification {
@Override
public boolean isSatisfiedBy(Animal aCriteria) {
//Right here is where the heart of your problem
//can be solved.
//
//Reserved conquering grounds.
}
}
现在您可以根据需要定义Animal层次结构。你现在唯一需要做的就是找出是什么让动物角落。您对该问题的回答将进入规范类。那么你的主要功能很简单。
public class Zoo {
public static void main(String[] args) {
Animal ram = getHornedAnimal(); //Instantiate however you'd like.
Specification specification = new HornedAnimalSpecification();
if (specification.isSatisfiedBy(ram)) {
//Bingo, it's horned.
} else {
//Not horned!
}
}
}