簡體   English   中英

如果我只有對象,該如何重寫類的行為?

[英]How to override the behavior of a class if I have only its object?

這個問題可能是一個愚蠢的問題,但是請幫我解決這個問題。 我需要重寫類的行為,但是我只會得到它的對象。 以下是代碼示例。

我想在所有地方都使用方法Util.newChildComponent(String id)本身。

class ParentComponent{
    public ParentComponent(String id){}
    protected void someBehavior(){}
}

class ChildComponent extends ParentComponent{
    public ChildComponent(String id){
        super(id);
    }
    protected void childsBehavior(){}
}

public class Util {
    public static ParentComponent newChildComponent(String id)
    {
        ParentComponent fc = new ChildComponent(id);
        // Initialize the object, and performs some common configuration.
        // Performs different operations on the generated child object.
        // The child class has many members apart from childsBehavior().
        return fc;
    }
}


// The above classes belongs to a jar, so I cannot edit the code.
// I need to use Util.newChildComponent(String id)
// But I need to override the behavior as mentioned below.
public void f1()
{
    // TODO: How to override childsBehavior() method using the object 
    // I have it in pc? Is it possible?
    // Util.newChildComponent(id) method decorates the ChildComponent
    // So I need to use the same object and just override childsBehavior() method only.
    ParentComponent pc = Util.newChildComponent("childId");

    // I need to achieve the result below
    /*
    ParentComponent pc = new ChildComponent(id){
            @Override
            protected void childsBehavior(){
                super.someBehavior();
                // Do the stuff here.
            }
        }; */
}

提前致謝。

聽起來您正在尋找DynamicObjectAdaptorFactory

這是他出色的對象的用途。

static class ParentComponent {

    protected void someBehavior() {
        System.out.println("ParentComponent someBehavior");
    }
}

static class ChildComponent extends ParentComponent {

    private ChildComponent(String id) {

    }

    protected void childsBehavior() {
        System.out.println("childsBehavior");
    }
}

public static class Util {

    public static ParentComponent newChildComponent(String id) {
        ParentComponent fc = new ChildComponent(id);
        // Initialize the object, and performs some common configuration.
        return fc;
    }
}

interface Parent {

    void someBehavior();

}

// The adaptor.
public static class Adapter implements Parent {

    final ParentComponent parent;

    private Adapter(ParentComponent pc) {
        this.parent = pc;
    }

    // Override the parent behaviour.
    public void someBehavior() {
        System.out.println("Adapter invoke someBehaviour()");
        parent.someBehavior();
        System.out.println("Adapter finished someBehaviour()");
    }

}

public void test() {
    ParentComponent pc = Util.newChildComponent("childId");
    Parent adapted = DynamicObjectAdapterFactory.adapt(pc, Parent.class, new Adapter(pc));
    adapted.someBehavior();
}

打印:

Adapter invoke someBehaviour()
someBehavior
Adapter finished someBehaviour()

看看Java的動態代理。 這應該允許您通過創建調用處理程序來處理對方法的調用,從而在運行時更改行為。 如果需要,您甚至可以將其轉發到原始方法。

聲明一個新類,用您要更改的方法擴展該類

public class MyClass extends ChildComponent {

    @Override
    protected void childsBehavior()
    {
     .....
    }

}

然后在您的代碼中可以使用強制轉換,由於繼承,我們可以享受這一功能

MyClass pc =(myClass)Util.newChildComponent(“ childId”);

但是然后您必須像這樣創建新對象

MyClass pc = new MyClass("childId");

並手動進行在Util完成的配置。 除非您擴展Util並創建一個創建MyClass對象並在其中復制配置代碼的方法。

現在pc將具有ParentComponentChildComponent所有方法,但是childsBehavior將是您聲明的內容。

如果您不想更改使用Util實例化所需對象的代碼,則可以使用所需行為重寫Util類(即,使用重寫的方法實例化擴展ChildComponent的類),只需記住將其放入在項目中具有相同名稱的程序包中(以便其客戶端的導入不會更改)。 這樣,其他任何代碼都不會更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM