Interface with not-required methods

蹲街弑〆低调 提交于 2019-12-11 13:12:05

问题


Is there a way to indicate optional method in the interface (so that the contract only indicated the number / type of arguments to be given)?

Please give maybe a little more understanding and insight into the problem, and indicate a solution? See for instance this discussion: Optional Methods in Java Interface

In the app I'm using Listeners connected to the Persistence (Doctrine). So I'm using some of these methods:

prePersist()
preUpdate()
postPersist()
postUpdate()

etc.

Now, while refactoring, since there are too many Entities (objects to be persisted) I decided to split the parts of these methods into separate classes.

However not all of them need all pre-... and post-... methods. I need to make sure they are given appropriate number and type of arguments. How do you do that in PHP?


回答1:


No. The whole idea of interfaces is to have a contract that guarantees that a method exists.

But a class can implement multiple interfaces, so you can define a different interface that contains that method and not add that interface to the class that doesn't have the method.




回答2:


Interfaces cannot have optional methods. That's the concept behind interface. If you however need something optional, then I suggest to additionally create default implementation of your interface which then all classes you need would then extend. This way all of these classes would implement interface and you would also be able to override just selected methods, having your optional behaviour.

Something like that:

interface MyInterface {
  public function method1();
  public function method2();
}

then Base class implements your interface's methods (I made it abstract to disallow direct use):

abstract class Base implements MyInterface {
    public function method1() {
       // dummy
    }
    public function method2() {
       // dummy
    }
}

and then:

class Optional extends Base {
   // method1 is not overridden, so Base' implementation applies

   public function method2() {
     // something here
   }
}



回答3:


Please see example here:

interface Workable
{
    public function work();
}

interface Feedable
{
    public function eat();
}

interface Employee extends Feedable, Workable
{
}

class Human implements Employee
{
    public function work()
    {
        // ....working
    }

    public function eat()
    {
        //.... eating in lunch break
    }
}

// robot can only work
class Robot implements Workable
{
    public function work()
    {
        // ....working
    }
}

Source: https://github.com/jupeter/clean-code-php




回答4:


I found an intresting library introducing WeakInterfaces. However I don't think it would be easy to make it work with Doctrine.




回答5:


After reading your comments, and internet as well, I decided to choose another work-around for the app design.

I created a single interface with all (required and to-be-optional) methods. Multiplying number of interfaces to acheive a simple task (especially in the case mentioned) I consider a really bad approach. This is because I believe the idea of class and interface is to keep connected things together, not to split them "artificially" into separate "containers". (Think for instance about a POPO in PHP / POJO in Java).

Now, all classes need to implement all potential methods, but some of them may be empty, or throw an exception, as indicated in the discussion with link given above, or by GolezTrol in his comment. Thanks for interest, anyway.



来源:https://stackoverflow.com/questions/27890873/interface-with-not-required-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!