Java – the best way to avoid duplicate code if two classes extend different classes

I'm developing an Android project and I'm facing this situation

I have two classes:

class A extends B
{

openDoor(){
//impl
}

closeDoor(){
//impl
}

}

class X extends Y{

openDoor(){
//impl
}

closeDoor(){
//impl
}

}

Now, if you observe these two classes in opendoor() and closedir()

What is the best way to avoid duplication?

My method

class ContainingDuplicateMethods{

     openDoor(){
    //impl
    }

    closeDoor(){
    //impl
    }

    }
   }

Create an object of containingduplicatemethod in the class and call the method. We call it policy pattern, but is this the best solution? Why can't we follow this practice in large projects, and people say it's not a good practice. What methods do I need to follow in this case?

Please note that class A and class X have extended other classes, and I don't want to use static, because - when the program execution starts, the static members are loaded into memory until the program is terminated, that is, my code runs continuously for days or weeks, and continues to create many objects with static references, so there may be a possibility of out of memory

Solution

"Conducive to the formation of heritage" is a thing worth remembering

Open the door and open the class Include a door as a member of a and B

Walla, finish the work

So a. getdoor() close(). B.getDoor(). Open, etc

If you need a common interface for a and B (so you can use it somewhere), then create

interface HasDoor {
    Door getDoor();
}

Now a and B can extend any class you like and implement hasdoor Any course that needs a door can accept hasdoor (or directly accept the door object), open, close, etc

No duplicate code, full flexibility

If you need your door to return methods to a and B, create the door class as an abstraction and implement it as an anonymous inner class in a and B Abstract methods will be called from the gate, and then when these methods are called, they can do any processing in a and B

For example, class A:

class A implements HasDoor {
      private Door door = new Door() {
          @override void notifyDoorChanged(boolean closed) {
                // The door is telling us its been opened or closed
          }
      }

      @override
      public Door getDoor() {
           return door;
      }
 }

The doors are:

public abstract class Door {
      boolean closed;
      abstract notifyDoorChanged();

      public void close() {
         closed = true;
         notifyDoorChanged(closed);
      }

      // etc
 }

Note that this is similar to the policy pattern, but not exactly the same The policy pattern has a main object and then inserts multiple policies (i.e. different forms of doors) There is a door that uses the same type of door as multiple other objects, although you can extend it to use strategic mode, and there are many doors that are easy to implement

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>