Class architecture, no friends allowed
- by Captain Comic
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()
}
}