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 9182815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:39:05+00:00 2026-06-17T18:39:05+00:00

I have 2 already defined classes, we’ll call them DogActivityType and HorseActivityType . They

  • 0

I have 2 already defined classes, we’ll call them DogActivityType and HorseActivityType.
They have the same fields, same methods, but they write to 2 different tables in the database, and of course, are named differently.

I have a function with all the business rules already working for one class, and the other class uses the same business rules.

Restrictions:

  1. I have to use these 2 classes since they are used throughout other parts of the project
  2. I can’t create one class and add another column (field) to distinguish between both types of classes.
  3. I can’t edit the source for these 2 classes.

.
Here is my simplified version of the code:

public doAllCalculations(){
  // retrieve collection
  foreach (DogActivityType activity in allActivities){
    // a lot of code here

    // more code... 

    createMoreOf(activity);  // this is overloaded since it needs to know which DB to write to
  }
}

// using overload for same function name
private createMoreOf(DogActivityType doggyActivity){
    /// do some specific when it is a dog
}

private createMoreOf(HorseActivityType horse){
    /// do some specific when it is a horse
}

Now, the problem is: doAllCalculations() is very extensive and complicated and may change during the course of development. I don’t want to have 2 different functions (doAllCalculationsDOG() and doAllCalculationsHORSE() ) in order to do the same analysis, just because I need one for the Dog and another for the Horse class. One day someone in the project may forget to update both functions or any other bad scenario…

So, I want to use the same function for both classes. So if I edit a rule in the big calculation function, I will know it works for both classes.
I’m thinking I’ll end up with something like this:

public class AnimalActityType {

}

public doAllCalculations(){
  // retrieve collection
  foreach (AnimalActivityType activity in allActivities){
    // a lot of code here
    // more code... 
    createMoreOf(activity); 
  }
}

AnimalActityType will simulate an abstract parent, I’ll call it reverse polymorphism…
But how do DogActityType and HorseActityType know about this parent? Can I force the parent?
Is it possible? Any ideas?

  • 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-06-17T18:39:06+00:00Added an answer on June 17, 2026 at 6:39 pm

    you can try to use decorator pattern, but in very unusual way.

        class Decorator
    {
        private object instance;
    
        public Decprator(object instance)
        {
             this.instance = instance;
        }
    
        public <type> SomeCommonProp
        {
          get{
            if(instance is DogActivityType)
            {
              return (instance as DogActivityType).SomeValueOrPropertyOrCall;
            }
            else
            {
              return (instance as HorseActivityType).SomeValueOrPropertyOrCall; 
            }
          }
        }
    }
    
    
    class MyCalculations
    {
      private Decorator instance;
    
      public MyCalculations(Decorator inst)
      {
          instance = inst;
      }
    
      public <type> SomeCalculationMethod()
      {
        // here you will use instance.SomeCommonProp for your calculations
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table of classes already defined, but one request has been to
i have a object in javascript and some already defined functions. but how can
I have a Java array defined already e.g. float[] values = new float[3]; I
I have already googled and searched stackoverflow but I can find nothing related to
I have already tried multiple ways. defaults write com.apple.Xcode PBXCustomTemplateMacroDefinitions '{ORGANIZATIONNAME=YourNameHere;}' Also i've tried
I have already read some posts, but no one helped me with my problem.
I have already seen a post about the error but This is some what
I have 2 classes defined in the .h file of my app class GroupElement
I know there have been a lot of questions on this topic already, but
I have already pushed all the changes to the server. Here is the order

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.