Hey guys

I've got a Factory that can return a database writing object. It takes in a config file that points it to which class to create an object of. Now this is were I'm stuck.

Factory -> returns a database writter

now there's more than 1 way of writing a database writter. In my case I wish to create a databasewritter than will write to a live database and another class that will write to a text file.

Here's a more simplistic idea:

Interface databaseWritter:
String writeMsg(String msg)

 public class MessageOne : databaseWritter
    { 
        public string writeMsg(String msg) 
        { 
            //INSERT INTO DATABASE....
            return "This is a message from Implementation1"; 
        } 
    }

Implementation2:

public class MessageTwo : databaseWritter
    { 
        public string message() 
        { 
            //WRITE TO FILE....
            return "This is a message from Implementation2"; 
        } 
    }

How would I go about coding such behaviour?

Your interface should have a few more properties, such as the server, database name, credentials. Besides that if may either subclass the writer to handle a Filename in the place of the server.
Then in your writeMsg method, you would append the text to the specified filename.

Jerry I'm not asking *How* to implement each class, more how to tell the compiler at runtime which object to create based on a text file.

Okay, maybe you can provide more detail. Walk me through exactly what you are trying to do. I assume you have a text file that is read on startup that has something that tells you how you want to write data.
From there you want to instantiate an object that will either use a live database, or another text file for output.
So in the constructor or Load event you want to use an (if) statement to instatiate the correct type. (IOW instead of using polymorphism for a single class, you want to instantiate a specific class).
Because they both derive from the same interface it should be possible, as long as the interface defines the possible methods required for both operations.

Your method is the tree I'm trying to bark up. However I'm not sure how to turn a string into a class implementation.

The text file contains paths from interfaces to the actual concrete implemenation of that requested class.

So if the applicaiton wants to run in demo mode, where no database will be deployed, Dummy information must be retrived from a file or whatever!!

dummy file:
car.int.interface=car.dummy.impl

real file:
car.int.interface=car.real.impl

so the if statement will know which one its needs whilst the underlying return type will be of the interface leaving the application that uses that object not knowing how its been implementated... Makes sense?

Java uses the object 'Class' and instaniates the class by using a loader object.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // Class level variable
        private IDatabaseWriter _myDbWriter = null;
        private string _demoMode = "yes";

        public Form1()
        {
            InitializeComponent();
            if (_demoMode == "yes")
                _myDbWriter = new MessageTwo();
            else
                _myDbWriter = new MessageOne();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _myDbWriter.writeMsg("Hello World");
        }

    }
    public interface IDatabaseWriter
    {
        void writeMsg(string value);
    }

    public class MessageOne : IDatabaseWriter
    {
        public void writeMsg(string value)
        {
            MessageBox.Show("Write to the real database: " + value);
        }
    }

    public class MessageTwo : IDatabaseWriter
    {
        public void writeMsg(string value)
        {
            MessageBox.Show("Write to a test file for demo mode: " + value);
        }
    }
}

I assume this was just some example code you sent earlier.. and that you are not really naming things that way. All Interface classes should start with a capital 'I' iow IDatabaseWriter

As you obviously already know, but for newbies to the concept of Interfaces, let me say that what acidburn is doing is creating an Interface file (the IDatabaseWriter). An interface defines what public methods and properties must exist in any class that derives from that interface.

acidburn has two classes, one for writing to a DB, one for writing to a Text file. Both of these classes inherit from the same Interface, and therefore have the same required methods and properties.

This allows you to create an object of that Interface type, and it can be assigned from any class that also Inherits from that same interface. Then acidburn uses some method to tell the program which one of the classes he wants to use. Once assigned, then any public method or property defined in the Interface is guaranteed to be present in the assigned object.

That's the magic of interfaces.


// Jerry

This article has been dead for over six months. Start a new discussion instead.