473,508 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Struggling to understand Bridge pattern benefits

Maybe my example isn't a good candidate for Bridge, or maybe I'm
applying Bridge incorrectly, but I don't see the benefit of the
pattern here. If I eliminated the Bridge and extended the shape
hierarchy to implement all combinations of shape/api, the number of
classes would be the same and the factory could still be used to
create the correct one. The client would still only care about the
shape interface. Seems like the proliferation of classes is simply
pushed to the implemetation hierarchy.

What am I missing?

#include <iostream>

class shape_impl {
public:
virtual ~shape_impl() {}
virtual void draw() = 0;
};

class opengl_circle : public shape_impl {
public:
virtual void draw() { std::cout << "opengl circle" <<
std::endl; }
};

class opengl_quad : public shape_impl {
public:
virtual void draw() { std::cout << "opengl quad" << std::endl; }
};

class directx_circle : public shape_impl {
public:
virtual void draw() { std::cout << "directx circle" <<
std::endl; }
};

class directx_quad : public shape_impl {
public:
virtual void draw() { std::cout << "directx quad" << std::endl; }
};

class raw_circle : public shape_impl {
public:
virtual void draw() { std::cout << "raw circle" << std::endl; }
};

class raw_quad : public shape_impl {
public:
virtual void draw() { std::cout << "raw quad" << std::endl; }
};

class shape {
protected:
shape_impl *impl;
public:
shape(shape_impl * i) : impl(i) {}
virtual ~shape() {}
virtual void draw() = 0;
};

class circle : public shape {
public:
circle(shape_impl * i) : shape(i) {}
virtual void draw() { impl->draw(); }
};

class quad : public shape {
public:
quad(shape_impl * i) : shape(i) {}
virtual void draw() { impl->draw(); }
};

class shape_factory {
static const int OPENGL;
static const int DIRECTX;
static const int RAW;
static shape_factory * instance;
int graphics_api;
public:
static shape_factory * get_instance(int api);
shape_factory(int api) {
graphics_api = (api == 1 || api == 2) ? api : RAW;
}
shape * create_circle() {
if (graphics_api == OPENGL) {
return new circle(new opengl_circle);
} else if (graphics_api == DIRECTX) {
return new circle(new directx_circle);
} else { // Raw mode
return new circle(new raw_circle);
}
}
shape * create_quad() {
if (graphics_api == OPENGL) {
return new quad(new opengl_quad);
} else if (graphics_api == DIRECTX) {
return new quad(new directx_quad);
} else { // Raw mode
return new quad(new raw_quad);
}
}
};

const int shape_factory::OPENGL = 1;
const int shape_factory::DIRECTX = 2;
const int shape_factory::RAW = 3;
shape_factory * shape_factory::instance = 0;
shape_factory * shape_factory::get_instance(int api) {
if (instance == 0) {
instance = new shape_factory(api);
}

return instance;
}

int main(int argc, char *argv[]) {
if (argc 1) {
shape_factory *sf =
shape_factory::get_instance(atoi(argv[1]));

shape * c = sf->create_circle();
c->draw();
shape * q = sf->create_quad();
q->draw();
} else {
std::cout << "Usage: bridge [1 | 2 | 3]" << std::endl;
}

return 0;
}

Jun 1 '07 #1
0 2087

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
15397
by: Steven M. Scotten | last post by:
Hi-- I seem to have the PHP Java Bridge (2.0.5 built from tarball) working with PHP 5.0.3 (also built from tarball) on my Fedora Core 1 server with Apache 2.0.50 and I'm pretty excited about it...
2
4032
by: Debajit Adhikary | last post by:
I'm still pretty new to design patterns... I was wondering, is there any difference between the Bridge Pattern and Herb Sutter's Pimpl Idiom? Both delegate responsibility to an implementation...
5
1541
by: Lorn | last post by:
I'm undertaking wriitng a bridge application between a remote data server API and multiple utility applications which pull and send data to the API. The reason for a bridge is that the data server...
0
1327
by: Tony Johansson | last post by:
Hello Experts! Some information. In a more general form is the handle body principles called the bridge design pattern. In the handle class you have a delegate that points to an pure abstract...
160
4526
by: RG | last post by:
Greetings friends, This semester I have started a course in C programming. I was moving along fine until I reached to the topic of loops (feeling embarrassed among you elite programmers). My...
6
1897
by: dotNeter | last post by:
The services, talked here, are things used in IServiceContainer, IServiceProvider, etc. In my opinion, everything can be a service, and a service is generally used for providing specific features...
12
1969
by: Adrian | last post by:
The code below was taken from an example. All the "noise" in the example was thrown out. This is supposedly according to the bridge pattern. What in the code (which lines) represent the bridge...
0
1276
by: sneha29 | last post by:
Hi, I am trying to write a bridge similar to Python- Uno bridge. I have read from these links about the PyUNO bridge. http://udk.openoffice.org/python/python-bridge.html But I haven't found...
2
1531
by: Simon Woods | last post by:
Hi I'm just working through (and learning) the standard GoF Design Pattern and the example of the Bridge pattern on http://www.dofactory.com/Patterns/PatternBridge.aspx#_self2 I notice at the...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.