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 have to create an UML class diagram from the following example code (Perl). I don't think that the standard factory pattern match in my case or I don't understand it correctly. I don't have AbstractFactory and ConcreteFactory but a single Factory.

What does this example looks like in UML class diagram?

Edit: This is only a very flat and simple example. The real application has many "Types" with totally different workflows. The class diagramm is to get an overview over the hole structure. Not realy for internal workflow like sequence diagrams does.

package Module::Factory;
sub new { bless { type => $_[1] }, $_[0]; }
sub create {
    my( $self, $name ) = @_;
    my $object = "Module::Types::$self->{type}"->new( $name );
    return( $object );
}

package Module::Types::Test1;
sub new { bless { name => $_[1] }, $_[0]; }
sub test {
    my( $self ) = @_;
    say "Hello, $self->{name}.";
    return( $self );
}

package Module::Types::Test2;
sub new { bless { name => $_[1] }, $_[0]; }
sub test {
    my( $self ) = @_;
    say "Bye, $self->{name}.";
    return( $self );
}

# even more Module::Types::TestN packages...

package main;
foreach my $type ( qw( Test1 Test2 ) ) {
    my $factory = Module::Factory->new( $type );
    foreach my $name ( qw( World John Peter ) ) {
        my $object = $factory->create( $name );
        $object->test();
    }
}
share|improve this question
    
I don't know much about Perl, but isn't your factory really the "abstract factory," and isn't your call to Factory->new('test') your concrete factory implementation? –  Robert Harvey Jul 25 '12 at 14:38
    
But I don't know how to write the methods and returns within the diagram. n times "create() : TestN" method for the factory? –  burnersk Jul 26 '12 at 8:52

1 Answer 1

up vote 5 down vote accepted

You can draw an UML sequence diagram out of this easily, but a class diagramm won't tell you much, except for some logical errors.

Let's draw up a sequence diagram first:

sequence diagram

Now, let's remove Factory for a moment, what do we have?

UML sequence diagram without a factory

Hmm, are we sure we're missing something?

Basically all your Factory module does is, it shortens a namespace into its last part.

Also, what kind of interface is App dependent of?

  1. it is dependent on Factory's interface of creating objects by a name
  2. it is dependent of Test's interface, as it calls that for its actual work.

Factory is dependent of

  1. Perl's reflection system, and
  2. TestModule's interface.

Usually, an Abstract Factory is called an Abstract Factory, because it sits on top of an object 'family', all sharing the same interface. It can be, that a Factory pattern is used to decide between two related "families", all providing the same interface, but, for example, one being a Swing UI component library, the other being an AWT UI component library (or Motif and GTK), or a "real" implementation and a mock-up implementation for testing.

So, with this one class, it's not obvious what are you using the factory for. Are you trying to switch namespaces to a mock-up version for testing?

You can draw your class system based on this (you see who is dependent on who and who is associated to who - dependent if knows about it, associated if has to know about it beforehand in order to be able to call) but I doubt it's the final solution.

share|improve this answer
1  
Thank you very much for you explanation, @Aadaam! I wasn't very specific in my question so I edited it again. You are absolutly right with the unnecessary Factory in my original question but it wasn't telling everything. My factory is able to create many different modules of course. Anyway, your work is not in vain I realy can use it within my technical "extension documentation" (the other one along with the technical usage documentation). –  burnersk Jul 26 '12 at 8:44

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.