我定义了几个类: 餐饮 面包 牛油 FoodFactory
如下:
Food.java
public abstract class Food {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public abstract void eat();
public Food(String id) {
this.id = id;
FoodFactory.getInstance().foodWasConstructed(this);
}
}
FoodFactory.java
public class FoodFactory {
private Map<String, Food> map = new HashMap<String, Food>();
private static FoodFactory factory = new FoodFactory();
private FoodFactory() {
}
public static FoodFactory getInstance() {
return factory;
}
public synchronized Food getFood(String id, String type) {
if(map.get(id) != null) {
return map.get(id);
} else {
if(type.equals("bread")) {
Food food = new Bread(id);
map.put(id, food);
return food;
} else if(type.equals("butter")) {
Food food = new Butter(id);
map.put(id, food);
return food;
}
}
return null;
}
public void foodWasConstructed(Food food) {
if(!map.containsKey(food.getId()))
this.map.put(food.getId(), food);
}
}
Bread.java
public class Bread extends Food {
static int i;
public Bread(String id) {
super(id);
i++;
}
public void eat() {
System.out.println("Eating bread : " + i);
}
}
Butter.java
public class Butter extends Food {
public Butter(String id) {
super(id);
}
public void eat() {
System.out.println("Eating butter");
}
}
现在我的要求是我不能创建两个具有相同id的Food实例。 我尝试运行以下代码:
public class SampleTest {
public static void main(String[] args) {
Food food1 = new Bread("1");
Food food2 = new Bread("1");
// food2 should return food1 reference. How can I accomplish this.
// food2 creates entirely new instance. What I want is it should refer to food1 instance.
// How can i do this. Is there any design flaw?
FoodFactory.getInstance().getFood("1", "bread").eat();
}
}
获得以下输出:
Eating bread : 2
我的问题如评论中所述: food2应该返回food1参考。我怎样才能做到这一点。 food2创造了全新的实例。我想要的是它应该引用food1实例。我怎样才能做到这一点。有任何设计缺陷吗?
答案 0 :(得分:1)
您需要根据变量 string JSONresult = JsonConvert.SerializeObject(dtbrands);
return JSONresult;
为hashCode()
类实施equals()
和Food
方法。因此,对于传递给构造函数的每个类似id
,只会创建一个类。当id
类扩展Bread
时,它会获得相同的行为,并且只为同一个Food
创建一个类。
您可以将id
课程更改为
Food
SampleTest.java
public abstract class Food {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public abstract void eat();
public Food(String id) {
this.id = id;
FoodFactory.getInstance().foodWasConstructed(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Food other = (Food) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
输出
<pre><code>public class SampleTest {
public static void main(String[] args) {
Food food1 = new Bread("1");
Food food2 = new Bread("1");
System.out.println(food1.hashCode());
System.out.println(food2.hashCode());
System.out.println(food1.equals(food2));
// food2 should return food1 reference. How can I accomplish this.
// food2 creates entirely new instance. What I want is it should refer
// to food1 instance.
// How can i do this. Is there any design flaw?
FoodFactory.getInstance().getFood("1", "bread").eat();
}
}</pre></code>
答案 1 :(得分:-2)
尝试像这样改变FoodFactory()构造函数
public class FoodFactory {
private Map<String, Food> map;
private static FoodFactory factory;
private FoodFactory() {
}
public static FoodFactory getInstance() {
if(factory==null)
{
factory = new FoodFactory();
map = new HashMap<String, Food>();
}
return factory;
}
}
它将始终返回FoodFactory实例的相同引用。