2015-04-03 58 views
2

基本上我試圖做一個庫來處理文件和目錄。是否有靜態調用對象方法的任何設計模式

的想法是將有單獨的類FileFindingFileReadingFileWriting

我試圖尋找是否有任何的設計模式來實現類似:

只是有一次班讓說

<?php namespace vendor/FileHandler; 

class FileHandler {} 

現在在圖書館裏我有具體的課程讓說

<?php namespace vendor/FileHandler; 

class FileFinder 
{ 
    /** 
    * Find files by their types 
    * 
    * @param string $path path to find files in. 
    * @param mixed $type string or array of file type/types (extensions). 
    * @return array array of file names. 
    */ 
    public function findFilesByType($path, $type) 
    { 
     // do logic here. 
     return $files; 
    } 
} 

現在,我希望我的圖書館用戶通過使用主類調用FileFinder::findFilesByType()FileHandler::findFilesByType();

請注意:FileFinder::findFilesByType()不是靜態方法,但我希望它被用作靜態方法從class FileHanlder

更新: 我上面提到的問題似乎與Laravel的Facade模式相似。但他們的實施超越了我的頭腦。即使我不確定Facade模式是否會做到這一點。

+0

visit -http://php.net/manual/en/language.oop5.static.php – 2015-04-03 10:01:04

+0

另一個 - > http://stackoverflow.com/questions/14382206/php-uses-static-methods-in -object-context – 2015-04-03 10:01:44

+0

@SagarNaliyapara你的鏈接不能回答我的問題。 – 2015-04-03 10:03:28

回答

1

外觀應該保持每個類的靜態實例,它提供了您自己想要轉發給庫用戶的功能。

在外觀的靜態方法中,利用上述對象並將方法調用轉發給它們。 只有使用這種方法,如果您轉發的對象是無狀態的,否則您必須在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); 
     } 
    } 
} 

嚴格地說實施門面只是適合您需要的這個確切的方法。門面不一定是具有靜態方法的實用程序類,它也可以是類的實例。

立面設計模式背後的原理是從一組類(或整個圖層)的內在抽象出來的,它們負責一些常見的功能,封裝操作並授予簡單的,​​也許高層次的訪問他們。


由於@PeeHaa在他的評論中提到的,這種靜態門面的做法確實不OOP的意義,因爲違反了law of demeter,它說:

一種方法,一類methodClass應該只調用方法

    Class 創建的對象的
  • 通過method
  • 上的method
  • 上的Class

不要使用在這個意義上說靜態方法的一個門面,因爲你調用這個類,而不是它的實例方法實例變量的參數。

+0

感謝我瞭解了這個概念。您能否提供您的評論作爲@PeeHaa在他的評論中提到的這種方法如何抵制OOP。我只想在更進一步之前清楚。謝謝 – 2015-04-03 12:57:28

+1

我更新了答案。 – mike 2015-04-03 13:12:03

1

您可以使用__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));  
    } 
} 
} 
?> 

電話:

FileHandler::FileHandler('/files', 'pdf'); 

REAS更在:

http://php.net/manual/en/language.oop5.overloading.php#object.callstatic

+0

這似乎是一個工作解決方案,但我有100個方法分爲5個類。做這麼多「如果」會太長。你怎麼看 ? – 2015-04-03 10:14:29

+1

@RaheelKhan你可以結合我的答案與反射http://php.net/ReflectionClass我累了,在我的國家是早上6點20分,我花了整整一夜醒來 – 2015-04-03 10:20:29

+0

大聲笑肯定,謝謝你的信息:) – 2015-04-03 10:21:40

相關問題