This is a discussion on Common Interface to I/O within the Software Patterns forums, part of the Testing category; Hi, I'm new to software patterns and I am trying to apply it to my C++ project. For my ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Common Interface to I/O
Hi,
I'm new to software patterns and I am trying to apply it to my C++ project. For my project I have a class called Protocol, I'm just playing with my own protocol. At the moment my Protocol class can write to two different I/O. I want to make my Protocol not care about the type of I/O just to read, write, open, and close the I/O. It shouldn't matter if it the network or usb. At the present, I created an abstract I/O class that contains read, write, open, and close. Then I created two I/O classes, CNetwork and CUSB that inherit from this abstract class and define the implementation. Here is an abstract example how I create my io and pass it to my protocol class abstractIO* m_io; m_io = new CNetwork(...); // or new CUSB( ) CProtocol protocol(m_io); I was wondering what other technique can be used to make my Protocol class more indepedent on the I/O. Thanks Danny |
|
|||
|
Re: Common Interface to I/O
you did a fine job of using the strategy pattern to encapsulate the basic
objects that you intend to use. You may want to look at using a factory pattern to encapsulate the creation of your object as well, so that the calling code is not bound to the static object that represents the specific I/O method that your configuration would indicate. -- --- Nick Malik [Microsoft] MCSD, CFPS, Certified Scrummaster http://blogs.msdn.com/nickmalik Disclaimer: Opinions expressed in this forum are my own, and not representative of my employer. I do not answer questions on behalf of my employer. I'm just a programmer helping programmers. -- <fernandez.dan@gmail.com> wrote in message news:1125083906.263857.157390@g47g2000cwa.googlegroups.com... > Hi, > > I'm new to software patterns and I am trying to apply it to my C++ > project. For my project I have a class called Protocol, I'm just > playing with my own protocol. At the moment my Protocol class can write > to two different I/O. > > I want to make my Protocol not care about the type of I/O just to read, > write, open, and close the I/O. It shouldn't matter if it the network > or usb. > > At the present, I created an abstract I/O class that contains read, > write, open, and close. Then I created two I/O classes, CNetwork and > CUSB that inherit from this abstract class and define the > implementation. > > Here is an abstract example how I create my io and pass it to my > protocol class > > abstractIO* m_io; > m_io = new CNetwork(...); // or new CUSB( ) > CProtocol protocol(m_io); > > > I was wondering what other technique can be used to make my Protocol > class more indepedent on the I/O. Thanks > > Danny > |
|
|||
|
Re: Common Interface to I/O
Thanks for the input Nick,
I was looking at the factory pattern to create my object. Also I was also looking at using a smart pointer that the factory would instantiate for the caller. It would be something like this IoPtr<abstractIO> m_io( new CNetwork(..) ); for my Network I/O. The Factory class would determine which to instantiate the CNetwork or CUsb eventually giving me something like this IoPtr<abstractIO> m_io = Factory.CreateIO(/*field to determine IO*/); CProtocol protocol(m_io); Thanks for any input. Danny |