UML relationship of a static call from another class - uml

I am creating a class diagram but I was wondering if there would be any association between the 2 classes shown below - as far as I understand it, for association, ClassA must have an instance of ClassB which in this case there is not, however, it does need to know about a variable of ClassB, so is there an association between these 2 classes?
public class ClassA()
{
int val = ClassB.x
}
public class ClassB()
{
public static int x = 5;
}

Sure there is association. You can't use ClassA without existing of ClassB.

Yes there is an association between these two classes. The association is neither an aggregation nor a composition, it is a "uses/usage" dependency.
ClassA ------Uses-----> ClassB
Take a look at this link to know more about different types of dependencies

Related

Suggestions how to break up projects with #XmlSeeAlso limiting me

I'd like to start breaking apart a large domain project into various smaller sub projects. The issue is that I have an external project that has some code that needs to unmarshal some xml into a top level base class that all the subprojects can share as a common base. This would be fine, except that I will need an #XmlSeeAlso on the base class that would include every possible subclass (so it seems.) This seems to make it impossible for me to break up the projects in the way I planned.
For example, using animals just for illustration.
Without the XmlSeeAlso on Animal defining the subclasses, I'm pretty much stuck.
//Core project jar
public abstract class Animal {
}
//Mammals jar
#XmlSeeAlso({ Dog.class, Cat.class})
public abstract class Mammal extends Animal {
}
//Birds jar
#XmlSeeAlso({ Sparrow.class, Hawk.class})
public abstract class Bird extends Animal {
}
//Third party jar
//use Bird or Mammal xml
input = new ByteArrayInputStream(birdXml.getBytes());
JAXBContext jc = JAXBContext.newInstance(Animal.class);
u = jc.createUnmarshaller();
//return Animal - FAILS since Animal would need the XmlSeeAlso of subclasses
Animal animal = (Animal)u.unmarshal(input);
I didn't realize that JAXBContext.newInstance can take an array of classes. So in my third party jar I just need to load them all into some common Array and pass them in there to the newInstance method ... JAXBContext.newInstance(myClassesArray);

How to instantiate classes only from inside another class in Haxe

I need to prevent class A from being instantiated anywhere but only from another class B, then class B can return the created instance of class A which can be used in any other class.
I understand that B could be a Factory in this example, I looked in the factory pattern in the Haxe code cookbook but it does not seem suit what I am looking for.
In my example class B is doing some work then should return the result in an instance of class A.
no one should be able to create an instance of class A because it is the result of the work that class B performs. anyone needs an instance of A should ask B to do the work and return the resulted A instance
hope I explained it clearly
You would usually do this by using #:allow() metadata in combination with a private constructor:
A.hx:
class A {
#:allow(B)
private function new() {}
}
B.hx:
class B {
public static function create():A {
return new A(); // compiles
}
}
Trying to instantiate A outside of B will result in a compiler error:
class Main {
static function main() {
new A(); // Cannot access private constructor of A
}
}
Note that it's still possible to work around this by using #:access() or #:privateAccess metadata - in Haxe, nothing is ever truly private. It follows a philosophy of "the programmer knows best", which can be very powerful.
Also, you might want to declare A as #:final so nothing can subclass it, because subclasses can access private fields in Haxe. But again, this can be overriden with #:hack metadata.

UML class diagram for static variable from other class

As I know, if the static method from other class, we may interpret their relationship with dependency, just like the answer from How to show usage of static methods UML Class Diagram
However, how about for the static variable from other class? Is it the similar case; using dependancy?
For example,
class A{
public static String CHAR="Charecter";
public static String INT="Integer";
public static String STR="String";
}
class B{
public String Type;
public B(){
Type=STR;
}
public void B(String t){
Type=t;
}
}
would it result in the uml class diagram as below?
Note that although I would rather use enumeration in real life for this case, I just would like to know how it works.
Yes, this is similar.
Dependency shows that one class is "aware" of some other class and uses it in some way (or more generally depends on it). It can for instance refer to (public) static attributes, static operations and so on.

Creating object of parent child class together in one line?

I wrote this but i can't understand what is this.
Public ClassA {
//some methods here
}
Public ClassB extends ClassA {
Public static void main(String[] args) {
ClassA abc=new ClassB
//What is purpose of this line and what advantage it gives us. I accidently wrote this but compiler (Eclipse not generating any error on this statement).
}
You happened to stumble across the distinction of static and dynamic type for a variable and its connection to the inheritance relation.
Your variable abc has a static type ClassA. Hence the compiler will only let you use methods defined in that class on that variable. After your initialization of abc, it has dynamic type ClassB.
Since ClassB extends ClassA, it has every method and attribute of ClassA (and maybe more) and it is OK to use is through abc.

Calling one specific overriden method in all derived classes

Consider the following code:
// ======== Abstract class ========
public abstract class Creatures {
public abstract void loseEnergy();
public void execute()
{
loseEnergy();
}
}
// ======== Animals ========
public class Animals : Creatures
{
public override void loseEnergy(){}
}
public class Birds : Animals
{
public override void loseEnergy(){}
}
// ======== Human ========
public class Human : Creatures
{
public override void loseEnergy(){}
}
public class Male : Human
{
public override void loseEnergy(){}
}
public class Female : Human
{
public override void loseEnergy(){}
}
[ This code was based on the code by Jayson suggested here: "Base class methods calling derived class methods ?" ]
In the given code example, I would like to have the runtime executing EACH derived class object's certain method, in this case, which is 'loseEnergy()', however, I could not find the solution.
How do I approach this problem?
What can be useful to know or to try.. in order to solve this issue?
Your help is very much appreciated!
Thank you!
Kind regards,
Segara
P.S. Some search I have done so far:
"How to call overriden methods in all derived classes"
"Collection of derived classes that have generic base class"
"How to call derived function using base class object"
"Call method of the derived class through reflection possible or no"
EDIT:
I decided to stick to the idea I had before which is to have some list that would contain the objects of the classes that have 'loseEnergy()' method. Having such list I will be able to call every object's method 'loseEnergy()', which is what I wanted.
Question can be closed.
Thank you.
I didn't really understand your problem but anyway i can try to give you some means to use abstract classes :
If you use a abstract method, you SHOULD override it in a subclasses (like a method declared in an interface)
If you want that all inherited class use a same method, you can implement it in the abstract class ; all subclasses will use the method you implements if you don't override it, you've have to not declare it in the subclasses (extends < ABS_CLASS > is good enough)
If you want use a method of the abstract class which is override in the sub class you can use the keyword super .
I hope it will help you.
if you mean that you want the calls: female.loseEnergy() -> human.loseEnergy() -> creature.loseEnergy(), call the base method in the first line of the overriden one
public class Female : Human
{
public override void loseEnergy()
{
base.loseEnergy();
// do stuff
}
}
In the Greenfoot environment that you mention in the post above, the act() method is called only on actors which have been added into the "world". Internally, this adds them into a list. The simulation process iterates through the list and calls act() on each object in turn. Objects that are not "in the world" are not known to the system and so do not have their act method called. There is no magic here going on here.
If you wanted similar behaviour but without manually adding objects into a list, you could possibly have the base class constructor add new objects into a global list. I don't know C# so I don't know precisely how to do this, but I cannot imagine it would be difficult.

Resources