在 Holub 的模式书中无法弄清楚这一点
我开始阅读 Holub 的模式书,不确定这是否是一个错误(第 59-61 页)。
他在清单 2-3 中
public interface Employee
{ void youAreFired();
}
public static class EmployeeFactory
{ private Factory() {}
public static Employee create()
{ return new Peon();
}
}
/* package*/ class Peon implements Employee
{ public void youAreFired()
{ //lots of code
}
}
使用了 Employee.Factory.create()。 Factory 不是 Employee 的内部类,那么如何使用它呢?
然后往下两页,他说 Employee.Factory
是一个单例。如何?我认为这是一个拼写错误,Factory
或Employee.Factory
实际上应该是EmployeeFactory
。我希望我没有错过 Java 编程方面的专业知识!
I started reading Holub's pattern book and not sure if this is a mistake (pg 59-61).
He has in listing 2-3
public interface Employee
{ void youAreFired();
}
public static class EmployeeFactory
{ private Factory() {}
public static Employee create()
{ return new Peon();
}
}
/* package*/ class Peon implements Employee
{ public void youAreFired()
{ //lots of code
}
}
He is using Employee.Factory.create()
. Factory is not a inner class of Employee, so how is using that?
Then two pages down he says Employee.Factory
is a singleton. How? I think its a typo, Factory
or Employee.Factory
should actually be EmployeeFactory
. I hope I am not missing something major in Java programming!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,只有一个拼写错误:
建议的修复:
public static class EmployeeFactory
=> 行public static class Factory
在考虑上述修复后修改您所写的内容将导致清晰的愿景。
yeb there is single typo only:
Proposed fix:
the line
public static class EmployeeFactory
=>public static class Factory
revise what you wrote after considering above fix shall result in clearing the vision.
我敢打赌,在这两种情况下他都指的是“EmployeeFactory”。看起来只是一个错字。
I bet he just meant "EmployeeFactory" in both those cases. It looks like just a typo.