Skip to main content
added 6 characters in body
Source Link
Errorsatz
  • 776
  • 3
  • 7

If I'm understanding correctly, you want to support some operations on the inner array, but not others. If this set of supported operations is general enough for a D2ArrayWrapper to make sense, then I would suggest using an interface.

For example, let's say you want people to be able to read cells and rows from the array, but not write them:

interface IReadOnlyD2Array<T>
{
   T getCell(int row, int col);
   IReadOnlyList<T> getRow(int row);
   IReadOnlyList<T> getColumn(int col);
}

That's in C#, but the same principle applies in any OO language. Then you have your D2Array implement that interface, and return it via that instead.

private D2Array<T> _internalArray;
public IReadOnlyD2Array<T> Array => _internalArray;

If you're implementing a library for public consumption, you may want to create a wrapper that enforces the read-only nature so people can't force-cast it back to D2Array, but otherwise just don't do that.

-

An alternate approach, if you don't have the ability to change the class involved, or are using C++ and don't want the overhead of virtual calls, is to make a "view" class that encapulates D2Array and only exposes the methods you want:

template <typename T>
class D2ArrayView 
{
   public:
      D2ArrayView(const D2Array<T> &srcArray) : array(srcArray) { }
      
      T getCell(int row, int col) const { return array.getCell(row,col); }
      // etc ...

   private:
      const D2Array<T> &array;
}

If I'm understanding correctly, you want to support some operations on the inner array, but not others. If this set of supported operations is general enough for a D2ArrayWrapper to make sense, then I would suggest using an interface.

For example, let's say you want people to be able to read cells and rows from the array, but not write them:

interface IReadOnlyD2Array<T>
{
   T getCell(int row, int col);
   IReadOnlyList<T> getRow(int row);
   IReadOnlyList<T> getColumn(int col);
}

That's in C#, but the same principle applies in any OO language. Then you have your D2Array implement that interface, and return it via that instead.

private D2Array<T> _internalArray;
public IReadOnlyD2Array<T> Array => _internalArray;

If you're implementing a library for public consumption, you may want to create a wrapper that enforces the read-only nature so people can't force-cast it back to D2Array, but otherwise just don't do that.

-

An alternate approach, if you don't have the ability to change the class involved, or are using C++ and don't want the overhead of virtual calls, is to make a "view" class that encapulates D2Array and only exposes the methods you want:

template <typename T>
class D2ArrayView 
{
   public:
      D2ArrayView(const D2Array<T> &srcArray) : array(srcArray) { }
      
      T getCell(int row, int col) { return array.getCell(row,col); }
      // etc ...

   private:
      const D2Array<T> &array;
}

If I'm understanding correctly, you want to support some operations on the inner array, but not others. If this set of supported operations is general enough for a D2ArrayWrapper to make sense, then I would suggest using an interface.

For example, let's say you want people to be able to read cells and rows from the array, but not write them:

interface IReadOnlyD2Array<T>
{
   T getCell(int row, int col);
   IReadOnlyList<T> getRow(int row);
   IReadOnlyList<T> getColumn(int col);
}

That's in C#, but the same principle applies in any OO language. Then you have your D2Array implement that interface, and return it via that instead.

private D2Array<T> _internalArray;
public IReadOnlyD2Array<T> Array => _internalArray;

If you're implementing a library for public consumption, you may want to create a wrapper that enforces the read-only nature so people can't force-cast it back to D2Array, but otherwise just don't do that.

-

An alternate approach, if you don't have the ability to change the class involved, or are using C++ and don't want the overhead of virtual calls, is to make a "view" class that encapulates D2Array and only exposes the methods you want:

template <typename T>
class D2ArrayView 
{
   public:
      D2ArrayView(const D2Array<T> &srcArray) : array(srcArray) { }
      
      T getCell(int row, int col) const { return array.getCell(row,col); }
      // etc ...

   private:
      const D2Array<T> &array;
}
added 519 characters in body
Source Link
Errorsatz
  • 776
  • 3
  • 7

If I'm understanding correctly, you want to support some operations on the inner array, but not others. If this set of supported operations is general enough for a D2ArrayWrapper to make sense, then I would suggest using an interface.

