UML – Representing Factory Pattern in UML

class-diagramdesign-patternsperluml

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();
    }
}

Best Answer

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.