Does this fall under any particular design pattern?

Started by
27 comments, last by Shannon Barber 18 years, 7 months ago
Hey guys. I was tinkering with the design of my next class, when all of a sudden I wrote something along the lines of:

class Temp
{
public:

	Temp();
	virtual ~Temp();

	// Suppose this returns true by default, but a derived
	// class can override it to return false.
	virtual bool IsSizable() const;
};

..Instead of:

class Temp
{
protected:

	bool m_sizable;

public:

	Temp();
	virtual ~Temp();

	bool IsSizable() const;
};

Where Temp just wants to do something slightly different if IsSizable() returns true. I don't know where the idea came from, but it somehow reminded me of when I used to program using java. So I wondered, where would something like this be used? It seems to work well to separate and defer functionality onto derived classes.. But has anyone come across it in the past, and might know what the name given to it is? Something to do with component engineering or plug-ins? Virtual function overheads don't concern me, so don't bring that up. Inexperienced developers need not reply - I'd really just rather read up on it a little for future reference. Thanks!
Advertisement
Yeah...I'm a noob at design pattern but I recognize it!
This design pattern is called "bad programming". [smile]

Why ?

see this [paragraph 23.3]

In the base ctor you cannot call a virtual function in the derived object; why ?
Because the derived object does not exist yet.The result is that you always call the base function.

To be sure try this
virtual bool IsSizable() const = 0; // pure virtual

You will get a compiler error or probably (example MSVC6) a runtime error (something like pure virtual function call)

PS: if you call IsSizable() in other functions AFTER the object is created is OK but it has not a specific name; it's the classic OOP.
That doesn't make it bad programming until you try and use it in the constructor. I've used mechanisms like this. For example, I have a 'stream' class which has a virtual member function 'valid'. The default implementation is always valid, and always returns true. Derived classes, however, can return false if there has been an error. I'm tempted to classify it as a 'type traits' pattern.
Quote:Original post by Deyja
That doesn't make it bad programming until you try and use it in the constructor.


If you use it in ctor/dtor is bad programming ( you dont call the overriden function )

Otherwise it is the base of every object oriented programming; I know that it's possible in C++ to use virtual functions and/or override base methods...[smile]
...the base of every object-oriented programming? Excusing for a moment the grammatical deficiencies, no. You do not need virtual functions to do OOP. You do not even need member functions. It is a paradigm, not a syntax.
The true-by-default-but-overrideable-by-derived format of this virtual function stinks of brittle code design. I wouldn't go so far to label it outright bad in and of itself, but it may be a sign of mixing class heiarchies which shouldn't be mixed as they currently are.

For an example of what I'm talking about I'd point to a reply made by myself in another thread: here. Just substitute "ediable" and "chewable" with "is_*"/"Is*" equivilants. Basically it boils down to frogetting to check this function, or making the assumpiton that you're dealing with the base class (and it's invariants - including allways being sizeable), things of that nature.
Quote:Original post by Deyja
...the base of every object-oriented programming? Excusing for a moment the grammatical deficiencies, no. You do not need virtual functions to do OOP. You do not even need member functions. It is a paradigm, not a syntax.


polymorphism is only one of the most used definitions for OOP...

Quote:
Without virtual functions, C++ wouldn't be object-oriented.

cpp-faq

Quote:Original post by blizzard999
Quote:
Without virtual functions, C++ wouldn't be object-oriented.

cpp-faq


Correct, but it'd be entirely possible to write object-oriented code using C++ all the same. If you meant "the base of every object-oriented programming language" you could be considered correct. But it sounded like you were refering to the act of writing code in and of itself... in which case I must agree entirely with Deyja. Virtual functions (or similar runtime polymorphism) are the cornerstone of an OOP-oriented language (arguibly). That does not make it the sole cornerstone of an OOP-oriented program.
template method.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Okay moving away from the unavoidable "this is bad .. so bad omg plz someone think of the children" posts, can anyone else answer my question?

And thanks for your concerns MaulingMonkey and Blizzard, however I think I've programmed long enough to know where and when it'd be a bad idea to do this - hence why I didn't ask about that.

Deyja thanks for your response, I'll look up that pattern after I've finished making dinner. Rating++

R++ to DigitalDelusion also. I'll check that one out too.

This topic is closed to new replies.

Advertisement