Learning UML Sequence diagrams with UMLPlant.
I am not sure that my diagram is right, and how to draw washingService.AddToWashingQueue(car);?
UML plant code
#startuml UML
Program -> Program : Main
Activate Program
create "washingService:WashingService"
Program -> "washingService:WashingService" : new
create "car:Car"
Program -> "car:Car" : new
Program -> "car:Car" : GetCarSerialNumber
Program -> "car:Car" : GetSpeed
#enduml
namespace ConsoleApp1
{
interface IEngine
{
int GetSpeed();
}
public class Car : IEngine
{
public int GetSpeed()
{
return 42;
}
public string GetCarSerialNumber()
{
return "RT87J324";
}
}
public class WashingService
{
private Queue<Car> queue = new Queue<Car>();
public void AddToWashingQueue(Car car)
{
queue.Enqueue(car);
}
}
class Program
{
public static void Main(string[] args)
{
WashingService washingService = new WashingService();
Car car = new Car();
var serialNumber = car.GetCarSerialNumber();
var speed = ((IEngine)car).GetSpeed();
washingService.AddToWashingQueue(car);
}
}
}
just send the message AddToWashingQueue(car) to washingService
so
Program -> "washingService:WashingService" : AddToWashingQueue(car)
to have
#startuml
Program -> Program : Main
Activate Program
create "washingService:WashingService"
Program -> "washingService:WashingService" : new
create "car:Car"
Program -> "car:Car" : new
Program -> "car:Car" : GetCarSerialNumber
Program -> "car:Car" : GetSpeed
Program -> "washingService:WashingService" : AddToWashingQueue(car)
#enduml
Related
I have to deal with code that can be reduced as follows:
void main ()
{
class MyBase
{
public:
virtual void MyFunc () = 0;
};
class MyClass : public MyBase
{
public:
virtual void MyFunc () {}
};
MyClass x;
}
When compiling under Visual-C++ 2010 with `cl /nologo /Wall text.cxx', I get the following warning:
test.cxx(7) : warning C4101: 'main::MyBase::MyFunc' : unreferenced local variable
I want to keep MyBase in place if possible (because it is deep down inside the code hierarchy).
Is there a way to 'fix' the code in place (other than using #pragma to ignore the warning)? Or is it bad practice to have a local abstract class?
Thanks in advance!
I have a simple example that seems like it should work:
import CoreData
#objc protocol CoreDataModel {
#optional class func entityName() -> String
}
class AbstractModel: NSManagedObject, CoreDataModel {
class func create<T : CoreDataModel>(context:NSManagedObjectContext) -> T {
var name = T.entityName?()
var object = NSEntityDescription.insertNewObjectForEntityForName(name, inManagedObjectContext: context) as T
return object
}
}
So we have a class called AbstractModel which conforms to the protocol CoreDataModel, and CoreDataModel defines an optional class method called entityName.
However, this line:
var name = T.entityName?()
causes the error:
Expected member name or constructor call after type name
Any idea what I'm doing wrong?
Edit
Removing the word #optional from the declaration and changing the function a bit allows the code to compile, but now I get a runtime error saying that the
'Swift dynamic cast failed'
#objc protocol CoreDataModel {
class func entityName() -> String
}
class AbstractModel: NSManagedObject, CoreDataModel {
class func entityName() -> String {
return "AbstractModel"
}
class func create<T : CoreDataModel>(aClass:T.Type, context:NSManagedObjectContext) -> T {
var name = aClass.entityName()
var object = NSEntityDescription.insertNewObjectForEntityForName(name, inManagedObjectContext: context) as T
return object
}
}
I cannot explain why your code causes a runtime exception. But it works if you change
the function prototype
class func create<T : CoreDataModel>(aClass:T.Type, context:NSManagedObjectContext) -> T
to
class func create<T : NSManagedObject where T: CoreDataModel>(aClass:T.Type, context:NSManagedObjectContext) -> T
Assuming that your managed object subclass conforms to the protocol, for example
extension Event : CoreDataModel {
class func entityName() -> String {
return "Event"
}
}
then this works and creates a new object:
let newManagedObject = AbstractModel.create(Event.self, context: context)
Alternatively, you could use the approach from the answer to
"Swift: return Array of type self" and
define an extension to the NSManagedObjectContext class:
extension NSManagedObjectContext {
func create<T : NSManagedObject where T : CoreDataModel >(entity: T.Type) -> T {
var classname = entity.entityName()
var object = NSEntityDescription.insertNewObjectForEntityForName(classname, inManagedObjectContext: self) as T
return object
}
}
Then a new object would be created as
let newManagedObject = context.create(Event.self)
From "The Swift Programming Language"
Because T is a placeholder, Swift does not look for an actual type called T.
As T is not a real type, it is maybe not useful to cast to T.
What is UML sequence diagram of the following code featuring a class with two inner classes where each one is instantiated once as seen in the main function?
class A{
class B{
C f(){}
}
class C{}
static void main(){
A a = new A()
B b = new B();
C c = new C();
c = b.f();
}
}
You could use an automated sequence diagram generator in Eclipse such as Diver: Dynamic Interactive Dynamic Interactive Views For Reverse Engineering. It generates both static and dynamic sequence diagrams and looks to answer your question.
I adjusted your code a bit to make it compile and used Diver to generate a sequence diagram:
That is the sequence diagram for this code:
package org.testing;
public class A {
static class B
{
C f() {
return new C();
}
}
static class C {
}
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
c = b.f();
}
}
I have a problem with calling an overriden method from java class.
I have the following Java class:
public class Base
{
int state = 0;
public void called()
{
System.out.println("Hello, from called method: " + state);
}
public String getFirst()
{
return "From Base;
}
//
...
//
}
I use a groovy script to override getFirst() that so that it calls called()
def base = [ getFirst : {
called() // this line has an error
"From Second"
}] as Base
base.getFirst()
How do I implement the this?
You can't use the proxy magic in that way... At the time of the Maps declaration, it doesn't know it's going to be a Proxy for Base, so it will throw the error
Why not just do it the normal way?
def base = new Base() {
public String getFirst() {
called()
"from me"
}
}
I have de folowing classes :
[DataContract]
public class MyProject
{
[DataMember(Name = "Branches")]
private SortedSet<ModuleFilter> branches = new SortedSet<ModuleFilter>(new ModuleFilterComparer());
[DataMember(Name="VbuildFilePath")]
private string buildprogram = null;
}
I can serialize it to a file with :
DataContractSerializer x = new DataContractSerializer(p.GetType());
using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(p.GetFilePath()))
{
x.WriteObject(writer, p);
}
But when I try to read it back with the folowing piece of code, it fails unless I add a dummy implementation of IComparable to the ModuleFilter object
DataContractSerializer x = new DataContractSerializer(typeof(MyProject));
using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(filePath))
{
p = (MyProject)x.ReadObject(reader);
}
Why does not the deserializer use the provided IComparer of the SortedSet member ?
Thank you
It is because DataContractSerializer uses default constructor of SortedSet to initialize field.
Solution 1: recreate field after deserialization with needed comparer
[DataContract]
public class MyProject : IDeserializationCallback
{
//...
void IDeserializationCallback.OnDeserialization(Object sender)
{
branches = new SortedSet<ModuleFilter>(branches, new ModuleFilterComparer());
}
}
Solution 2: use your own sorted set implementation instead of SortedSet<ModuleFilter>
public class ModuleFilterSortedSet : SortedSet<ModuleFilter>
{
public ModuleFilterSortedSet()
: base(new ModuleFilterComparer())
{
}
public ModuleFilterSortedSet(IComparer<ModuleFilter> comparer)
: base(comparer)
{
}
}