Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What is this pattern called?

35 views
Skip to first unread message

JiiPee

unread,
Feb 18, 2017, 6:06:55 AM2/18/17
to
I use this a lot but just dont know (even after reading about patterns)
which pattern it is. Surely a simple question..

Can somebody help me please, what pattern is this when you can add a
different behaviour by pointer member variable:


class DrivingStyle
{
public:
virtual void drive();
};

class DriveNormally : public DrivingStyle
{
public:
void drive();
};

class DriveErratically : public DrivingStyle
{
public:
void drive();
};



class Car
{
public:
DrivingStyle* m_drivingStyle;
void drive() {
m_drivingStyle->drive();
}

};


void main()
{
Car car1;
car1.m_drivingStyle = new DriveErratically();
car1.drive();
car1.m_drivingStyle = new DriveNormally();
car1.drive();
...
}

Mr Flibble

unread,
Feb 18, 2017, 6:44:36 AM2/18/17
to
It is most definitely strategy, could be facade, it is almost proxy, but
what it actually is is just basic subtype polymorphism.

/Flibble

Alf P. Steinbach

unread,
Feb 18, 2017, 7:38:33 AM2/18/17
to
On 18.02.2017 12:06, JiiPee wrote:
>
> void main()

has always been invalid in both C and C++.


Cheers & hth.,

- Alf

Chris Vine

unread,
Feb 18, 2017, 8:00:30 AM2/18/17
to
It's called 1990s OOP pattern.

These days you would probably (depending on the problem area) consider
higher order functions with std::function.

JiiPee

unread,
Feb 18, 2017, 8:40:08 AM2/18/17
to
On 18/02/2017 12:36, Alf P. Steinbach wrote:
> has always been invalid in both C and C++.


but is it a big error? maybe not...?? and it compiles...

JiiPee

unread,
Feb 18, 2017, 8:42:30 AM2/18/17
to
On 18/02/2017 13:00, Chris Vine wrote:
> It's called 1990s OOP pattern.


yes but what is the name of that pattern?

JiiPee

unread,
Feb 18, 2017, 8:43:39 AM2/18/17
to
On 18/02/2017 12:36, Alf P. Steinbach wrote:
..but.. even int main() does not need to return a value!!

JiiPee

unread,
Feb 18, 2017, 8:44:30 AM2/18/17
to
On 18/02/2017 11:44, Mr Flibble wrote:
> It is most definitely strategy, could be facade, it is almost proxy,
> but what it actually is is just basic subtype polymorphism.


can you choose already? :) so which one??

Chris Vine

unread,
Feb 18, 2017, 9:18:51 AM2/18/17
to
That depends on why you are doing it. Based on your small example it
looks as if you want to implement the policy pattern (aka strategy
pattern) for run time.

If so, something on the scale of your example Car type would be much
more easily implemented by delegating the polymorphic behavior to a
std::function object as a member of the Car type, holding a lambda
object or some other function object giving effect to the selected
behavior.

Öö Tiib

unread,
Feb 18, 2017, 2:40:54 PM2/18/17
to
On Saturday, 18 February 2017 13:06:55 UTC+2, JiiPee wrote:
> I use this a lot but just dont know (even after reading about patterns)
> which pattern it is. Surely a simple question..
>
> Can somebody help me please, what pattern is this when you can add a
> different behaviour by pointer member variable:

snip

>
> void main()

That is called "flame bait" pattern. Posting code with "void main"
in it gets flames in both comp.lang.c and comp.lang.c++ and so livens up
discussions there.

> {
> Car car1;
> car1.m_drivingStyle = new DriveErratically();
> car1.drive();
> car1.m_drivingStyle = new DriveNormally();

That is called "memory leak" pattern. It helps the industry to sell more
and bigger RAM to customer computers.

> car1.drive();
> ...
> }

JiiPee

unread,
Feb 18, 2017, 5:38:12 PM2/18/17
to
On 18/02/2017 19:40, Öö Tiib wrote:
> That is called "memory leak" pattern. It helps the industry to sell more
> and bigger RAM to customer computers.


heh, but can you also now asnwer the question itself???

Öö Tiib

unread,
Feb 18, 2017, 7:48:35 PM2/18/17
to
If to put defects aside then similar things are usually called
"strategy pattern".

JiiPee

unread,
Feb 19, 2017, 8:17:02 AM2/19/17
to
yes thats correct, thanks. Good to know now...

JiiPee

unread,
Feb 19, 2017, 8:17:13 AM2/19/17
to
On 18/02/2017 11:44, Mr Flibble wrote:
> It is most definitely strategy


yes, correct. thanks

0 new messages