Even with the example, this article is impenetrable. Some of the problem may just be bad grammar/phrasing, but since I don't understand what it's trying to say
| This article is rated Start-class on Wikipedia's content assessment scale. It is of interest to the following WikiProjects: | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Even with the example, this article is impenetrable. Some of the problem may just be bad grammar/phrasing, but since I don't understand what it's trying to say I can't fix it myself. 18.24.0.120 04:23, 4 Dec 2003 (UTC)
The diagram and the accompaning Java code do not match. According to the code the arrow from Abstraction to Implementor is actually from RefinedAbstraction to Implementor.
Sample for C++ bridge pattern is missing.
Added code for C++ but als also derived another class from the Abstraction to demonstrate the interchangebility - the main purpose of this Design Pattern. For the C++ code, the aggregate from Abstraction to Implementor is correct. The code for C# and Java isn't (July 11th, 2006).
Vladimir Bosnjak 10:12, 12 July 2006 (UTC)
Made the C# code compilable, aggregate still incorrect.
Somebody added a line that the C++ code isn't the bridge pattern. It sure is - only more efficiënt implemented. The poster/editor noted that a variable is "pushed up" (whatever that means). Please note that I implemented more abstract abstractions. That's what abstract classes are for! Please, next time, start a tech discussion first before editing the related wiki-page. Vladimir Bosnjak 18:45, 22 September 2006 (UTC)
This article still seems to be too technical. That might be the case easely indeed for some or many. -->>What exactly needs to be done to make it more accessible? I could add a non-software image to visualize the "car example". I could add even more images, refining the steps. I could do a lot more in general. First of all I'd like to see a quick tutorial on how to add images to a wiki page. If someone is willing, please move to my talk-page and help me out with that part. Currently the "wiki manual" on this subject is overwhelming. My time is limited I'm affraid to read it all. I believe a detailed explanation on the details (e.g. code) is out of place. Yet I do agree that more detailed explanation is required. However, this needs teamwork. More insights on that later. Vladimir Bosnjak 20:15, 22 September 2006 (UTC)Vladimir Bosnjak 20:07, 22 September 2006 (UTC)
I recommend deleting the current "Car abstraction" example, as it is incorrect. A road is not an implementation of a car. For that matter, I would avoid using any automotive analogies here. Consider a real-world bridge-- i.e., a structure allowing a road (and therefore cars) to pass over an obstruction. This has little to do with the Bridge pattern, so examples involving cars and roads are likely to just confuse the reader. 14-JUN-2007 Mahler3 21:17, 15 June 2007 (UTC)
Nallap 11:54, 24 October 2007 (UTC)
OK, since the car example has persisted for many months despite it being incorrect and confusing, and no one has defended it, I finally decided to just get rid of it. Honestshrubber (talk) 18:41, 7 November 2008 (UTC)
Isn't an implementation of an interface supposed to be a dashed line with an white arrow head? Right now the ConcreteImplementor connects to the Implementor with a solid line with a white arrow head.
The Car example needs some changes as it does not explain the pattern practically. The change I would suggest is as follows.
Car is an abstraction and may be the refined abstraction of car can be Mercedes Make-I (e.g C-class) and Make-II (E-class).Then I totally disagree with Road as the implementation as it is too generic/amateuristic and not a very good real life example. I would suggest the actual implementation of the car can be Engine as it is the heart of any car. So refined implementation can be some thing like a Petrol Engine implementation and a diesel engine implementation. More refining can be done here something like 2-valve engine, 4,6,8 valve engines etc. Any feed back? Also this requires a total revamp of the examples in all languages. I can take care of C++ and C#. Let me know if this is OK.
I find it difficult to see the light when it comes to the Bridge Pattern. The most common example I see is the Shape example which is also used here. I can easily understand the example, but I find the solution using the Bridge Pattern complete silly.
Using the Bridge Pattern each shape instance is tied to a certain Drawing API. But that seems silly to me; why should shapes be bound to a media at all?
Many places including here the Bridge Pattern is related to working on several operating systems. But can you imagine that some of the shapes in a single running instance of program is on one operating system while others are on another operating system? I cannot, and therefore it seems strange that each shape should have an individual member signifying the operating system (using a Singleton pattern containg a strategy (cf. Strategy Pattern) seems more appropriate in this case).
If facing the problem of drawing using several different APIs in a single running instance of a program. I would use a different pattern (don't know if it has a name). Instead of tying each shape to a drawing API, I would supply the Drawing API to the draw operation. Using the C++ example the main code would look like this:
//The Client. // -->> int main() merely demonstrates the interchangebility. int main() { Shape* TheShape[4]; //2 shapes x 2 implementations = 4 client-options. DrawingAPI* DA[2]; //2 Implementations. DA[0] = new OpenGL; DA[1] = new DirectX;
TheShape[0] = new Circle(1, 2, 7.5); TheShape[0]->Draw(DA[0]); //Draw circle with OpenGL
TheShape[1] = new Circle(5, 7, 27.5); TheShape[1]->Draw(DA[1]); //Draw circle with DirectX
TheShape[2] = new Square(10, 14); TheShape[2]->Draw(DA[0]); //Draw square with OpenGL
TheShape[3] = new Square(2.9, 80); TheShape[3]->Draw(DA[1]); //Draw square with DirectX
//Give back allocated memory. delete DA[0], DA[1]; delete TheShape[0], TheShape[1], TheShape[2], TheShape[3];
return 0; }
Another reason that this makes more sense is that the "Drawing API" in this case could also represent the media, so that you can draw the same shapes in several windows and on a printer, etc.
While I beleive I understand the Bridge Pattern, I still lack an example that justifies its use.
Thomas Linder Puls, Prolog Development Center, Denmark
that's just retarded. Give one or two languages and leave it be. —The preceding unsigned comment was added by 169.231.11.44 (talk) 06:04, 27 February 2007 (UTC).
I don't think the sample adequately defines the bridge pattern, it feel more like a Strategy pattern (Anon) —Preceding unsigned comment added by 98.111.71.111 (talk) 04:44, 4 March 2009 (UTC)
I think it is good to provide example in different languages, because understanding of structure or pattern depends on your understanding of implemented language, we can always refer to example in language that we are familiar with, and of course we don’t have to look at all different language example, i.e. I am more comfortable with JAVA so I usually jump to java example, which give me enough understanding of pattern, i don’t have to look at python example if i don’t know python. —Preceding unsigned comment added by Shaikh sharique (talk • contribs) 08:31, 12 May 2010 (UTC)
A single good example in pseudocode would be ideal. All software engineers should understand pseudocode (assuming it's well written of course!). As for what a good example might be - no idea either! The best explanation of the pattern I've seen was on youtube (Christopher Okhravi – Bridge Pattern – Design Patterns (ep 11)), but even then it just seemed like what you would naturally do rather than any special pattern. Which might be absolutely right; we might instinctively reach to code in ways that constitute a design pattern. 2A02:C7F:DCA4:7A00:8C22:E3B2:66AD:8724 (talk) 14:14, 25 March 2021 (UTC)
I deleted many of the examples. Erlang isn't that popular (yet)(according to wikipedia), and c#, c++, and java are industry standards and university standards. —Preceding unsigned comment added by Michael miceli (talk • contribs) 06:08, 6 April 2008 (UTC)
Commentary:
"c# and java are industry standards and university standards" which means that they are the most important. What you did is wrong (at least in this respect). —Preceding unsigned comment added by 195.72.132.1 (talk) 12:27, 5 May 2008 (UTC)
I vote to leave only a Java example in place. C# programmers can easily understand the syntax (I'm a C# programmers by the way - not a Java one). This isn't a programming tutorial - so too much emphasis on implementation distracts from the point and clutters the article. LivingAndLearning (talk) 13:16, 8 October 2008 (UTC)
The c++ example has many problems. It has straight forward bugs (leaks) and some very controvertial coding habits (using namespace std;), but it's not all, the whole design doesn't fit c++. Seems to have been written by a programmer of a GC language with no c++ experience. I cannot fix it because I don't exactly understand the purpose of this pettern, but it is clearly not supposed to be done this way. --Itaj Sherman (talk) 12:44, 24 February 2009 (UTC)
The bridge pattern is useful when not only the class itself varies often, but the class does also
Is there a typo in there? It effectively says 'when not only X but also X'! —Preceding unsigned comment added by 80.169.133.251 (talk) 10:47, 20 February 2009 (UTC)
I am not familiar with java that well- so maybe it's not a mistake, but: it is written:
interface Shape {
public void draw(); // low-level
public void resizeByPercentage(double pct); // high-level
}
class CircleShape implements Shape {
private double x, y, radius;
private DrawingAPI drawingAPI;
}
but the "private DrawingApi drawingAPI" should be a member of the Shape interface. that's exactly the association (aggregation) from the Abstraction and the Implementor. or in other words, the implementor is a member of the abstraction, not of the refined abstractions. so it should be:
interface Shape {
public void draw(); // low-level
public void resizeByPercentage(double pct); // high-level
private DrawingAPI drawingAPI; //NEW LINE!!
}
This is my first time i write something in Wikipedia. always was a reader. i hope i did it right :-) Nirhur (talk) 18:00, 20 May 2009 (UTC)
Smok x (talk) 21:02, 13 July 2010 (UTC) Current Java example is not quite correct. One of the benefits of Bridge pattern is that clients do not need to know the exact implementation class. In the example the client explicitly instantiates DrawingAPI1 or DrawingAPI2. Shape should be abstract class instead of interface, and Shape must choose which implementation to use:
public abstract class Shape {
Shape() {
// Choose implementation here. Clients don't know anything about it.
drawingAPI = DrawingAPIFactory.getInstance().getDrawingAPI();
}
public abstract void draw();
public abstract void resizeByPercentage(double pct);
protected DrawingAPI getDrawingAPI(); // For subclasses.
private DrawingAPI drawingAPI;
}
I don't think there is a need for two Perl Moose examples so I moved this one to the discussion page. If there is more than one Perl example then one should probably use just core modules. Argel1200 (talk) 19:19, 12 August 2009 (UTC)
# Implementor
package Drawing::API;
use Moose::Role;
requires 'draw_circle';
# Concrete Implementor 1
package Drawing::API::1;
use Moose;
with 'Drawing::API';
sub draw_circle {
my $self = shift;
printf "API1.circle at %f:%f radius %f\n" => @_;
}
# Concrete Implementor 2
package Drawing::API::2;
use Moose;
with 'Drawing::API';
sub draw_circle {
my $self = shift;
printf "API2.circle at %f:%f radius %f\n" => @_;
}
# Abstraction
package Shape;
use Moose::Role;
requires qw( draw resize );
# Refined Abstraction
package Shape::Circle;
use Moose;
with 'Shape';
has $_ => ( is => 'rw', isa => 'Any' ) for qw( x y r );
has api => ( is => 'ro', does => 'Drawing::API' );
sub draw {
my $self = shift;
$self->api->draw_circle( $self->x, $self->y, $self->r );
}
sub resize {
my ($self, $percentage) = @_;
$self->{r} *= $percentage;
}
# Client
package main;
my @shapes = (
Shape::Circle->new( x=>1, y=>2, r=>3, api => Drawing::API::1->new ),
Shape::Circle->new( x=>5, y=>7, r=>11, api => Drawing::API::2->new ),
);
$_->resize( 2.5 ) and $_->draw for @shapes;
I don't know if all the code examples are the same, but the C++ example code doesn't match the description. The "maintains reference to the implementation" is in the wrong class, so it does not follow the pattern.
—Długosz (talk) 16:12, 9 March 2010 (UTC)
As some others have indicated, this seems to be describing the Strategy pattern, not the Bridge pattern. With the Strategy pattern you have a class with some functionality that needs to get implemented. The functionality is farmed off to a separate class who implements the functionality. With a Bridge pattern, you have an interface that must be implemented. You have multiple classes that implement that interface and you choose the correct one for your circumstances. It is often (almost always?) used in conjunction with an abstract factory.
The difference is subtle. The Strategy patten implements functionality that is plugged into an object. It is often a single method, or a group of methods that are used by the original object. For example, I might have a print method that prints to a screen and a print method that prints to a printer. The Bridge pattern implements an entire interface. So for instace if I am implementing a GUI I'll have a Swing version of my object and an Android version of my object. The bridge connects the abstraction to the implementation.
Mikekchar (talk) 02:27, 7 June 2012 (UTC)
What is the purpose for the IAbstractBridge inteface in the C# example? I see no techical or didactical reason for not just creating the AbstractBridge directly, with no interface. 5.23.32.98 (talk) 18:07, 13 January 2017 (UTC)
The Bridge [1] design pattern is one of the twenty-three well-known GoF design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.
What problems can the Bridge design pattern solve? [2]
When using subclassing, an implementation is selected at compile-time and can't be changed at run-time.
What solution does the Bridge design pattern describe?
Abstraction) from its implementation (Implementor) by putting them in separate class hierarchies.Abstraction in terms of (by delegating to) an Implementor object.This enables to configure an Abstraction with an Implementor object at run-time.
See also the UML class and sequence diagram below.
Vanderjoe (talk) 12:29, 1 September 2017 (UTC)
References
{{cite book}}: CS1 maint: multiple names: authors list (link)
I suggest that attention be drawn to the following:
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.