2

I have a problem understanding an UML below:

UML Image

Specifically, what is the relationship between PersistentSet and ThirdPartyPersistentSet? What is the relationship between PersistentObject and ThirdPartyPersistentSet?

Please note that the UML is from Agile Principles, Patterns, and Practices in C# By Martin C. Robert, Martin Micah 2006. Chapter 10

Thanks in advance!

Sisyphus
  • 4,181
  • 1
  • 22
  • 15
Pingpong
  • 7,681
  • 21
  • 83
  • 209

5 Answers5

2

Specifically, what is the relationship between PersistentSet and ThirdPartyPersistentSet?

The solid diamond <|>-----> is Composition ("has a") where the "parts" are destroyed when the "whole" is. In the image below, if you destroy a Car, you destroy the Carburetor.

The empty diamond < >-----> is Aggregation ("has a") where the "parts" might are not destroyed when the "whole" is. In the image below, if you destroy a pond, you don't necessarily destroy the ducks (they move to a different pond if they are smart).

Composition
(source: wikimedia.org)

What is the relationship between PersistentObject and ThirdPartyPersistentSet?

This is a dependency relationship. See my answer here for further information.

So when does a dependency relationship change to an association relationship when using parameter passing?

If you store the parameter locally, then it changes from a dependency relationship, to an association relationship. If you only use the parameter locally, then it stays a dependency.

C# Code example:

// Association 
public class ThirdPartyPersistentSet
{
    private PersistentObject _object;
    public ThirdPartyPersistentSet(PersistentObject obj)
    {
        _object = obj; // Store it to a local variable.
        // Now ThirdPartyPersistentSet 'knows' about
        // the PersistentObject.
    }
}

// Dependency
public class ThirdPartyPersistentSet
{
    public ThirdPartyPersistentSet(PersistentObject obj)
    {
        obj.GetSomething(); // Do something with obj,
        // but do not store it to a local variable.
        // You only 'use' it and ThirdPartyPersistentSet
        // does not 'know' about it.
    }
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
1

The relationship between PersistentSet and ThirdPartyPersistentSet is an Aggregation, which means the PersistentSet contains one or more ThridPartyPersistenSet instances. This is a "weak" relationship, meaning the instances of ThirdPartyPersistentSet can exist outside of the PersistentSet.

The relationship between PersistentObject and ThirdPartyPersistentSet is a Dependency, meaning basically ThirdPartyPersistentSet needs a PersistentObject to do it's work.

So, to translate this to code, your PersistentSet would contain something like this:

public class PersistentSet
{
    public List<ThirdPartyPersistentSet> Items { get; }
    ...
}

And your ThirdPartyPersistentSet would look something like this:

public class ThirdPartyPersistentSet
{
    private PersistentObject _object;
    public ThirdPartyPersistentSet(PersistentObject obj)
    {
        _object = obj;
    }
    ...
}
ckramer
  • 9,419
  • 1
  • 24
  • 38
  • 2
    Ckramer in your code ThirdPartyPersistentSet is associated with PersistentObject. Dependency is weaker form of dependency. ThirdPartyPersistentSet could for example accept parameter of PersistentObject and use it locally and this would still be dependency. – StanislawSwierc Jun 01 '11 at 22:31
  • @proxon - Yes, you are correct, however since the class diagram didn't contain any method defs I didn't want to add too much extra stuff for fear of confusing things too much. Constructor args are a harder dependency, but they are also pretty self-explanatory. – ckramer Jun 01 '11 at 23:41
  • thanks both of you. Thus, ThirdPartyPersistentSet and PersistentObject can be via contructor parameter, and method parameter. – Pingpong Jun 02 '11 at 20:06
  • 1
    @Pingpong - If you decide to code it this way, as proxon has pointed out, you would change your UML diagram to be a solid line with an arrow (Association). As soon as you store a parameter passed in to a local variable, it becomes an association. If you pass in the PersistentObject as a parameter to a function, and dont store it locally, its a dependency. I've added an example to show what I mean. – SwDevMan81 Jun 03 '11 at 18:56
0

Specifically, what is the relationship between PersistentSet and ThirdPartyPersistentSet?

PersistentSet has-many ThirdPartyPersistentSets

What is the relationship between PersistentObject and ThirdPartyPersistentSet?

ThirdPartyPersistentSet is dependent on (uses-a) PersistentObject

all of the lines in a uml class diagram indicate dependencies of one sort or another except for the dashed like to a comment (dog-eared box). a solid line with no arrows indicates a two (2) way (bi-directional) dependency.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
0

The black diamond represents composition, containment (as in having a field of the type pointed by the arrow): PersistentSet entities contain ThirdPartyPersistentSet entities When PersistenSet is destroyed, all ThirdPartyPersistenSet objects contained will also be destroyed.

The dashed line represents dependency, as in makes a function call that has a parameter of the type pointed by the arrow): ThirdPartyPersistentSet depends on PersistentObject

Look at the Wikipedia entry for more details

Gustavo Mori
  • 8,319
  • 3
  • 38
  • 52
0

Specifically, what is the relationship between PersistentSet and ThirdPartyPersistentSet?

http://en.wikipedia.org/wiki/Class_diagram#Aggregation

What is the relationship between PersistentObject and ThirdPartyPersistentSet?

http://en.wikipedia.org/wiki/Class_diagram#Dependency

StanislawSwierc
  • 2,571
  • 17
  • 23