-1

I would like to use the Strategy Design Pattern, however, I'd like to force my top-level Strategy class to enforce a policy that every Strategy subclass is also Abstract so that I can trust my developers to generalize more carefully.

Paul McElroy
  • 373
  • 1
  • 2
  • 13
  • 1
    Don't override one of the abstract methods. – pptaszni Sep 30 '20 at 06:54
  • Why do you want to enforce that? I don't see any connection between forcing another layer of inheritance and generalizing more carefully. – molbdnilo Sep 30 '20 at 07:09
  • If every subclass is abstract then there cannot be any instances of this hiearchy. – Quimby Sep 30 '20 at 07:20
  • 1
    Do you mean that there's a mid-level categorization of strategies? E.g. s1 and s2 are of type sA and s3 is an sB, where you want sA and sB to be abstract classes derived from an abstract S, *but* s1, s2 and s3 should be concrete implementations. – Bob__ Sep 30 '20 at 08:43
  • Yes, I want to enforce a mid-level categorization of strategies exactly as you described. s1, s2 and s3 could be either concrete or abstract still, but it is no longer enforced. – Paul McElroy Oct 06 '20 at 01:44

1 Answers1

0
public class A
{
    int x;
    A():x(0) {};
    virtual void foo() { x=x+1; }
    virtual void bar() = 0;
}

public class B : public A
{
    B():A() {};
    virtual void foo() { x=x-1; }
    virtual void bar() = 0;
}

Congratulations you have here an example of subclass still abstract. Didn't try the code but I don't see anything that can prevent abstract subclass.

But you will never be able to instantiate A and B, is it what you want ?

cclst
  • 11
  • 4
  • Sorry, no, I want to enforce that Class B is Abstract. I don't want to give the implementer a choice as to whether certain functions should still be left virtual. – Paul McElroy Oct 06 '20 at 01:45
  • But if any subclass of A is forced to be abstract, nobody will be able to instantiate any A and its subclasses. What is the purpose of A ? Freezing a class name in a specific namespace ? I don't think there is something for that in C++ – cclst Oct 08 '20 at 09:08
  • Yeah, that's pretty much what I'm trying to. It's like doing "Kinds" in Haskell. I just want to enforce an umbrella of abstract classes to better organize the follow-up abstract classes which should then be allowed to have concrete subclasses. – Paul McElroy Nov 09 '20 at 22:08