[Uml-devel] KDE/kdesdk/umbrello/umbrello

Oliver Kellogg okellogg at users.sourceforge.net
Thu Feb 16 21:14:29 UTC 2012


SVN commit 1280352 by okellogg:

AssociationWidget: Replace public constructors by static factory methods.
Reason: The constuctors were much too heavy weight.
Rule of thumb: As soon as method invocations are needed, replace constructor
by factory method.


 M  +1 -1      clipboard/umldragdata.cpp  
 M  +1 -1      toolbarstateassociation.cpp  
 M  +15 -15    umlview.cpp  
 M  +35 -22    widgets/associationwidget.cpp  
 M  +10 -3     widgets/associationwidget.h  


--- trunk/KDE/kdesdk/umbrello/umbrello/clipboard/umldragdata.cpp #1280351:1280352
@@ -687,7 +687,7 @@
     QDomNode associationWidgetNode = associationWidgetsNode.firstChild();
     QDomElement associationWidgetElement = associationWidgetNode.toElement();
     while ( !associationWidgetElement.isNull() ) {
-        AssociationWidget* associationWidget = new AssociationWidget(view->umlScene());
+        AssociationWidget* associationWidget = AssociationWidget::create(view->umlScene());
         if (associationWidget->loadFromXMI(associationWidgetElement, widgets))
             associations.append(associationWidget);
         else {
--- trunk/KDE/kdesdk/umbrello/umbrello/toolbarstateassociation.cpp #1280351:1280352
@@ -226,7 +226,7 @@
         valid = AssocRules::allowAssociation(type, widgetA, widgetB);
     }
     if (valid) {
-        AssociationWidget *temp = new AssociationWidget(m_pUMLScene, widgetA, type, widgetB);
+        AssociationWidget *temp = AssociationWidget::create(m_pUMLScene, widgetA, type, widgetB);
         addAssociationInViewAndDoc(temp);
         if (type == Uml::AssociationType::Containment) {
             UMLListView *lv = UMLApp::app()->listView();
--- trunk/KDE/kdesdk/umbrello/umbrello/umlview.cpp #1280351:1280352
@@ -2089,7 +2089,8 @@
     if (newParentWidget == NULL)
         return;
     // Create the new containment association.
-    AssociationWidget *a = new AssociationWidget(umlScene(), newParentWidget,
+    AssociationWidget *a = AssociationWidget::create
+                             (umlScene(), newParentWidget,
             Uml::AssociationType::Containment, selfWidget);
     a->calculateEndingPoints();
     a->setActivated(true);
@@ -2201,7 +2202,7 @@
             continue;
         }
         // Create the AssociationWidget.
-        assocwidget = new AssociationWidget(umlScene());
+        assocwidget = AssociationWidget::create(umlScene());
         assocwidget->setWidget(widgetA, A);
         assocwidget->setWidget(widgetB, B);
         assocwidget->setAssociationType(assocType);
@@ -2238,7 +2239,7 @@
                 if (widget->rect().contains(w->rect()))
                     continue;
                 // create the containment AssocWidget
-                AssociationWidget *a = new AssociationWidget(umlScene(), widget,
+                AssociationWidget *a = AssociationWidget::create(umlScene(), widget,
                         Uml::AssociationType::Containment, w);
                 a->calculateEndingPoints();
                 a->setActivated(true);
@@ -2265,7 +2266,7 @@
     if (!breakFlag || pWidget->rect().contains(widget->rect()))
         return;
     // create the containment AssocWidget
-    AssociationWidget *a = new AssociationWidget(umlScene(), pWidget, Uml::AssociationType::Containment, widget);
+    AssociationWidget *a = AssociationWidget::create(umlScene(), pWidget, Uml::AssociationType::Containment, widget);
     a->calculateEndingPoints();
     a->setActivated(true);
     if (! addAssociation(a))
@@ -2346,18 +2347,17 @@
     }
     Uml::AssociationType assocType = Uml::AssociationType::Composition;
     UMLWidget *w = findWidget(type->id());
-    AssociationWidget *aw = NULL;
     // if the attribute type has a widget representation on this view
     if (w) {
-        aw = findAssocWidget(widget, w, attr->name());
-        if (aw == NULL &&
+        AssociationWidget *a = findAssocWidget(widget, w, attr->name());
+        if (a == NULL &&
                 // if the current diagram type permits compositions
                 AssocRules::allowAssociation(assocType, widget, w, false)) {
             // Create a composition AssocWidget, or, if the attribute type is
             // stereotyped <<CORBAInterface>>, create a UniAssociation widget.
             if (type->stereotype() == "CORBAInterface")
                 assocType = Uml::AssociationType::UniAssociation;
-            AssociationWidget *a = new AssociationWidget(umlScene(), widget, assocType, w, attr);
+            a = AssociationWidget::create(umlScene(), widget, assocType, w, attr);
             a->calculateEndingPoints();
             a->setVisibility(attr->visibility(), B);
             /*
@@ -2380,14 +2380,14 @@
             UMLWidget *w = c ? findWidget(c->id()) : 0;
             // if the referenced type has a widget representation on this view
             if (w) {
-                aw = findAssocWidget(widget, w, attr->name());
-                if (aw == NULL &&
+                AssociationWidget *a = findAssocWidget(widget, w, attr->name());
+                if (a == NULL &&
                         // if the current diagram type permits aggregations
                         AssocRules::allowAssociation(Uml::AssociationType::Aggregation, widget, w, false)) {
                     // create an aggregation AssocWidget from the ClassifierWidget
                     // to the widget of the referenced type
-                    AssociationWidget *a = new AssociationWidget
-                    (umlScene(), widget, Uml::AssociationType::Aggregation, w, attr);
+                    a = AssociationWidget::create (umlScene(), widget,
+                                                   Uml::AssociationType::Aggregation, w, attr);
                     a->calculateEndingPoints();
                     a->setVisibility(attr->visibility(), B);
                     //a->setChangeability(true, B);
@@ -2469,7 +2469,7 @@
 
             // for foreign key contstraint, we need to create the association type Uml::AssociationType::Relationship.
             // The referenced entity is the "1" part (Role A) and the entity holding the relationship is the "many" part. ( Role B)
-            AssociationWidget *a = new AssociationWidget(umlScene(), w, assocType, widget);
+            AssociationWidget *a = AssociationWidget::create(umlScene(), w, assocType, widget);
             a->setUMLObject(fkConstraint);
             a->calculateEndingPoints();
             //a->setVisibility(attr->getVisibility(), B);
@@ -3541,7 +3541,7 @@
         if (tag == "assocwidget" ||
                 tag == "UML:AssocWidget") {  // for bkwd compatibility
             countr++;
-            AssociationWidget *assoc = new AssociationWidget(umlScene());
+            AssociationWidget *assoc = AssociationWidget::create(umlScene());
             if (!assoc->loadFromXMI(assocElement)) {
                 uError() << "could not loadFromXMI association widget:"
                     << assoc << ", bad XMI file? Deleting from umlview.";
@@ -3637,7 +3637,7 @@
                 UMLWidget *wB = findWidget(objB->id());
                 if (wA != NULL && wB != NULL) {
                     AssociationWidget *aw =
-                        new AssociationWidget(umlScene(), wA, at, wB, umla);
+                        AssociationWidget::create(umlScene(), wA, at, wB, umla);
                     aw->syncToModel();
                     m_AssociationList.append(aw);
                 } else {
--- trunk/KDE/kdesdk/umbrello/umbrello/widgets/associationwidget.cpp #1280351:1280352
@@ -53,38 +53,48 @@
 
 using namespace Uml;
 
-// this constructor really only for loading from XMI, otherwise it
-// is bad..and shouldn't be allowed as it creates an incomplete
-// associationwidget.
 /**
-  * Constructor.
+  * Constructor is private because the static create() methods shall
+  * be used for constructing AssociationWidgets.
   *
   * @param scene              The parent view of this widget.
   */
 AssociationWidget::AssociationWidget(UMLScene *scene)
   : WidgetBase(scene, WidgetBase::wt_Association)
 {
-    init();
 }
 
-// the preferred constructor
 /**
-  * Constructor.
+ * This constructor is really only for loading from XMI, otherwise it
+ * should not be allowed as it creates an incomplete associationwidget.
   *
   * @param scene      The parent view of this widget.
+ */
+AssociationWidget* AssociationWidget::create(UMLScene *scene)
+{
+    AssociationWidget* instance = new AssociationWidget(scene);
+    instance->init();
+    return instance;
+}
+
+/**
+  * Preferred constructor (static factory method.)
+  *
+  * @param scene      The parent view of this widget.
   * @param WidgetA   Pointer to the role A widget for the association.
   * @param Type      The AssociationType for this association.
   * @param WidgetB   Pointer to the role B widget for the association.
   * @param umlobject Pointer to the underlying UMLObject (if applicable.)
   */
-AssociationWidget::AssociationWidget(UMLScene *scene, UMLWidget* pWidgetA,
+AssociationWidget* AssociationWidget::create
+                                    (UMLScene *scene, UMLWidget* pWidgetA,
                                      Uml::AssociationType assocType, UMLWidget* pWidgetB,
                                      UMLObject *umlobject /* = NULL */)
-  : WidgetBase(scene, WidgetBase::wt_Association)
 {
-    init();
+    AssociationWidget* instance = new AssociationWidget(scene);
+    instance->init();
     if (umlobject) {
-        setUMLObject(umlobject);
+        instance->setUMLObject(umlobject);
     } else {
         // set up UMLAssociation obj if assoc is represented and both roles are UML objects
         if (Uml::AssociationType::hasUMLRepresentation(assocType)) {
@@ -99,7 +109,8 @@
                 // done BEFORE creation of the widget, if it mattered to the code.
                 // But lets leave check in here for the time being so that debugging
                 // output is shown, in case there is a collision with code elsewhere.
-                UMLAssociation * myAssoc = m_umldoc->findAssociation( assocType, umlRoleA, umlRoleB, &swap );
+                UMLDoc *doc = UMLApp::app()->document();
+                UMLAssociation *myAssoc = doc->findAssociation( assocType, umlRoleA, umlRoleB, &swap );
                 if (myAssoc != NULL) {
                     if (assocType == Uml::AssociationType::Generalization) {
                         uDebug() << " Ignoring second construction of same generalization";
@@ -112,32 +123,34 @@
                 }
                 if (myAssoc == NULL)
                     myAssoc = new UMLAssociation( assocType, umlRoleA, umlRoleB );
-                setUMLAssociation(myAssoc);
+                instance->setUMLAssociation(myAssoc);
             }
         }
     }
 
-    setWidget(pWidgetA, A);
-    setWidget(pWidgetB, B);
+    instance->setWidget(pWidgetA, A);
+    instance->setWidget(pWidgetB, B);
 
-    setAssociationType(assocType);
+    instance->setAssociationType(assocType);
 
-    calculateEndingPoints();
+    instance->calculateEndingPoints();
 
     //The AssociationWidget is set to Activated because it already has its side widgets
-    setActivated(true);
+    instance->setActivated(true);
 
     // sync UML meta-data to settings here
-    mergeAssociationDataIntoUMLRepresentation();
+    instance->mergeAssociationDataIntoUMLRepresentation();
 
     // Collaboration messages need a name label because it's that
     // which lets operator== distinguish them, which in turn
     // permits us to have more than one message between two objects.
-    if (isCollaboration()) {
+    if (instance->isCollaboration()) {
         // Create a temporary name to bring on setName()
-        int collabID = m_scene->generateCollaborationId();
-        setName('m' + QString::number(collabID));
+        int collabID = instance->m_scene->generateCollaborationId();
+        instance->setName('m' + QString::number(collabID));
     }
+
+    return instance;
 }
 
 /**
--- trunk/KDE/kdesdk/umbrello/umbrello/widgets/associationwidget.h #1280351:1280352
@@ -4,7 +4,7 @@
  *   the Free Software Foundation; either version 2 of the License, or     *
  *   (at your option) any later version.                                   *
  *                                                                         *
- *   copyright (C) 2002-2011                                               *
+ *   copyright (C) 2002-2012                                               *
  *   Umbrello UML Modeller Authors <uml-devel at uml.sf.net>                  *
  ***************************************************************************/
 
@@ -61,8 +61,9 @@
         Center
     };
 
-    AssociationWidget(UMLScene *scene);
-    AssociationWidget(UMLScene *scene, UMLWidget* WidgetA,
+    static AssociationWidget* create(UMLScene *scene);
+    static AssociationWidget* create
+                     (UMLScene *scene, UMLWidget* WidgetA,
                       Uml::AssociationType Type, UMLWidget* WidgetB,
                       UMLObject *umlobject = NULL);
 
@@ -231,6 +232,12 @@
 
 private:
 
+    /**
+     * Constructor is made non accessible:
+     * Users shall use the static create() methods for constructing AssociationWidgets.
+     */
+    AssociationWidget(UMLScene *scene);
+
     void setUMLAssociation (UMLAssociation * assoc);
 
     void mergeAssociationDataIntoUMLRepresentation();




More information about the umbrello-devel mailing list