Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Design Patterns

Posted on 2000-05-14
5
241 Views
Last Modified: 2013-12-03
I have written my own string library in C++ and have been asked to apply the bridge pattern to it in order to split the interface of the string class from its implementation.

I am unsure exactly how this is done and would appreciate some guidance on what would be envolved.  My knowledge of C++ is limited at this time, but I am trying to learn it.
0
Comment
Question by:checkin
  • 3
  • 2
5 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2808555
This is not the best example for the bridge pattern.  If this is for an assigment, I don't think it was well-conceived.

I guess in this case the best you can do is to provide an abstract base class that performs "primitive" operations--very simple operations on the string.  A few I could think of are

*set string length.
*get string length.
*set character at specified position.
*get character at specified postion.

These 4 operations are probalby the only ones you have to have, but a few more might make things easier.  However the idea is to keep the number of operations, and their complexify down.  And note that these operations are to be generic in nature, i.e. they should be general enough that they can work for any string implimentation.  So for example an operation that retreived a string's reference count would not be permitted, because some implimentations might not use a reference count.  An operation to set value of the terminator character used to mark the end of the string would also not be allowed, since some implimentations might not use a terminator character to mark the end of the string.

continues
0
 
LVL 22

Expert Comment

by:nietod
ID: 2808569
Now from this base class you need to define 1 or more (preferably at least 2 or this whole exercise is pointless) concrete classes.  These classes define  the operations declared in the base class.  Each class should be using a different implimentation for the string.  2 I can think of is that one class might use a NUL (0) character to mark the end of the string while the other class might instead store the length of the string as integer in oen of its data members.  

Now your string class needs to store a pointer to a bridge object,  The type of the pointer will be declared as a pointer to an object of the base bridge class, but actually the object it points to will be one of the derived classes.

Now all the operations in the string class that need to access the string in some way, need to do so through this bridge object.  i.e if the string class has a function that returns the string's length, the function now needs to call the "get length" function of the bridge object.  if the string class has a operator [] function that returns the character at a specitied postion, then the function must be written to use the bridge's "get character at specified position" function.

Let em know if you have any questions.
0
 

Author Comment

by:checkin
ID: 2836209
Hi !

I have read your comments, I think I understand but I am unsure of the class hierachy, how would the classes be organised ?

Would it be possible to provide a shell framework ?
0
 
LVL 22

Expert Comment

by:nietod
ID: 2836489
Why don't you try it and post what you get done?   Start with the abstract class for the bridge--this is a class with 4 undefined pure virtual functions.   Then derive a class or two from this that impliments the 4 functions.   Then make your string class use this class.
0
 

Author Comment

by:checkin
ID: 2836882
ok thanks, I will try and post something to you within the next day.

Marvin.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question