473,569 Members | 2,400 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_imp l * i) : impl(i) {}
virtual ~shape() {}
virtual void draw() = 0;
};

class circle : public shape {
public:
circle(shape_im pl * 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(in t api);
shape_factory(i nt 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(in t api) {
if (instance == 0) {
instance = new shape_factory(a pi);
}

return instance;
}

int main(int argc, char *argv[]) {
if (argc 1) {
shape_factory *sf =
shape_factory:: get_instance(at oi(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 2092

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

Similar topics

9
15399
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 all. There are a couple of configuration issues I've run into that are perplexing me. First, I can't seem to change the classpath. I've added:
2
4035
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 and thus allow a clear and flexible separation of interface and implementation such that the implementation can be changed freely.
5
1543
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 limits connectivity to a single connection/account. I already have the bridge->API connection functioning and now I need to begin the multiple...
0
1332
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 class(interface) If the delegate points to an interface called implementor you have some classes that implement the interface.
160
4596
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 prof. would post program questions and the solutions online. For practice I would try to do the problems. I would reach to a certain point in the...
6
1903
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 for service consumers, at design time. But, I think the cnsumers must completely know all aspects of that service. This sounds not good, and breaks...
12
1977
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 pattern (make this code to be according to the bridge pattern), and what is the advantage of employing the bridge pattern? It seems "l'art pour...
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 information about the basic achitecture of the bridge. Eg: the basic requirements of the files, library, specific directory structure . Is there any...
2
1532
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 heart of the real world example is an abstract class MustInherit Class DataObject Public MustOverride Sub NextRecord()
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3657
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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 we have to send another system

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.