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

  • Home
  • SEARCH
  • 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 8180747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:17:25+00:00 2026-06-07T00:17:25+00:00

On www.dofactory.com I found a real world example of the Factory Pattern. But the

  • 0

On http://www.dofactory.com I found a real world example of the Factory Pattern. But the code generates a warning in ReSharper about a virtual member call in the constructor.

The code causing the warning is the following:

abstract class Document
{
    private List<Page> _pages = new List<Page>();

    // Constructor calls abstract Factory method
    public Document()
    {
        this.CreatePages(); // <= this line is causing the warning
    }

    public List<Page> Pages
    {
        get { return _pages; }
    }

    // Factory Method
    public abstract void CreatePages();
}

class Resume : Document
{
    // Factory Method implementation
    public override void CreatePages()
    {
        Pages.Add(new SkillsPage());
        Pages.Add(new EducationPage());
        Pages.Add(new ExperiencePage());
    }
}

In the consuming code, you can then simply use:

Document document = new Resume();

I do understand why it’s a bad idea to call a virtual member in the constructor (as explained here).

My question is how you can refactor this in order to still use the factory pattern, but without the virtual member call in the constructor.

If I’d just remove the call to CreatePages from the constructor, the consumer would have to explicitly call the CreatePages method:

Document document = new Resume();
document.CreatePages();

I much more prefer the situation where creating a new Resume is all that’s needed to actually create a Resume containing pages.

  • 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-07T00:17:27+00:00Added an answer on June 7, 2026 at 12:17 am

    One way to refactor this would be passing the pages upfront, and passing them to a protected constructor:

    public abstract class Document {
        protected Document(IEnumerable<Page> pages) {
            // If it's OK to add to _pages, do not use AsReadOnly
            _pages = pages.ToList().AsReadOnly();
        }
        // ...
    }
    
    public class Resume : Document {
        public Resume() : base(CreatePages()) {
        }
        private static IEnumerable<Page> CreatePages() {
            return new Page[] {
                new SkillsPage(),
                new EducationPage(),
                new ExperiencePage()
            };
        }
    }
    

    P.S. I am not sure what this has to do with the factory method. Your post illustrates the Template Method Pattern.

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

Sidebar

Related Questions

Ive ABSTRACT FACTORY pattern in one of my projects: http://www.dofactory.com/Patterns/PatternAbstract.aspx Code: public class QuestaoResposta
https://www.x.com/community/ppx/code_samples The sample code returns ACK, but where is the proper response? For example
I am trying to understand UML diagram describing Decorator Pattern at link below http://www.dofactory.com/Patterns/PatternDecorator.aspx
http://www.dofactory.com/Patterns/PatternFlyweight.aspx There is a UML diagram on that page. It has three different kinds
http://www.example.com?a=1&a=2 has a valid form-urlencoded querystring and has two values ( 1 and 2
http://www.radikalgenc.com/bulten/ekim.htm this is a monthly newsletter. but it does not appear fine on mozilla,
From : www.example.com/cut/456 To : www.example.com/cut/index.php?tag=456 I try this and it doesn't work RewriteEngine
The references (GOF Design Patterns, Head First Design Patterns, http://www.dofactory.com/Patterns/PatternObserver.aspx ) that I've read
I am trying to understand Adapter pattern and its use in real world. After
I'm learning about Design Patterns and found the Builder design pattern. What are the

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.