I have a situation similar to this:
classA.h:
class A{};
classB.h:
#include "classA.h"
class B{};
Is there a way (and should I do it?) to show that classB uses (includes) classA even though there is no heredity involved? ClassB doesn't even have classA as a member, but I feel like it would make sense to somehow show this in the class diagram.
You can do it two ways. The first is to simply enlarge B so it can hold A like this:
Note the fully qualified name for A.
Another way is to use an import relation:
I try to write ocl to add constraint to child (animal), but the constraint must user parent class name (mamifere). I think the first version doesn't work, and I think there is a nicer solution that the second example. Help me please ?
image for example animal:
That looks strange. Your first constraint tells (provided it's linked to animal) that the type of aninmal must be mamifere, but mamifere inherits from animal. That does not make sense.
The second variant does not make sense either. Provided the constraint applies to anything on the diagram, each instance must be named mamifere2. So you can have only instances with name == mamifere2. Especially strange with a vivipare2 instance.
I don't see where you model any child relation at all. So I'm just guessing you mean this:
A child has two parents (well, for humans there now can be more). And there can be * children which must have the same type as the parents (so you can't model mules or the like).
I am trying to write a module in F#, making it easier working with Excel, by extracting rows, columns, etc. along with type casting and so on. One of the first things I wanted to do, was to extend various classes/types to implement the IDisposable interface. I tried to write something like the following
type Excel.ApplicationClass with
interface IDisposable with
member this.Dispose() =
this.excel.Quit()
Marshal.ReleaseComObject(this.excel) |> ignore
What I wasn't aware of, was that I would get the following error "All implemented interfaces should be declared on the initial declaration of the type".
My question is the following: Since I am not allow to extend a type with an interface - what else could I do?
If you inherit from the base class it can work, like this
type myAppClass() =
inherit Excel.ApplicationClass() //may not be correct signature - you need to match the base constructor
interface IDisposable with
member this.Dispose() =
//function body
I'm trying to figure out if this is possible.
I have a class, BaseGameEntity, from this I currently derive NormalDrop and OtherDrop each of which has an instance of a StateMachine< T > where T is NormalDrop and OtherDrop respectively.
From here relevant states can be called that apply to those kinds of drop.
What I want to do is put another layer in, the Drop class, which derives from BaseGameEntity which the other forms of drop then derive from.
Within Drop I want a StateMachine< T > where the "T" becomes NormalDrop or OtherDrop depending on what is using it as its base class.
Is this actually possible?
Yes, you can use the curiously recurring template pattern:
public class Drop<T> where T : Drop<T> { ... }
public class NormalDrop : Drop<NormalDrop> { ... }
public class OtherDrop : Drop<OtherDrop> { ... }
Then within the Drop base class T is always NormalDrop or OtherDrop.
This pattern generally isn't considered developer friendly as upon first glance it is confusing and there are probably better ways to structure code (though possibly not always). Eric Lippert wrote a very good blog post about this pattern and some of its shortcomings here.
In a UML class diagram, what is the difference between an association relationship and a dependency relationship?
From what I know, an association is a stronger relationship than a dependency, but I'm not sure how it is stronger.
Any example would be more than welcome :)
An association almost always implies that one object has the other object as a field/property/attribute (terminology differs).
A dependency typically (but not always) implies that an object accepts another object as a method parameter, instantiates, or uses another object. A dependency is very much implied by an association.
In OOP terms:
Association --> A has-a C object (as a member variable)
Dependency --> A references B (as a method parameter or return type)
public class A {
private C c;
public void myMethod(B b) {
b.callMethod();
}
}
There is also a more detailed answer.
What is the difference between dependency and association?:
In general, you use an association to represent something like a field
in a class. The link is always there, in that you can always ask an
order for its customer. It need not actually be a field, if you are
modeling from a more interface perspective, it can just indicate the
presence of a method that will return the order's customer.
To quote from the 3rd edition of UML Distilled (now just out) "a
dependency exists between two elements if changes to the definition of
one element (the supplier) may cause changes to the other (the
client)". This is a very vague and general relationship, which is why
the UML has a host of stereotypes for different forms of dependency.
In code terms, such things as naming a parameter type and creating an
object in a temporary variable imply a dependency.
...
Dependency is like when you define a method that takes a String(in Java, C#, as string is a object in them) as a parameter, then your class is dependent on String class.
Association is like when you declare a string as an attribute in your class.
then your code is associated with the string class.
String name = null //: is a association.
Dependency - A change in a class affects the change in it's dependent class. Example- Circle is dependent on Shape (an interface). If you change Shape , it affects Circle too. So, Circle has a dependency on Shape.
Association- means there is a certain relationship between 2 objects
(one-one, one-many,many-many)
Association is of 2 types-
Composition
Aggregation
1) Composition- stronger Association or relationship between 2 objects. You are creating an object of a class B inside another class A
public class A {
B b;
public void setB(){
this.b= new B();
}
}
If we delete class A , B won't exist( B object is created inside A only).
Another example -Body & Liver .Liver can't exist outside Body.
2) Aggregation - weaker type of Association between 2 objects.
public class A {
B b;
public void setB(B b_ref){
this.b= b_ref;
/* object B is passed as an argument of a method */
}
}
Even if you delete class A, B will exist outside(B is created outside and passed to Class A)
Another example of this- Man & Car . Man has a Car but Man & Car exist independently.
Here: "Association vs. Dependency vs. Aggregation vs. Composition", you have a great vade mecum with uml class diagrams and code snippets.
The author gives us a list of relationships: Association, Dependency, Aggregation, Composition in one place.
A dependency is very general and lowering complexity is about diminishing dependencies as much as possible.
An association is a strong (static) dependency. Aggregation and Composition are even stronger.
I was always checking this answer as it didn't stick in my mind. I found this one more helpful after reading the accepted answer
Association is when one object just has a link to another and don't use relational object methods. For ruby for example
class User
has_one :profile
end
user = User.first
profile = user.profile
profile.sign_out
It means you can get a profile object from user but user don't use profile's methods inside himself(has no dependency on a Profile's interface).
Dependency means that User has link to another object and call that object's methods inside himself
class User
has_one :profile
def personal_info
profile.info
end
end
Here if Profile's info method will be changed or renamed our Dependent User class also need to be changed.