Transform class with static methods into interface

Asked

Viewed 99 times

4

I have a custom class calling for ArrayList, I created to handle objects in a small project of mine, but I’d like to abstract it further, transforming it into an interface. In this way, I could create other classes that do the same thing with different banks (in this class, saved in a txt, but I have another that does the same things in a JSON file and in the future I will do one to save in Mysql).

How can I perform this transformation if it is feasible?

Follows the class ArrayList

class ArrayList {

    private static $list = null;

    private function __construct() {

    }

    private static function getList() {
        if (!isset(self::$list)) {
            $linha = "";
            if (file_exists(dirname(__DIR__) . '/model/DB.txt')) {
                $banco = dirname(__DIR__) . '/model/DB.txt';
                $a = fopen($banco, 'r');
                $linha = fread($a, filesize($banco));
                self::$list = unserialize($linha);
            } else {
                self::$list = Array();
            }
        }
    }

    public static function add($item) {
        self::getList();
        self::$list[] = $item;
    }

    public static function remove($item) {
        self::getList();
        for ($i = 0; $i < self::size(); $i++) {
            if (self::get($i) === $item) {
                unset(self::$list[$i]);
                break;
            }
        }
        self::$list = array_values(self::$list);
    }

    public static function get($indice) {
        self::getList();
        return self::$list[$indice];
    }

    public static function size() {
        self::getList();
        return sizeof(self::$list);
    }

    private static function gravar() {
        $texto = serialize(self::$list);
        $a = fopen(dirname(__DIR__) . '/model/DB.txt', 'w');
        fwrite($a, $texto);
        fclose($a);
    }

    public static function atualizarDB() {
        self::gravar();
    }

}

What I’m trying to do would be something like this:

interface ArrayList{
getList();
add($item);
save();
}
  • I didn’t quite understand the problem, to do this just create the interface and define all methods(signatures) without implementation and then each class that implements it, will need to create code that varies between classes.

  • 1

    it is more practical to create an abstract class, since it has some functions that are equal regardless of implementation, such as add, get, remove and size

  • @rray the problem is that, handling json or txt, if I change the files from different locations, the instance that is implementing the interface will not update.

1 answer

2


The first thing you’d have to do is stop using static elements in this class. It makes no sense to use an interface in an essentially static class.

I find it strange to have a database update method within such a generic data structure. I think this should be out, but it’s up to you.

I also do not know the requirements and exactly the purpose of creating the interface, but I believe that what you want is this:

interface iArrayList {
    public function add($item);
    public function remove($item);
    public function get($indice);
    public function size();
}

If you want you can put other mandatory and general methods.

Then this class, in addition to the suggested changes would be written like this:

class ArrayListDB implements iArrayList

The next one would probably be:

class ArrayListJson implements iArrayList

I put in the Github for future reference.

  • That’s exactly what I want to do, but using statistical methods, if my file is changed, I have real-time change when calling getList(), if I instantiate an example object ArrayListJson, that doesn’t happen.

  • 1

    Interfaces and static methods do not come together. The second part of the comment doesn’t make sense to me, you probably know more than I do about what’s going on in your class. Anyway this is another problem unrelated to the question.

  • If two people use the application at the same time and while the first makes a change to the json or txt by adding another object, the second call the getList for example, the change of the first user will not be reflected to the second, using static methods, they are reflected immediately (at least in the tests I did). Using instantiated class, I don’t know how to perform this change control.

  • 1

    It seems to me that the whole architecture of this is wrong. The solution is not this. There are several problems with this, "it works on the test I did" doesn’t mean it’s right. But the problem is moving away from what was asked. Even if the solution was to make the class static, and it’s not, you wouldn’t have been able to use interface. This is all I can answer within the scope of the question. If you have another problem, ask another question by showing it.

  • It’s I know, it’s a converted java:/ project. Maybe using Singleton and making the instance of single arraylist on the server would give a similar result, no?

  • As I said the problem seems to be another to what was asked, but the only thing you will get by converting to Singleton is to put the interface on it, does not solve the wrong architecture. It will only mix more responsibilities in the same class.

  • 1

    @Diegofelipe I would simply say that there is a problem of logic, and at this point, applying standards blindly does not help at all. Better specifies which type of application is.

  • @Edilson is a college activity here, he’s really having modeling problems. As I am not going to do everything from scratch, after reading the bigown’s answer, I decided to separate responsibilities and creating separate DAO’s for each type of rescue(txt, json or others). It takes work but at least it gets more organized.

  • @Diegofelipe Imagine, 2 users with simultaneous access to the same instance of an object is not the same as simultaneous login on different machines ? And so, they practically have the same properties, so the actions of one are those of the other. It’s like this.

  • @Edilson messed up my mind all this problem kk. I’m going to ask another question, more didactic because I didn’t quite understand a POO thing.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.