Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6339121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:35:10+00:00 2026-05-24T19:35:10+00:00

Here is my problem domain in the financial industry : Asset ========== Asset parent

  • 0

Here is my problem domain in the financial industry :

Asset
==========
Asset parent
assetID
+ compareTo(Asset anotherAsset)

Portfolio : Asset
-----------------
name
risk limit
List stocks
+ compareTo(Asset anotherAsset)
+ composite.CompareTo(Portfolio, ComparisonRules).

Stock : Asset
-------
market
amount
company
+ compareTo(Asset anotherAsset)

AnotherStock : Stock
--------------------
someOtherProperty
+ compareTo(Asset anotherAsset)

I have applied the composite pattern to structure *Stock*s within *Portfolio*s. I want to have a clean way of customizing the compareTo method of this composite. That is, AnotherStock will always be compared to another AnotherStock, Stocks to Stocks. This looks like the strategy pattern to me.

I would like to do something like the following (psuedocode)

differences = composite.CompareTo(anotherComposite, ComparisonRules).

composite.CompareTo would be something like :

ComparisonRules.Compare(this.Stocks[currentAssetID], otherComposite[currentAssetID])

ComparisonRules.Compare(Asset a, Asset b) would do something ugly like this :

if( a is Stock and b is Stock) : convert to stock and do stock-based comparison
else if (a is AnotherStock and b is AnotherSTock): convert to AnotherStock 

Is there a way to write ComparisonRules in such a way that I don’t have to downcast yet still provide a custom ComparisonRules object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T19:35:11+00:00Added an answer on May 24, 2026 at 7:35 pm

    From a rule perspective, it sounds like what you need are generics. If you define something along these lines:

    public class ComparisonRule<TStock> where TStock : Stock
    {
        public int Compare(TStock lValue, TStock rValue)
        {
            ...
        }
    }
    

    That will guarantee that only types at or below TStock will be accepted. For example, if I had a ComparisonRule<AnotherStock>, then only types at or below AnotherStock could be passed in. However, you may want to rethink your type hierarchy if you want to be able to define a rule that can compare Stock but not AnotherStock. You should consider having a common ancestor, but the concrete stock types should be in different inheritance trees.

    In other words, you have this:

                  Stock
                    |
        --------------------------
       |                          |
    OneStock                AnotherStock 
    

    This would allow you to define a rule that could compare any Stock as ComparisonRule<Stock>, or a rule that can only compare OneStock as ComparisonRule<OneStock>.

    This, however, doesn’t help you sort out how to know which Stock objects to pass to which rules at a higher level. For that, you’ll need to be able to define a less-specific version of ComparisonRule We can do that with an interface:

    public interface IComparisonRule
    {
        bool CanCompare(Stock lValue, Stock rValue);
        int Compare(Stock lValue, Stock rValue);
    }
    
    public abstract class ComparisonRule<TStock> : IComparisonRule where TStock : Stock
    {
        bool IComparisonRule.CanCompare(Stock lValue, Stock rValue)
        {
            return lValue is TStock && rValue is TStock;
        }
    
        int IComparisonRule.Compare(Stock lValue, Stock rValue)
        {
            return Compare((TStock)lValue, (TStock)rValue);
        }
    
        public abstract int Compare(TStock lValue, TStock rValue);
    }
    

    Now, strictly speaking, your question asked how to do this without downcasting, which (again, strictly speaking) isn’t possible. This, however, should save you from having to do that with every implementation. For example, a simple rule to compare two AnotherStock instances would be:

    public class MyRule : ComparisonRule<AnotherStock>
    {
        public override int Compare(AnotherStock lValue, AnotherStock rValue)
        {
            return lValue.someOtherProperty.CompareTo(rValue.someOtherProperty);
        }
    }
    

    At the higher level (i.e. within Portfolio), you can simply hold on to a list of IComparisonRule as your rules, then you can call CanCompare and pass in two Stock instances to see if it’s a valid comparison, then pass them into Compare in order to perform the comparison.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So here is my problem. I want to redirect name.domain.com/trips/1 to domain.com?username=name&trip=1 using modrewrite.
Problem: to get the command working here. My domain is http://cs.edu.com/user/share_dir , but I
Bit of an abstract problem here. I'm experimenting with the Domain Model pattern, and
Here is my problem: Domain: I have following Entities: [Sensor] that can be positioned
The problem here is how to add an NSPopUpButton in Xcode 4's Interface Builder
Maddening problem here. When my page loads: <body onload=getClientDateTime();> It runs this function: document.getElementById('ClientDateTime').value=hello
date here my problem: String datetime = 2012-03-24 23:20:51; I know that that string
Super weird problem here. I'm having trouble getting jQuery to bind any selectors except
I have a problem here im trying to upload a file first time it
Im having a problem here. i downloaded jake wharton. He gave an example of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.