Is there any Design Pattern for calling object methods statically

Raheel

Basically I am trying to make a library for handling files and directories.

The idea is there will be separate classes for FileFinding, FileReading, FileWriting etc.

What I am trying to search is there any design pattern to achieve something like :

There is just once class lets say

<?php namespace vendor/FileHandler;

 class FileHandler {}

Now in the library I have specific classes let say

<?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;
    }
}

Now I want my library users to call the FileFinder::findFilesByType() by using the main class FileHandler::findFilesByType();

Please note: The FileFinder::findFilesByType() is not static method but I want it to be utilized as static method from class FileHanlder

Updated: The question I have asked above seems similar to Laravel's Facade pattern. But their implementation going above my head. Even I'm not sure if Facade pattern will do this.

mike

The facade should keep a static instance of every class that provides functionality you yourself want to forward to the library users.

In the facade's static methods, utilize the above objects and forward the method calls to them. Only use this approach, if the objects you forward to are stateless, otherwise you have to create the appropriate objects inside the facades method, in order to not propagate state information between method calls.

In the following a little example in java, but you'll get the point

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);
        }
    }
}

Strictly speaking this exact way of implementing a facade just suits your needs. A facade does not have to be a utility class with static methods, it can also be an instance of a class.

The principles behind the facade design pattern are abstracting from the intrinsics of a group of classes (or of a whole layer) that are responsible for some common functionality, encapsulating the operations and granting easy, maybe high-level, access to them.


As @PeeHaa mentioned in his comment, this static facade approach indeed is not in the sense of OOP, because violates the law of demeter, which says:

A method method of a class Class should only call methods

  • of Class
  • of an object created by method
  • on the arguments of method
  • on instance variables of Class

You don't use a facade with static methods in that sense, since you call the methods on the class and not on instances of it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

OOP design pattern - calling super methods implicitly or some other solution

From Dev

Is there any design pattern for using helper methods in models in Rails?

From Dev

Call main thread object methods from second thread design pattern

From Dev

Calling multiple methods on same same object without any references

From Dev

Is there any design pattern to use?

From Dev

Calling object methods in Javascript

From Java

Query Object Pattern (Design Pattern)

From Dev

Calling a class and methods from another class, non-statically

From Java

Transition methods in state design pattern

From Dev

Any Design Pattern for this use case

From Dev

Why does PHP allow calling non-static methods statically but does not allow calling non-static properties statically?

From Dev

Calling multiple methods of an object in sequence

From Dev

Design pattern: child class calling base class

From Java

what is context object design pattern?

From Java

Design pattern to populate an object with xml

From Java

Null object design pattern question

From Dev

Design Pattern for Object Modification with Timestamp

From Dev

Why Query Object Design Pattern

From Dev

Object creation design pattern in vtk

From Dev

Object Oriented Analysis and Design, design pattern

From Dev

Factory Design Pattern - defining methods in subclasses

From Java

Design pattern for logging entry and exit of methods?

From Dev

Which design pattern to use (Active and passive methods)?

From Java

Decorator design pattern java overriding methods question

From Dev

Best Design Pattern for Large Data processing methods

From Java

Design Pattern for Data Structure with Methods to Populate It?

From Dev

OO design pattern: How to add methods dynamically?

From Dev

Design pattern with methods that return modified parent

From Dev

Delegation design pattern with abstract methods in python

Related Related

  1. 1

    OOP design pattern - calling super methods implicitly or some other solution

  2. 2

    Is there any design pattern for using helper methods in models in Rails?

  3. 3

    Call main thread object methods from second thread design pattern

  4. 4

    Calling multiple methods on same same object without any references

  5. 5

    Is there any design pattern to use?

  6. 6

    Calling object methods in Javascript

  7. 7

    Query Object Pattern (Design Pattern)

  8. 8

    Calling a class and methods from another class, non-statically

  9. 9

    Transition methods in state design pattern

  10. 10

    Any Design Pattern for this use case

  11. 11

    Why does PHP allow calling non-static methods statically but does not allow calling non-static properties statically?

  12. 12

    Calling multiple methods of an object in sequence

  13. 13

    Design pattern: child class calling base class

  14. 14

    what is context object design pattern?

  15. 15

    Design pattern to populate an object with xml

  16. 16

    Null object design pattern question

  17. 17

    Design Pattern for Object Modification with Timestamp

  18. 18

    Why Query Object Design Pattern

  19. 19

    Object creation design pattern in vtk

  20. 20

    Object Oriented Analysis and Design, design pattern

  21. 21

    Factory Design Pattern - defining methods in subclasses

  22. 22

    Design pattern for logging entry and exit of methods?

  23. 23

    Which design pattern to use (Active and passive methods)?

  24. 24

    Decorator design pattern java overriding methods question

  25. 25

    Best Design Pattern for Large Data processing methods

  26. 26

    Design Pattern for Data Structure with Methods to Populate It?

  27. 27

    OO design pattern: How to add methods dynamically?

  28. 28

    Design pattern with methods that return modified parent

  29. 29

    Delegation design pattern with abstract methods in python

HotTag

Archive