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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:50:51+00:00 2026-05-12T15:50:51+00:00

I have the following design decision to make – running into it for the

  • 0

I have the following design decision to make – running into it for the second time, so I hope I’m not the only one…

I have various Command classes that represent some actions. Each Command class (there are many, say SleepCommand, RunCommand, MeasureCommand) has different parameters with default values and so on, but in the end each has a generate() method that returns some packed representation.

Now, I also have a CommandSequence class that aggregates many commands in a list. It has its own generate() method that does some custom work, runs generate() for all the commands in its list, adds stuff of its own and so on.

I have a doubt of how to create these command objects. There are several alternatives:

(1) Let the user create instances of the various Command objects and just call an add_command() method on CommandSequence
(2) Create a “factory method” in CommandSequence for each of the possible Commands. The factory methods accepts the Command’s arguments and passes them to the Command constructor.
(3) Since method (2) duplicates code (the constructor initialization lists of the objects), I can just make all the factory methods accept *args, **kwargs and pass them to the objects.

Now, approach (1) doesn’t duplicate code, but exposes the plethora of Command classes to the user. As opposed to (2) the user code is more verbose:

# seq is a CommandSequence object
seq.add_command(SleepCommand(time='10 ms', wakeup_alarm=False))

as opposed to:

seq.sleep(time='10 ms', wakeup_alarm=False)

Since I’m building a kind of a DSL, user code shortness and clearness is very important to me. I can use method (3) to save on code duplication, but then where to place the documentation of the Command classes – in the factory methods or in the classes’ constructors themseves.

Am I missing some obvious pattern here? How would you approach this?

  • 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-12T15:50:51+00:00Added an answer on May 12, 2026 at 3:50 pm

    I’d stick with 1.

    When you have a part of the code that is required to execute the SleepCommand, it makes sense to me to expose the construction interface of the SleepCommand to that part of the code. How else can they construct one?

    You could use the factory, but I don’t think it gains you much here (apart from code duplication as you point out). It also means that every time you need to add a new command, you need to go and change the CommandSequence class to expose an interface to execute the new command. This violates the open-closed principle (http://en.wikipedia.org/wiki/Open/closed_principle).

    Edit: Another important point is that number 1 is a simpler design. To add a new command, just create a new command class – this is good OO-design. Also, with the 2 lines of code you write in the question, I don’t think the second is really much more complicated than the first.

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

Sidebar

Related Questions

I have the following design in one of my projects: template<typename C> class B
I have to find a design decision for the following task: I have a
I have a following design function crudDataTable() { this.addEditFormContainerSelector = ''; this.paginationType = full_numbers;
I have the following database design: Employee Table: Username, Name, DivisionCode Division Table: SapCode,
I have the following database design: Employee Table: Username, Name Quiz Table: QuizID, Title,
I am creating an App with following design: I have a UITableView with custom
I am in need of some guidance for the following design. I have a
I have troubles with the following database design (simplified for the example). It allows
Is there any name for the following DB table design: Basically we have generic
The following is using EF 5.0.0-rc and Code First. In my design, I have

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.