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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:56:41+00:00 2026-06-17T01:56:41+00:00

I have two classes, one does all the work of getting data from a

  • 0

I have two classes, one does all the work of getting data from a Database(already implemented) and the other from a File(implementing right now).
The thing here is that I want to be able to switch the streaming(Database or File). The process that check whether is Database or File is needed only once.
So, for each method in a class I don’t wanna check everytime what should be use when this method is called.
for now, I do as follows:

if(IsDataBaseStream())
    Database::execQuery("SELECT * from table");
else //is FileStream
    File::GetAllFrom("Table");

This is ugly. I’m refusing to do this.
I thought about callbacks, but that doesnt work between different classes.

Is there a way of not wasting processor work like that?

Thanks in advance

  • 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-17T01:56:42+00:00Added an answer on June 17, 2026 at 1:56 am

    You should use the Strategy Design Pattern. Give both classes a common base with an abstract method taking the data, make a pointer to that base class, and then assign the pointer a file-based or a db-based implementation after a single check.

    struct BaseStream {
        virtual void process();
    };
    struct FileStream : public BaseStream {
        virtual void process() {
            File::GetAllFrom("Table");
        }
    };
    struct DBStream : public BaseStream {
        virtual void process() {
            Database::execQuery("SELECT * from table");
        }
    };
    ...
    BaseStream *s;
    if (IsDataBaseStream()) {
        s = new DBStream;
    } else {
        s = new FileStream;
    }
    ...
    s -> process();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes one of which inherits from the other. Im trying to
I have two classes, one that inherits from the other. The base class is
I have the following two classes, one inherits from the other Class A{ void
I have two classes (MVC view model) which inherits from one abstract base class.
i have two data classes which hold only data members(no functions). One is CallTask
Starting to learn Canvas and have two classes so far (main one to call
First I want to describe my situation briefly. I have two classes, one MainClass
I have two (unrelated) classes. The first one is Point: typedef std::complex<double> complex_number; class
I have created two viewController classes such that one is superclass of another.i have
Suppose I have two projects. One is an application and the other is a

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.