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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:20:20+00:00 2026-05-22T20:20:20+00:00

I am trying to decide the best way to structure some code. I will

  • 0

I am trying to decide the best way to structure some code. I will admit this may be overboard and is turning into something more academic than practical. Sometimes you just cannot help yourself.

Let me contrive a simple example:

Suppose you have classes/interfaces such as:

interface IProcessedPhoto { }

interface IPhotoProcessor
{
    IProcessedPhoto Process(byte[] bytes);
    void Alter(IProcessedPhoto processedPhoto);
}

class PhotoProcessedWithAMethod : IProcessedPhoto { }

class PhotoProcessedWithBMethod : IProcessedPhoto { }

class AProcessor : IPhotoProcessor
{ 
    IProcessedPhoto Process(byte[] bytes);  // Returns PhotoProcessedWithAMethod 
    void Alter(IProcessedPhoto processedPhoto)
    {
        var casted = processedPhoto as PhotoProcessedWithAMethod;
        // a "B" would crash here.
    }
}

class BProcessor : IPhotoProcessor
{ 
    IProcessedPhoto Process(byte[] bytes);  // Returns PhotoProcessedWithBMethod  
    void Alter(IProcessedPhoto processedPhoto)
    {
        var casted = processedPhoto as PhotoProcessedWithBMethod;
        // an "A" would crash here.
    }
}

class Algorithm
{
    void DoStuff()
    {
        var processor = ProcessorFactory.CreateProcessor(//stuff);
        var processedPhoto = processor.ProcessPhoto(new byte[100]);
        processor.Alter(processedPhoto);
    }
}

So basically I want the DoStuff() method to create one kind of image processor, and call the appropriate Process method. However, despite what the interface suggests, Process only works on an IProcessedPhoto of the appropriate type (A and B photos are NOT interchangeable, they just have similar method names). My real code is more complicated in that each processor has several classes specific to them and not interchangeable, but I want to perform the same set of “logical” operations like a template method.

var artifactA = processor.DoA();
var artifactB = processor.DoB();
var final = processor.Process(artifactA, artifactB);

I hope that explains it.

  • 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-22T20:20:21+00:00Added an answer on May 22, 2026 at 8:20 pm

    You can use generics to bind the specific implementation of IProcessedPhoto to your IPhotoProcessors:

    interface IPhotoProcessor<TProcessedPhoto> 
      where TProcessedPhoto: IProcessedPhoto {
    
      TProcessedPhoto Process(byte[] bytes);
      void Alter(TProcessedPhoto processedPhoto);
    
    }
    
    ...
    
    class AProcessor : IPhotoProcessor<PhotoProcessedWithAMethod> { ... }
    
    class BProcessor : IPhotoProcessor<PhotoProcessedWithBMethod> { ... }
    

    The downside is that your factory also needs this information:

    ProcessorFactory.CreateProcessor<PhotoProcessedWithAMethod>(/*stuff*/); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decide the best way of having radiobuttons switch around the contents
I'm trying to decide on the best strategy for accessing the database. I understand
I am trying to decide if a string is a number in Ruby. This
I am trying decide on how I want to handle the UI for an
I'm still trying to decide whether my (home) project should use UTF-8 strings (implemented
I'm trying to decide what to put in a dialog box that tells the
I am trying to decide whether to learn ASP.NET MVC (or to spend the
We are trying to decide how to handle object persistence accross postbacks, to avoid
I'm trying to decide whether it is better to use static methods for loading/saving
I'm trying to decide whether to use a Rails or a Django guru to

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.