Delegate

    技术2026-04-12  1

    The act of one object forwarding an operation to another object, to be performed on behalf of the first object.

    What’s the point? Delegation makes your code more reusable. It also lets each object worry about its own funtionality, rather than spreading the code that handles a single object’s behavior all throughout your application.  This means your objects are more independent of each other, or more loosely coupled. So the functionality of your app is spread out over lots of well-defined objects, which each do a single task really well.

    The most important thing is that your code Easy to change. One of the most common examples of delegation in Java is equals() method. Instead of a method trying to figure out if two objects are equal, it calls equals() on one of the objects and passes in the second object.

    最新回复(0)