How can I define some rules or additional effects of an action and select it dynamically?

For example, let's imagine a FinancialProduct, which can be a Share or a Bond, each of them having a Hedge. Now while there are several different kind of Hedge, their number is still finite and known beforehand, it is just the Hedge / FinancialProduct association that can be chosen at runtime (but creating new rulesets at runtime can be interesting too, but one question at a time).

How could I implement this kind of behaviour ?

If the Hedge business is implemented in a different class, how can it know FinancialProduct properties and eventually modify them for example? Or modify a property from a different class (a Portfolio which contains a list of owned or available FinancialProducts for exemple?)

  • A Hedge, is a FinancialProduct, is it not? A hedge is just the use of that financial product (just asking for clarification before I answer) – TheCatWhisperer Jun 13 '17 at 14:37
  • @TheCatWhisperer hedge is usually a financial derivative product, but in this question i don't bother. We can just consider the hedge as a ruleset that will influence the underlying asset properties (FinancialProduct properties) or how the underlying asset is handled. For a more concrete example, some hedge will change the price if selling before a given date or fordid the selling action under a certain price or before a certain date. My apologies if I wasn't clear. Finance knowledge isn't required here, it is purely an OOP question i believe, i could have transposed in another example. – Ythio Csi Jun 13 '17 at 14:49
  • Not sure what you asking here - an object X (maybe a hedge) knows an object Y (the financial product it belongs to) by holding a reference to it, so it can modify the financial product object's properties (=attributes) for example by assigning a new value to a property. Or do you mean "property" in a completely different sense? Maybe you mean "modify the financial product object's behaviour"? Please clarify. – Doc Brown Jun 13 '17 at 14:59
  • Financial knowledge is needed to properly assess the architecture of a financial application ;) – TheCatWhisperer Jun 13 '17 at 15:05
  • @DocBrown You have an object financial product, it holds its risk insurance contract type and you want to enforce the terms of this contract (the contract is a product itself) Those terms can modifying your financial product attributes, or forbidding existing (or allowing new) behaviors to the objects that manipulate financialproducts. is it clearer this way ? – Ythio Csi Jun 13 '17 at 15:34

First off, let me start by saying. Having a FinacialProduct base class is a recipe for disaster. If you think about, options, bonds, and stocks, really don't have much in common. The only thing they have in common are things you can do to them, such as being bought, sold, ect. To enable polymorphism in these common behaviors, you should define interfaces: ICanBeBought, ICanBeSold, ICanCheckIfAllowedToBeSold, ICanBeHedged, ect. If you do have a FincialProduct base class, it should be minimized to only having a FincialProductType property.

A hedge is basically just a link between two financial products:

public class Hedge
      private IDynamicStrategy Rule {get; set;}//Change the rules dynamically here
      public DateTime CreateDate {get;set;}
      public ICanBeHedged Product1 {get;set;}
      public ICanBeHedged Product2 {get;set;}
      //The Hedge class knows about both of its financial products because it has references to them.

     public bool FollowsDynamicRules()
         return Rule.IsAllowed(Product1, Product2);

public class Bond : ICanBeHedged, ICanBeBought, ICanBeSold
     public bool CanIDoSomething(Hedge parentHedge)
         //Now I know about my parent hedge where I need to! However, I don't have a reference to my parent hedge defined as a field/property because it is not a inherent part of me. Maybe I don't need to know about a hedge at all and this belongs in the Hedge class?
  • Composition over Inheritance ? But where do you hold your financial product state then (your price, duration, etc...) ? I was thinking about Single Responsability, financial product and its child classes only hold states, and eventually trigger events when certain actions are taken. A controller knows all rules and handle the events ? – Ythio Csi Jun 13 '17 at 15:36
  • I was just using this as an exmple, not an exact design. In truth, the above design will give you issues, you should probablly be dealing with FincialPositions NOT FinancialProducts in this context. A client views and purchases FincialProducts which results in them having FincialPositions, which are what the hedges will reference – TheCatWhisperer Jun 13 '17 at 15:41
  • Yes, I prefer composition over inheritance. However, regardless, neither is appropriate for finical products. They are far too different to be linked so strongly. – TheCatWhisperer Jun 13 '17 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.