The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Awkward design pattern

I have an awkward design pattern.  I'll explain below with references to my code example.

I wish to have a class (DataProvider) that crunches data based on a definition (AbstractDefinition) of that data.  The algorithm for crunching the data is known and can be coded and forgotten.  So, I just plug in any ConcreteDefintion and I am set. Note that I wish to retrieve individual data items from the DataProvider (getItem) by using an enumeration (TheItems::Items) to identify the items. 

The enumeration makes this awkward.

I shall reuse DataProvider by feeding DataProvider different defintions and corresponding enumerations. This seems awkward: The code in DataProvider will not change, yet to reuse it I must compile with different versions of the enumeration.  Note that I have the same situation with AbstractDefinition.

Does this seem awkward to you folks?  If so, does anyone have a suggestion for a different design?  To me, it makes sense to separate the algorithm from the defintion.  I also want to identify my items with an enumeration (though I am flexible here).  The pattern just doesn't feel right. 

Any suggestions are welcome.




class TheItems
{
public:
  enum Items
  {
      ITEM1,
      ITEM2
  };

  TheItems() {}
  TheItems() {}
};


//*** ItemDef.hpp ***
struct ItemDef
{
public:
  // whatever defines an item
};

//*** AbstractDefinition.hpp ***
class AbstractDefinition
{
public:
  AbstractDefinition() {}
  virtual ~AbstractDefinition() {}

  virtual ItemDef getItemDef(TheItems::Items item) = 0;
}

//*** ConcreteDefinition.hpp ***
class ConcreteDefinition : public AbstractDefinition
{
public:
  ConcreteDefinition() {}
  virtual ~ConcreteDefinition() {}

  virtual ItemDef getItemDef(TheItems::Items item)
  {
      // return ItemDef struct for 'item'
  }
}

//*** ItemProvider.hpp ***
class ItemProvider
{
public:
  ItemProvider(AbstractDefinition& definition):
      m_definition(definition)
  {
  }

  ~ItemProvider() {}

  SomeType getItem(TheItems::Items item)
  {
      // return item value based on supplied definition (m_definition)
  }

private:
  AbstractDefinition& m_definition;

}
Dave K Send private email
Thursday, July 10, 2008
 
 
You could change the argument from 'TheItems::Items' to an integer and pass in a 'TheItems::Items' value anyway.
dev1
Friday, July 11, 2008
 
 
Take a look at my post here:

http://discuss.joelonsoftware.com/default.asp?design.4.650311.17

I have a problem with enumerations (not the same case you have, but very close) and ended not using them but creating my own enumeration class to allow extensions.

The thread is rather difficult to follow, but take a look at my second post, where I describe the solution. If it seams to  you it could help you, please let me know and I would try to clarify any doubt.
Pablo Send private email
Sunday, July 13, 2008
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz