在 Holub 的模式书中无法弄清楚这一点

发布于 2024-08-18 21:49:01 字数 626 浏览 2 评论 0原文

我开始阅读 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 是一个单例。如何?我认为这是一个拼写错误,FactoryEmployee.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十二 2024-08-25 21:49:01

是的,只有一个拼写错误:
建议的修复:
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.

多情癖 2024-08-25 21:49:01

我敢打赌,在这两种情况下他都指的是“EmployeeFactory”。看起来只是一个错字。

I bet he just meant "EmployeeFactory" in both those cases. It looks like just a typo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文