Solved

Design Patterns

Posted on 2000-05-14
5
231 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now