Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I need help with a abstract factory pattern design. This question is a continuation of: Design help with parallel process

I am really confused where I should be initializing all of the settings for each type of medium (ex: RS232, TCP/IP, etc).

Attached is the drawing on how I am setting up the pattern: enter image description here

As shown, when a medium is created, each medium imposes a ICreateMedium interface. I would assume that the Create() method also create the proper object, such as SerialPort serialPort = new SerialPort("COM1", baud); however, TCPIPMedium would have an issue with the interface because it wouldn't need to initialize a serial port object.

I know I am doing something majorly wrong here. I just can't figure it out and have been stuck for a while.

What I also get confused on show the interface IMedium will get access to the communication object once it is created so it can write out the appropriate byte[] packet.

Any guidance would be greatly appreciated. My main goal is to have the Communicator class spit a packet out without caring which type of medium is active.

share|improve this question
    
So, where is the problem? It isn't clear to me why the communicator class does care which type of medium is active. So it seems you have achieved your goal. As far as creating the port, yes TCPIPMedium has to and Rs232Medium doesn't, but why is that a problem? And, why does IMedium need access to the communication object? Won't the commicator object call IMedium? Does IMedium need to call back for some reason you haven't mentioned? –  psr Jun 8 '12 at 17:13
    
"As far as creating the port, yes TCPIPMedium has to and Rs232Medium doesn't, but why is that a problem?" Which class would that be done in? That is where I am confused the most, I guess. Would that be done in the abstract factory portion, example RS232MediumCreate? Do I need to create another method in my interface (ICreateMedium) for this to happen? "Won't the commicator object call IMedium?" Yes that is a design flaw on my part. That will be fixed. Thanks. –  brazc0re Jun 8 '12 at 17:18
    
TCPIPMediumCreate will create a TCPIPMedium object, if I understand correctly. That class knows it needs a port (and what to do with one), so it should create one. I guess the only issue I can see is if needs more information to create the port (like the port number to use). If so you have a problem, because your interface doesn't allow that information to be passed in, so you have to think about how to get it there. If it can just pick a port I don't really see any issue. –  psr Jun 8 '12 at 17:36
    
I'm a bit confused. You've linked to a question where the suggestion (rightly) is the Adapter pattern and asked a question about the Abstract Factory pattern, using what appears to be more of a Factory Method pattern. I don't know, but perhaps this answer will help you a bit: programmers.stackexchange.com/questions/81838/… –  pdr Jun 8 '12 at 17:37
    
Thanks pdr. My issue was that I could have just added a constructor to each abstract factory class for ICreateMedium (from NickC answer below). I over-thought this problem. Sorry for mis-communication on original question. I switched from an abstract pattern to abstract factory after doing a bit of research. –  brazc0re Jun 8 '12 at 18:08
add comment

1 Answer

up vote 4 down vote accepted

In this case, if the RS232Medium constructor requires a SerialPort then the RS232 Factory, RS232MediumCreate as you have called it, will need to have that port already stored inside it:

(sorry about the Java)

class RS232MediumCreate implements ICreateMedium {
    SerialPort port;

    RS232MediumCreate(SerialPort port) {
        this.port = port;
    }

    @Override
    public IMedium Create() {
        return new Rs232Medium(port);
    }
}

Now your usage of Communicator looks like this:

void getMediumFrom(ICreateMedium factory) {
    IMedium medium = factory.Create();

    //...
}

and your usages of that method will look like this:

communicator.getMediumFrom(new RS232MediumCreate(new SerialPort("COM1"), baud));

When using an Abstract Factory Pattern, as you mentioned, your interfaces must align and so any differences in required parameters for the final object must be accounted for in the corresponding Factory.

That means that Factory must already know about what parameters will be used. This is a consequence of the Factory pattern, and to work around it would defeat the purpose of using an abstract factory.

share|improve this answer
    
This worked perfectly. Thanks for the clarification. Being new to OOP, it was sort of a mind-twister, but it all makes sense now. –  brazc0re Jun 8 '12 at 18:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.