Class architecture, no friends allowed

Posted by Captain Comic on Stack Overflow See other posts from Stack Overflow or by Captain Comic
Published on 2010-04-26T09:40:24Z Indexed on 2010/04/26 9:43 UTC
Read the original article Hit count: 175

The question of why there are no friends in C# has been extensively discussed.

I have the following design problems.

I have a class that has only one public function AddOrder(Order ord). Clients are allowed to call only this function. All other logic must be hidden. Order class is listening to market events and must call other other function of TradingSystem ExecuteOrder, so I have to make it public as well. Doing that I will allow clients of Trading system to call this function and I don't want that.

class TradingSystem
{
 // Trading system stores list of orders
  List<Order> _orders;

 // this function is made public so that Order can call ir  
  public ExecuteOrder(Order ord)
{
}  
  // this function is made public for external clients
  public AddOrder(OrderRequest ordreq)
 {
   // create order and pass it this 
   order.OnOrderAdded(this);
 }
}

class Order
{
  TradingSystem _ts;

  public void OnOrderAdded(TradingSystem ts)
{
   _ts = ts;
}

void OnMarketEvent()
{
  _ts.ExecuteOrder()
}  
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about object-oriented-design