Php 是否存在静态调用对象方法的设计模式
基本上,我正在尝试创建一个用于处理文件和目录的库 我们的想法是将为Php 是否存在静态调用对象方法的设计模式,php,algorithm,design-patterns,static,Php,Algorithm,Design Patterns,Static,基本上,我正在尝试创建一个用于处理文件和目录的库 我们的想法是将为文件查找、文件读取、文件写入等提供单独的课程 我想搜索的是,是否有任何设计模式可以实现以下目标: 比如说,只有一节课 <?php namespace vendor/FileHandler; class FileHandler {} 您可以使用\u callStatic <?php namespace vendor/FileHandler; class FileHandler { /** PHP >=
文件查找
、文件读取
、文件写入
等提供单独的课程
我想搜索的是,是否有任何设计模式可以实现以下目标:
比如说,只有一节课
<?php namespace vendor/FileHandler;
class FileHandler {}
您可以使用\u callStatic
<?php
namespace vendor/FileHandler;
class FileHandler {
/** PHP >= 5.3.0 */
public static function __callStatic($name, $arguments) {
if($name == 'findFilesByType') {
$obj = new FileFinder();
return $obj->findFilesByType(implode(', ', $arguments));
}
}
}
?>
更多信息请访问:
facade应该保留每个类的静态实例,这些类提供您自己想要转发给库用户的功能
在facade的静态方法中,利用上述对象并将方法调用转发给它们。
仅当转发到的对象是无状态的时,才使用此方法,
否则,您必须在facades方法内创建适当的对象,以避免在方法调用之间传播状态信息
下面是一个java小例子,但你会明白重点
public class Facade {
private static final HashComputer computer = new HashComputer();
// since this operation changes state of accumulator,
// it has to create one on each invocation
public static List<String> accumulate(String... args) {
Accumulator acc = new Accumulator();
for (String arg : args)
acc.add(arg);
return acc.collect();
}
// this operation does not change state of the object it delegates to,
// so there is no need to create a new instance on every invocation
public static int computeHash(String s) {
return computer.hashFor(s);
}
// has stateless instances
private static class HashComputer {
public int hashFor(String s) {
return s.hashCode();
}
}
// instances have state depending on state of list
private static class Accumulator {
List<String> arguments = new ArrayList<String>();
public void add(String s) {
arguments.add(s);
}
public List<String> collect() {
return Collections.unmodifiableList(arguments);
}
}
}
公共类Facade{
私有静态最终HashComputer计算机=新HashComputer();
//由于此操作改变了蓄能器的状态,
//它必须在每次调用时创建一个
公共静态列表累积(字符串…参数){
蓄能器acc=新蓄能器();
for(字符串arg:args)
附件add(arg);
返回acc.collect();
}
//此操作不会更改其委托对象的状态,
//因此,无需在每次调用时创建新实例
公共静态整数计算哈希(字符串s){
返回计算机。哈希值为(s);
}
//具有无状态实例
专用静态类哈希计算机{
公共整数哈希值(字符串s){
返回s.hashCode();
}
}
//实例的状态取决于列表的状态
私有静态类累加器{
列表参数=新的ArrayList();
公共无效添加(字符串s){
参数。添加(s);
}
公共列表收集(){
返回集合。不可修改列表(参数);
}
}
}
严格地说,这种实现门面的方法正好适合您的需要。facade不必是具有静态方法的实用程序类,它还可以是类的实例
facade设计模式背后的原则是从负责某些公共功能的一组类(或整个层)的本质中抽象出来,封装操作并授予对它们的简单(可能是高级)访问
正如@PeeHaa在其评论中所提到的,这种静态外观方法实际上不是OOP意义上的,因为它违反了,即:
类的方法method
应该只调用方法
- 属于
类
- 由
方法创建的对象的
- 关于
method
- 关于
类的实例变量
从这个意义上讲,您不能将facade与静态方法一起使用,因为您在类上而不是在其实例上调用方法。访问另一个方法->@SagarNaliyapara您的链接不能回答我的问题。FileFinder应该依赖于FileHandler,但静态调用不可能。因此,另一种方法是使用FileFinder扩展抽象FileHandler。然后您可以调用FileHandler的方法,用户不能直接使用抽象类。。。但是我不确定这是否是一个好的实践..删除了OOP标签,因为你想做的是OOP的反面。这似乎是一个可行的解决方案,但我有100个方法,分为5个类。如果
,那么要做这么多的,时间太长了。你觉得怎么样?@RaheelKhan你可以把我的回答和思考结合起来我累了,在我的国家是早上6:20,我整晚都醒着,我明白了这个概念。您能否提供您的评论,如@PeeHaa在其评论中提到的,这种方法是如何反对OOP的。我只是想在继续之前澄清一下。谢谢
FileHandler::FileHandler('/files', 'pdf');
public class Facade {
private static final HashComputer computer = new HashComputer();
// since this operation changes state of accumulator,
// it has to create one on each invocation
public static List<String> accumulate(String... args) {
Accumulator acc = new Accumulator();
for (String arg : args)
acc.add(arg);
return acc.collect();
}
// this operation does not change state of the object it delegates to,
// so there is no need to create a new instance on every invocation
public static int computeHash(String s) {
return computer.hashFor(s);
}
// has stateless instances
private static class HashComputer {
public int hashFor(String s) {
return s.hashCode();
}
}
// instances have state depending on state of list
private static class Accumulator {
List<String> arguments = new ArrayList<String>();
public void add(String s) {
arguments.add(s);
}
public List<String> collect() {
return Collections.unmodifiableList(arguments);
}
}
}