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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:32:16+00:00 2026-06-17T14:32:16+00:00

In our project we often choose algorithm by some conditions. For example, suppose we

  • 0

In our project we often choose algorithm by some conditions.

For example, suppose we need to print order for different products. We need to choose different blanks and retrieve different data according to product type, price or any other product characteristic. Usually, we write something like this.

public interface IOrderPrinter
{
    bool CanPrint(Order order);
    PrintResult Print(Order order);
}

class CompositeOrderPrinter : IOrderPrinter
{
    private IList<IOrderPrinter> printers;

    public bool CanPrint(Order order)
    {
        return printers.Any(p => p.CanPrint(order));
    }

    public PrintResult Print(Order order)
    {
        foreach (var printer in printers)
        {
            if (printer.CanPrint(order))
                return printer.Print(order);
        }
        // throw some exception
    }

    public void AddPrinter(IOrderPrinter printer)
    {
        printers.Add(printer);
    }
}

class FirstOrderPrinter : IOrderPrinter
{
    public bool CanPrint(Order order)
    {
        return order.ProductType == ProductType.Banana && order.Price < 100;
    }

    public PrintResult Print(Order order)
    {
        if (!CanPrint)
            // throw some exception
        // print ...
    }
}

class SecondOrderPrinter : IOrderPrinter
{
    public bool CanPrint(Order order)
    {
        return order.ProductType == ProductType.Apple;
    }

    public PrintResult Print(Order order)
    {
        if (!CanPrint)
            // throw some exception
        // print ...
    }
}

Does this pattern have any commonly known name? Yes, it’s look pretty similar as Composite pattern, but there are differences anyway. Is there any standard design pattern which solves this task in better (more elegant, less code, …) way? It’ll be interesting to read about another ways to implement similar behavior.

  • 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-17T14:32:18+00:00Added an answer on June 17, 2026 at 2:32 pm

    When you choose an algorithm at runtime, it’s called the Strategy Pattern.

    In computer programming, the strategy pattern (also known as the policy pattern) is a software design pattern, whereby an algorithm’s behaviour can be selected at runtime.

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

Sidebar

Related Questions

Our project currently uses Crystal Reports for Visual Studio 2008. We need to upgrade
In our project we need to import the csv file to postgres. There are
In our project, we often end up writing complex joins over like 3 tables.
My team is incorporating the Python 2.4.4 runtime into our project in order to
Part of my project requires that we maintain stats of our customers products. More
Our project is in Jetty server and work completely then this project is deployed
Our project has a trunk. I worked on Branch A, adding several features, and
Our project constitutes of many modules (e.g. a HR management system containing modules like
In our project we're mixing xml and annotations based spring mvc security configuration. We've
In our project we use Zend Framework Model generator, which produces something like this

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.