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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:26:33+00:00 2026-05-15T12:26:33+00:00

Okay, so you have a load of methods sprinkled around your system’s main class.

  • 0

Okay, so you have a load of methods sprinkled around your system’s main class. So you do the right thing and refactor by creating a new class and perform move method(s) into a new class. The new class has a single responsibility and all is right with the world again:

class Feature
{
public:
    Feature(){};

    void doSomething();
    void doSomething1();
    void doSomething2();   
};

So now your original class has a member variable of type object:

  Feature _feature;

Which you will call in the main class. Now if you do this many times, you will have many member-objects in your main class.

Now these features may or not be required based on configuration so in a way it’s costly having all these objects that may or not be needed.

Can anyone suggest a way of improving this?


EDIT: Based on suggestion to use The Null Object Design Pattern I’ve come up with this:

An Abstract Class Defining the Interface of the Feature:

class IFeature
{
public:
    virtual void doSomething()=0;
    virtual void doSomething1()=0;
    virtual void doSomething2()=0;

    virtual ~IFeature(){}
};

I then define two classes which implement the interface, one real implementation and one Null Object:

class RealFeature:public IFeature
{
public:
    RealFeature(){};

    void doSomething(){std::cout<<"RealFeature doSomething()"<<std::endl;}
    void doSomething1(){std::cout<<"RealFeature doSomething()"<<std::endl;}
    void doSomething2(){std::cout<<"RealFeature doSomething()"<<std::endl;}
}; 

class NullFeature:public IFeature
{
public:
    NullFeature(){};

    void doSomething(){std::cout<<"NULL doSomething()"<<std::endl;};
    void doSomething1(){std::cout<<"NULL doSomething1()"<<std::endl;};
    void doSomething2(){std::cout<<"NULL doSomething2()"<<std::endl;};


};

I then define a Proxy class which will delegate to either the real object or the null object depending on configuration:

class Feature:public IFeature
{
public:
    Feature();
    ~Feature();

    void doSomething();
    void doSomething1();
    void doSomething2();

private:
    std::auto_ptr<IFeature> _feature;
};

Implementation:

   Feature::Feature()
    {
        std::cout<<"Feature() CTOR"<<std::endl;
        if(configuration::isEnabled() )
        {
            _feature = auto_ptr<IFeature>( new RealFeature() );
        }
        else
        {
            _feature = auto_ptr<IFeature>( new NullFeature() );
        }
    }


    void Feature::doSomething()
    {
        _feature->doSomething();
    }

    //And so one for each of the implementation methods

I then use the proxy class in my main class (or wherever it’s required):

Feature _feature;

_feature.doSomething();
  • 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-15T12:26:34+00:00Added an answer on May 15, 2026 at 12:26 pm

    If a feature is missing and the correct thing to do is ignore that fact and do nothing, you can get rid of your checks by using the Null Object pattern:

    class MainThing {
        IFeature _feature;
    
        void DoStuff() {
            _feature.Method1();
            _feature.Method2();
    }
    
    interface IFeature {
        void Method1();
        void Method2();
    }
    
    class SomeFeature { /* ... */ }
    
    class NullFeature {
        void Method1() { /* do nothing */ }
        void Method2() { /* do nothing */ }
    }
    

    Now, in MainThing, if the optional feature isn’t there, you give it a reference to a NullFeature instead of an actual null reference. That way, MainThing can always safely assume that _feature isn’t null.

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

Sidebar

Related Questions

Okay I have a series of objects based on a base class which are
I have two methods that do the same thing. The first one makes performance
Okay, so I have a JSON array of image URLs. I want to load
Okay so basically : i have this simple example: main.cpp using namespace VHGO::Resource; std::list<BaseTable*>
Okay, so I have an application that is going to be load balanced on
Okay so I have the following code: function() { $(ul#postbit).load('load.php').fadeIn(slow); }, 3000); What I
Okay - I have a dilemma. So far my script converts page titles into
okay i have been trying to understand this for hours i am learning VB
Okay I have a large CRUD app that uses tabs with Forms embedded in
okay i have found the way to run a video in a image.... 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.