For example, let's say you want people to be able to read cells and rows from the array, but not write them:

interface IReadOnlyD2Array<T>
{
   T getCell(int row, int col);
   IReadOnlyList<T> getRow(int row);
   IReadOnlyList<T> getColumn(int col);
}

That's in C#, but the same principle applies in any OO language. Then you have your D2Array implement that interface, and return it via that instead.

private D2Array<T> _internalArray;
public IReadOnlyD2Array<T> Array => _internalArray;

If you're implementing a library for public consumption, you may want to create a wrapper that enforces the read-only nature so people can't force-cast it back to D2Array, but otherwise just don't do that.

-

An alternate approach, if you don't have the ability to change the class involved, or are using C++ and don't want the overhead of virtual calls, is to make a "view" class that encapulates D2Array and only exposes the methods you want:

template <typename T>
class D2ArrayView 
{
   public:
      D2ArrayView(const D2Array<T> &srcArray) : array(srcArray) { }
      
      T getCell(int row, int col) { return array.getCell(row,col); }
      // etc ...

   private:
      const D2Array<T> &array;
}

If I'm understanding correctly, you want to support some operations on the inner array, but not others. If this set of supported operations is general enough for a D2ArrayWrapper to make sense, then I would suggest using an interface.

For example, let's say you want people to be able to read cells and rows from the array, but not write them:

interface IReadOnlyD2Array<T>
{
   T getCell(int row, int col);
   IReadOnlyList<T> getRow(int row);
   IReadOnlyList<T> getColumn(int col);
}

That's in C#, but the same principle applies in any OO language. Then you have your D2Array implement that interface, and return it via that instead.

private D2Array<T> _internalArray;
public IReadOnlyD2Array<T> Array => _internalArray;

If you're implementing a library for public consumption, you may want to create a wrapper that enforces the read-only nature so people can't force-cast it back to D2Array, but otherwise just don't do that.

If I'm understanding correctly, you want to support some operations on the inner array, but not others. If this set of supported operations is general enough for a D2ArrayWrapper to make sense, then I would suggest using an interface.

For example, let's say you want people to be able to read cells and rows from the array, but not write them:

interface IReadOnlyD2Array<T>
{
   T getCell(int row, int col);
   IReadOnlyList<T> getRow(int row);
   IReadOnlyList<T> getColumn(int col);
}

That's in C#, but the same principle applies in any OO language. Then you have your D2Array implement that interface, and return it via that instead.

private D2Array<T> _internalArray;
public IReadOnlyD2Array<T> Array => _internalArray;

If you're implementing a library for public consumption, you may want to create a wrapper that enforces the read-only nature so people can't force-cast it back to D2Array, but otherwise just don't do that.

-

An alternate approach, if you don't have the ability to change the class involved, or are using C++ and don't want the overhead of virtual calls, is to make a "view" class that encapulates D2Array and only exposes the methods you want:

template <typename T>
class D2ArrayView 
{
   public:
      D2ArrayView(const D2Array<T> &srcArray) : array(srcArray) { }
      
      T getCell(int row, int col) { return array.getCell(row,col); }
      // etc ...

   private:
      const D2Array<T> &array;
}
Source Link
Errorsatz
  • 776
  • 3
  • 7

If I'm understanding correctly, you want to support some operations on the inner array, but not others. If this set of supported operations is general enough for a D2ArrayWrapper to make sense, then I would suggest using an interface.

For example, let's say you want people to be able to read cells and rows from the array, but not write them:

interface IReadOnlyD2Array<T>
{
   T getCell(int row, int col);
   IReadOnlyList<T> getRow(int row);
   IReadOnlyList<T> getColumn(int col);
}

That's in C#, but the same principle applies in any OO language. Then you have your D2Array implement that interface, and return it via that instead.

private D2Array<T> _internalArray;
public IReadOnlyD2Array<T> Array => _internalArray;

If you're implementing a library for public consumption, you may want to create a wrapper that enforces the read-only nature so people can't force-cast it back to D2Array, but otherwise just don't do that.