我尝试用一个辅助点(M)连接两个类,但是组件没有覆盖该点,所以我的连接线在组件框外面。
\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{positioning}
\usepackage{tikzscale}
\pgfdeclarelayer{background,foreground}
\pgfsetlayers{background,main,foreground}
\usepackage{tikz-uml}
\begin{document}
\begin{tikzpicture}
\begin{umlpackage}{DecoratorPattern}
\umlclass[]{Shape}{}{+void draw()}
\umlclass[below=1cm of Shape]{Rectangle}{}{+draw()}
\umlclass[left=1cm of Rectangle]{Circle}{}{+draw()}
\umlclass[right=1cm of Rectangle]{ShapeDecorator}{}{+draw()\\+void setRedBorder()}
\umlclass[below=1cm of ShapeDecorator]{RedShapeDecorator}{}{+draw()\\+void setRedBorder()}
\umlinherit[geometry=|-|]{Circle}{Shape}
\umlinherit[geometry=|-|]{Rectangle}{Shape}
\umlinherit[geometry=|-|]{ShapeDecorator}{Shape}
\umlinherit[geometry=|-|]{RedShapeDecorator}{ShapeDecorator}
\coordinate (M) at ([xshift=1cm]ShapeDecorator.east |- Shape);
\umlaggreg[geometry=-|]{ShapeDecorator}{M};
\umlassoc[geometry=--]{M}{Shape};
\end{umlpackage}
\end{tikzpicture}
\end{document}
如果不使用帮助点,连接位置将与其他连接线相交。
答案1
您可以使用以下坐标代替
\umlemptyclass[right=1cm of ShapeDecorator,coordinate,opacity=0]{M}
该coordinate
选项使它成为\coordinate
而不是普通的node
,并opacity=0
使其不可见。然后,您还需要稍微改变绘制线条的方式,请参阅代码中的注释。
\documentclass[tikz,border=20pt]{standalone}
\usetikzlibrary{positioning}
\usepackage{tikzscale}
\pgfdeclarelayer{background,foreground}
\pgfsetlayers{background,main,foreground}
\usepackage{tikz-uml}
\begin{document}
\begin{tikzpicture}
\begin{umlpackage}{DecoratorPattern}
\umlclass[]{Shape}{}{+void draw()}
\umlclass[below=1cm of Shape]{Rectangle}{}{+draw()}
\umlclass[left=1cm of Rectangle]{Circle}{}{+draw()}
\umlclass[right=1cm of Rectangle]{ShapeDecorator}{}{+draw()\\+void setRedBorder()}
\umlclass[below=1cm of ShapeDecorator]{RedShapeDecorator}{}{+draw()\\+void setRedBorder()}
\umlinherit[geometry=|-|]{Circle}{Shape}
\umlinherit[geometry=|-|]{Rectangle}{Shape}
\umlinherit[geometry=|-|]{ShapeDecorator}{Shape}
\umlinherit[geometry=|-|]{RedShapeDecorator}{ShapeDecorator}
\umlemptyclass[right=1cm of ShapeDecorator,coordinate,opacity=0,draw=none]{M}
\umlaggreg[geometry=--]{ShapeDecorator}{M} % use -- here
\umlassoc[geometry=|-]{M}{Shape} % and |- here
\end{umlpackage}
\end{tikzpicture}
\end{document}