纯静态类与单例类
编写一个 PHP 应用程序并有几个仅具有静态方法的类(不需要实例方法)。其中一个示例是 NumericValidator,它具有诸如 checkInteger($toCheck) 之类的方法,用于检查以确保您传递给它的参数是 int 类型,以及 checkGreaterThan ($lOperand, $rOperand),它确保左操作数大于右操作数,等等。
我知道我可以将这些方法中的每一个都扔到 PHP 文件中,而无需将它们放在一个类中,但我想在这里采用 OOP 方法,以防 API 发展到需要实例化 NumericValidator
。
但这确实引出了一个问题:具有 100% 静态方法的类与实现单例设计模式(整个代码库中使用的每个引用都调用同一个实例)的类有何不同?
例如,我的代码现在如下所示:
public function doSomething($p_iNum)
{
if(!NumericValidator::checkInteger($p_iNum))
// throw IllegalArgumentException
// ...
}
但是我可以将所有NumericValidator
的静态方法转换为非静态实例方法,迫使程序员实例化它,然后实现一个单例设计模式,这样您只能引用它的 1 个实例:
public function doSomething($p_iNum)
{
NumericValidator $nv = NumericValidator::getInstance();
if(!nv->checkInteger($p_iNum))
// throw IllegalArgumentException
// ...
}
最后,我的问题是:哪个更好、更符合最佳实践?是否有性能方面的考虑?这两种方法将如何影响并发性或来自多个用户的请求等问题?
Writing a PHP app and have several classes that only have static methods (no need for instance methods). An example of one is NumericValidator
, which has methods like checkInteger($toCheck)
which checks to make sure the argument you pass it is of type int, and checkGreaterThan($lOperand, $rOperand)
, which makes sure that the left operand is greater than the right operand, etc.
I know I could just throw each of these methods into a PHP file without putting them inside of a class, but I want to take an OOP approach here in case the API evolves to require instantiating NumericValidator
.
But it does beg the question: how is a class with 100% static methods any different than have a class implement a singleton design pattern, where every reference used throughout the code base invokes the same instance?
For example, here is what my code looks like now:
public function doSomething($p_iNum)
{
if(!NumericValidator::checkInteger($p_iNum))
// throw IllegalArgumentException
// ...
}
But I could turn all of NumericValidator
's static methods into non-static instance methods, forcing the programmer to instantiate it, and then implement a singleton design pattern so you can only ever reference 1 instance of it:
public function doSomething($p_iNum)
{
NumericValidator $nv = NumericValidator::getInstance();
if(!nv->checkInteger($p_iNum))
// throw IllegalArgumentException
// ...
}
Finally, my question: which is better and more in keeping with best practices? Are there performance considerations? How would either approach affect things like concurrency, or requests coming from multiple users?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将在您的示例中使用静态类。我将使用的区别在于您尝试在访问中保留的实例属性是否存在任何状态。这就是单例的设计目的。静态类提供对命名空间中的方法的有组织的访问,这有助于使代码清晰,但它本身没有任何属性。
所以,是的,您可以使用单例,但这将是一种不好的形式,因为您不希望跨页面访问提供任何实例属性。
希望这有帮助。
I would use a static class in your example. The differentiator I would use is if there is any state of the properties of an instance you are trying to preserve across access. This is what a singleton is designed for. The static class gives organized access to methods in a namespace which is helpful for clarity in your code but it does not have any properties about itself.
So yes you can use a singleton but it would be bad form because there are no instance properties that you want to make available across page accesses.
Hope this helps.
在 PHP 5.3 中,您可以获得静态类的实例:
课堂测试
{
公共静态函数实例()
{
打印“zz”;
}
}
$z = 新测试;
$z->instance();
不关心PHP中的并发请求,它是单线程的,每个进程执行自己的代码。
In PHP 5.3 you can get instance of static class:
class test
{
public static function instance()
{
print 'zz';
}
}
$z = new test;
$z->instance();
Don't care about concurrency requests in PHP, it's single threaded, each process executes own code.