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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:39:46+00:00 2026-05-10T23:39:46+00:00

I have the following code for factory design pattern implementation. class Pen{ public: virtual

  • 0

I have the following code for ‘factory’ design pattern implementation.

class Pen{ public:      virtual void Draw() = 0; };  class RedPen : public Pen{ public:      virtual void Draw(){          cout << 'Drawing with red pen' << endl;      } };  class BluePen : public Pen{ public:      virtual void Draw(){          cout << 'Drawing with blue pen' << endl;      } };  auto_ptr<Pen> createPen(const std::string color){      if(color == 'red')          return auto_ptr<Pen>(new RedPen);      else if(color == 'blue')          return auto_ptr<Pen>(new BluePen); } 

But I heard that it can be done in a better way using ‘C++ templates’. Can anyone help how it is done and how template approach is better than this?

Any thoughts

  • 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. 2026-05-10T23:39:46+00:00Added an answer on May 10, 2026 at 11:39 pm

    In the example you posted, neither a factory or a template approach makes sense to me. My solution involves a data member in the Pen class.

    class Pen { public:     Pen() : m_color(0,0,0,0) /* the default colour is black */     {                 }      Pen(const Color& c) : m_color(c)     {     }      Pen(const Pen& other) : m_color(other.color())     {     }      virtual void Draw()     {         cout << 'Drawing with a pen of color ' << m_color.hex();     }     void setColor(const Color& c) { m_color = c; }     const Color& color() const { return m_color; } private:     Color m_color; };  class Color { public:     Color(int r, int g, int b, int a = 0) :         m_red(r), m_green(g), m_blue(other.blue()), m_alpha(a)       {     }      Color(const Color& other) :          m_red(other.red()), m_green(other.green()),          m_blue(other.blue()), m_alpha(other.alpha())     {     }      int red() const { return m_red; }     int green() const  { return m_green; }     int blue() const { return m_blue; }     int alpha() const { return m_alpha; }      std::string hex() const     {         std::ostringstream os;         char buf[3];         os << '#';          sprintf(buf, '%2X', red());         os << buf;          sprintf(buf, '%2X', green());         os << buf;          sprintf(buf, '%2X', blue());         os << buf;          sprintf(buf, '%2X', alpha());         os << buf;          return os.str();     }  private:     int m_red;     int m_green;     int m_blue;     int m_alpha; } 

    Of course, the color class would have to be adjusted to the drawing API you use — and perhaps be way more advanced than this one (different color spaces, etc).

    Why not templates?

    The reason it does not make sense to use templates, is that (presumably) the only difference between the different drawing operations is the color variable. So, by using templates (or manually declaring different classes, as you did), you will duplicate similar code. This will make your program large, and slow it down.

    So, the draw function should either take the color as an argument, or (as in my example) have the color as a class data member.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You might want to look at NHibernate, LINQ to SQL,… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer Check out ANTS ... Line-level code timing – drill down… May 11, 2026 at 11:44 pm
  • Editorial Team
    Editorial Team added an answer This will perform a query by joining your tables: var… May 11, 2026 at 11:44 pm

Related Questions

I think, the following can't be done in Java. But I would be happy
Being a Java developer in an agile development cycle, I have learnt that it
I am building a small website for fun/learning using a fairly standard Web/Service/Data Access
In short: what is the best way to design and implement a factory+plugin mechanism,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.