Is there a pattern whereby a subclass ensures that it invokes certain methods?

Is there a template or structure that ensures that the subclass will, at a certain stage in the life cycle, either invoke certain methods?

I am looking for something similar when a class implements a specific interface, in fact, it promises implements certain methods.

Anti pattern? I understand that it can actually be a kind of antipattern. I basically need to check the sanity that a collection of subclasses does the work that they should do. If nothing exists, I will just write a parser for the mini files, which will quickly verify that the methods are being called.

This question is a language agnostic, but in this case I am using C #

+4
source share
5 answers

I do not think this is possible, simply by design. I mean, the superclass doesn't know anything about its subclasses, so I don’t see how it can be.

Now, when you talk about the life cycle, maybe you have some methods start()and stop()? If so, why not use another method in the superclass, say finalCheck()one that cannot be overridden in a subclass and that checks to see if your methods have been called or not, maybe just use some fields of the class bool. I know that this does not guarantee anything, but at least it can be checked at runtime.

, , -. , :

abstract class Parent {
    void mustDo1() { }
    void mustDo2() { }
    abstract void do() { }
}

class Child : Parent {
    override void do() {
        base.mustDo1(); // force ?
        // something custom
        base.mustDo2(); // force ?
    }
}

- :

abstract class Parent {
    void mustDo1() { }
    void mustDo2() { }
    void do() {
        this.mustDo1();
        this.customDo();
        this.mustDo2();
    }
    abstract void customDo() { }
}

class Child : Parent {
    override void customDo() {
        // something custom
    }
}
+4

100%, , - , :

, , , ?

.

abstract, , abstract . abstract .

+5

, . , . , , , . , . , , , , .

+4

, - , , ..., , , , , :

  • , , - (, ). , , . , . , , , .

  • , , , 3 4 , , , DoImportantThingNow(); - , , , .

  • bsse. , , , , .

  • , , , .

, , - , , .

+3
source

No, this is not possible, the main reason is that the superclass does not know anything about its subclasses

+3
source

All Articles