1

enter image description here

How to create NYPizzaIngredientFactory to others association using one command/ anything easy in pgf-umlcd?

enter image description here

Mainly, I want the unidirectionalassociation from one source to multiple targets.

I am trying this -

\documentclass{article}
\usepackage{pgf-umlcd}
\begin{document}
\begin{tikzpicture}
  \begin{class}{Factory}{0,0}
    \attribute{}
    \attribute{}
    \operation{}
    \operation{}
  \end{class}

  \begin{class}{ProductA}{4,5}
    \attribute{}
    \attribute{}
    \operation{}
    \operation{}
  \end{class}

  \begin{class}{ProductB}{-2,5}
    \attribute{}
    \attribute{}
    \operation{}
    \operation{}
  \end{class}

  \begin{class}{ProductC}{-8,5}
    \attribute{}
    \attribute{}
    \operation{}
    \operation{}
  \end{class}
  \unidirectionalAssociation{Factory}{creates}{}{ProductA}
  \unidirectionalAssociation{Factory}{creates}{}{ProductB}
  \unidirectionalAssociation{Factory}{creates}{}{ProductC}
\end{tikzpicture}
\end{document}

output -

enter image description here

But I don't want 3 incoming lines to Factory class.

  • Lots of similar questions have been asked before, if not in the specific case of pgf-umlcd (not that it matters much, it's all TikZ anyway). A couple of examples I found: tex.stackexchange.com/questions/178603 tex.stackexchange.com/questions/50780 tex.stackexchange.com/questions/280521 If you're unable to figure it out, add a complete code example to your question with what you've tried, and someone will surely be able to fill in the blanks. – Torbjørn T. Oct 17 '17 at 18:02
  • @TorbjørnT. probably I couldn't explain the asking? I can use \unidirectionalassociation to one source to one target. But here is one source to multiple target. I edited my question. – alhelal Oct 18 '17 at 10:07
  • \unidirectionalassociation only draws a single straight line. That's it. You can still do what you ask for, but you'll need to use something else. But please post code, I don't feel like making up an example only to have to redo it three times afterwards because I didn't fully understand what you were after in the first place. – Torbjørn T. Oct 18 '17 at 10:15
2

As mentioned, you can't really do that with \unidirectionalassociation the way it is defined. It might be possible to redefine, but I think drawing those arrows using TikZ-macros instead is just as well. There will be several ways of doing this, I'll explain one here.

In the code below, the first thing I do, after the classes, is

  \draw [umlcd style] (Factory.north) -- ++(0,2cm)
                      coordinate (tmp)
                      node[right,midway] {creates};

This draws a line using a style defined by pgf-umlcd, starting at the top of the Factory class, and ending 2cm higher. The ++ indicates that the coordinate, (0,2cm), is relative to the previous coordinate. Then I define a coordinate at that endpoint called tmp, which is used later.

I also add a node (used to place text) halfway along the line (that's what midway means), on the right side of it. If you don't want the word creates next to this line, remove node[right,midway] {creates}. (But don't remove the semicolon, that is required to end the path.)

Then I draw three lines starting at the tmp coordinate defined above, to each of the product classes:

  \foreach \endpoint in {ProductA,ProductB,ProductC}
    \draw [umlcd style,fill=none,->] (tmp) -| (\endpoint.south)
                                     node[pos=0.75,right] {creates};

I used a loop here, you could also have three separate \draw statements. For these lines, I add fill=none,-> in addition to umlcd style. The first is needed because the style sets a fill colour, so filling must be disabled. The latter is needed to activate the arrow tip at the end of the lines.

The syntax (a) -| (b) means that instead of drawing a straight line, TikZ draws first horizontally from the point a, and then vertically up to b.

Again I add a node, and again, remove it if you don't want it. The pos=0.75 normally means that it should be placed at the point that is 75% of the way from a to b. For paths using -| (or |-) however, pos=0.5 is always at the corner, so pos=0.75 is halfway between the corner and the end of the path.

Output and complete code below.

output of code

\documentclass[border=5mm]{standalone}
\usepackage{pgf-umlcd}
\begin{document}
\begin{tikzpicture}
  \begin{class}{Factory}{0,0}
    \attribute{}
    \attribute{}
    \operation{}
    \operation{}
  \end{class}

  \begin{class}{ProductA}{4,5}
    \attribute{}
    \attribute{}
    \operation{}
    \operation{}
  \end{class}

  \begin{class}{ProductB}{-2,5}
    \attribute{}
    \attribute{}
    \operation{}
    \operation{}
  \end{class}

  \begin{class}{ProductC}{-8,5}
    \attribute{}
    \attribute{}
    \operation{}
    \operation{}
  \end{class}

  \draw [umlcd style] (Factory.north) -- ++(0,2cm)
                      coordinate (tmp)
                      node[right,midway] {creates};

  \foreach \endpoint in {ProductA,ProductB,ProductC}
    \draw [umlcd style,fill=none,->] (tmp) -| (\endpoint.south)
                                     node[pos=0.75,right] {creates};

\end{tikzpicture}
\end{document}
  • Why do you use standalone package. Is there any benefit over article package I used? – alhelal Oct 19 '17 at 6:07
  • How can I shift the first line(Factory.north) -- ++(0,2cm) 1cm left? – alhelal Oct 19 '17 at 6:44
  • @alhelal The standalone class (not package) crops/extends the page size to the size of the content. Your diagram was far too wide for a standard article, that's the only reason I switched. Just try my example, and then replace \documentclass[border=5mm]{standalone} with \documentclass{article}. For the second: ([xshift=-1cm]Factory.north) – Torbjørn T. Oct 19 '17 at 6:51
  • Using aritcle class I expended many time to fit my contents. Thank you. – alhelal Oct 19 '17 at 6:55
  • How can I set a caption to this UML diagram. i.e-Abstract Factory Pattern? – alhelal Oct 21 '17 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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