Need help reading a UML Diagram - uml

Hello, I've been asked to create a class using the UML Diagram provided above.I'm not quite sure how to read it. I've currently created:
using System;
namespace Packets
{
enum PacketType { ChatMessage, PrivateMessage, ClientName };
public class Packet
{
public PacketType packetType
{
}
Packet:ChatMessagePacket
{
public string message;
public void ChatMessagePacket(string message)
{
}
}
}
But this is incorrect. I'm not sure about the get ~set or ChatMessagePacket:Packet.
Any help is much appreciated.
Many thanks,
Ben.

That get~set is an invention of the UML author and no standard. It would be correct to specify both the getter and setter separately as operations. But basically it would be enough to either type an attribute or use a role with an association towards the enumeration. Making getter/setter is merely an implementation detail and bringing that into the UML makes the design just confusing. Often UML modelers overdo it and (probably) think that coders are just monkeys which they are not.

Related

Can an Air Conditioner remote be considered polymorphic (in context of OOP)?

I know its a weird question but I was asked this in an interview by the CEO of a software house,
First, he asked if a remote could be considered an Object, If yes then explain why?
If it is an object then can it be polymorphic in nature (in the context of OOP) ?
I said no because it can only switch on/off an AC, but he said what if I use it as a weapon and throw it at someone?
Does that make it polymorphic?
Can somebody please explain this?
Yes, it could. Think in a remote controller compatible with Air Coinditioners of the same brand. You can raise or lower the temperature of several models, but you can't for example enable ECO System for all models. You have a base behavior sharing an interface and you have concrete remotes specialized with a very concrete behavior.
And yes, throw a remote as a weapon could be polymorphic as you can think of it as an object. All objects in your house could be throwed against someone. All classes derive from object class (simple inheritance), so all objects could share the object base behavior.
if a remote could be considered an Object
yes, because you can design/model any real world item
https://www.educative.io/blog/object-oriented-programming
can it be polymorphic in nature (in the context of OOP)
yes, you can create abstract class to share behaviours among derived classes. After share behaviour is defined, then you can implement concrete remote controls like Electrolux, LG, Samsung
public abstract class RemoteControl
{
public abstract void TurnOnOff();
}
public class RemoteControl_Electrolux : RemoteControl
{
public override void TurnOnOff()
{
Console.WriteLine("Electrolux is turned on/off");
}
}
public class RemoteControl_Samsung : RemoteControl
{
public override void TurnOnOff()
{
Console.WriteLine("Samsung is turned on/off");
}
}
public class RemoteControl_LG : RemoteControl
{
public override void TurnOnOff()
{
Console.WriteLine("LG is turned on/off");
}
}
and use it:
List<RemoteControl> remoteControls = new List<RemoteControl>();
remoteControls.Add(new RemoteControl_Electrolux());
remoteControls.Add(new RemoteControl_Samsung());
remoteControls.Add(new RemoteControl_LG());
foreach (RemoteControl control in remoteControls)
{
control.TurnOnOff();
}
what if I use it as a weapon and throw it at someone? Does that make it polymorphic
no, it does not make it polymorphic as remote control does not have behaviour of a weapon. And this is a violation of Liskov substitution principle of SOLID principles.

Abstract Class Error in Java

I'm new to Java and I'm running into a compile error I cannot figure out.
Chapter5Debug is not abstract and does not override abstract method itemStateChanged(java.awt.event.ItemEvent) in java.awt.event.ItemListener
public class Chapter5Debug extends Frame implements ItemListener
^
Can anyone help me understand what I need to do to fix this?
Appreciate the help!
Sheila
You have to remember that if ItemListener is abstract, then you will need to implement all the methods inside ItemListener. If you want to keep your current structure, you can just add an empty itemStateChanged method to your Chapter5Debug class.
To elaborate on #kevolution's answer:
public class Chapter5Debug extends Frame implements ItemListener {
public void itemStateChanged(ItemEvent e) {
// Write your method here
}
}
An abstract class is one which is just like a regular class but can contain some 'stub' - or abstract - methods. These are methods which need to be implemented by the class extending the abstract class. In this case, itemStateChanged() is marked as abstract, meaning that you need to declare it.
Abstract classes are useful for when you're writing code which you need the caller to provide guts in some ways. In this case, Java cannot know what to do when the item's state changes, but it can do a whole lot of other stuff. The other stuff is in regular methods in the Frame class, and these call the itemStateChanged() method - which will then invoke your own handling code.
You need o implement itemStateChanged(ItemEvent) within Chapter5Debug
example code
public class Chapter5Debug extends Frame implements ItemListener{
//where initialization occurs
checkbox.addItemListener(this);
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
label.setVisible(true);
...
} else {
label.setVisible(false);
}
}
}
ItemListener is an interface and hence implementing ItemListener means you either you will have to provide the implementation in Chapter5Debug
or
You can make Chapter5Debug abstract and then provide the implementation in the class inheriting Chapter5Debug.
Crux is that if you implementing an interface, You can't get away by not providing the implementation. Either you have to provide it there itself or carry it to the child classes.

Domain Driven Design - Access modifier for domain entities

I am just starting out with domain driven design and have a project for my domain which is structured like this:
Domain
/Entities
/Boundaries
/UserStories
As I understand DDD, apart from the boundaries with which the outside world communicates with the domain, everything in the domain should be invisble. All of the examples I have seen of entity classes within a domain have a public access modifer, for example here I have a entity named Message:
public class Message
{
private string _text;
public string Text
{
get { return _text; }
set { _text = value; }
}
public Message()
{
}
public bool IsValid()
{
// Do some validation on text
}
}
Would it not be more correct if the entity class and its members were marked as internal so it is only accessible within the domain project?
For example:
internal class Message
{
private string _text;
internal string Text
{
get { return _text; }
set { _text = value; }
}
internal Message()
{
}
internal bool IsValid()
{
// Do some validation on text
}
}
I think there's a confusion here: the Bounded Context is a concept which defines the context in which a model is valid there aren't classes actualy named Boundary. Maybe those are objects for anti corruption purposes, but really the Aggregate Root should deal with that or some entry point in the Bounded Context.
I wouldn't structure a Domain like this, this is artificial, you should structure the Domain according to what make sense in the real world process. You're using DDD to model a real world process in code and I haven't heard anyone outside software devel talking aobut Entities or Value Objects. They talk about Orders, Products, Prices etc
Btw that Message is almost certain a value object, unless the Domain really needs to identify uniquely each Message. Here the Message is a Domain concept, I hope you don't mean a command or an event. And you should put the validation in the constructor or in the method where the new value is given.
In fairness this code is way to simplistc, perhaps you've picked the wrong example. About the classes being internal or public, they might be one or another it isn't a rule, it depends on many things. At one extreme you'll have the approach where almost every object is internal but implements a public interface common for the application, this can be highly inefficient.
A rule of the thumb: if the class is used outside the Domain assembly make it public, if it's something internally used by the Domain and/or implements a public interface, make it internal.

DDD Using Specification pattern for Validation

I am thinking of using Specification pattern for validation purposes. The hard thing is how to tell the user why some Specification was not satisfied. What if the Specification.IsSatisfiedBy() will not only return a bool value, but also the reason of failure. It would look something like this:
interface ISpecification<T>
{
CheckResult IsSatisfiedBy(T candidate);
}
where CheckResult is:
class CheckResult
{
public bool IsSatisfied { get; }
public string FailureReason { get; }
}
In Fowler & Evans work there is a concept of Partially Satisfied Specification whose purpose is to provide explanation what exactly was not satisfied. However in that document, it is implemented as additional method remainderUnsatisfiedBy which returns the Specification which was not accomplished by the Candidate.
So the question is: When using Specification for validation purposes, how to provide feedback to user that a given Specification was not satisfied? Is the solution I've presented above good?
Although you may use your Specifications classes for validation, I would suggest you keep them as separate concepts within your domain. You may find that you need to re-use the same underlying specifications but need to return different "Failure Reasons" depending on purpose and context. See this article for more details.
The author of the post referenced above has also kindly shared code to github and posted the code as NCommon. Review these areas in particular:
Specifications: https://github.com/riteshrao/ncommon/tree/v1.2/NCommon/src/Specifications
Validations: https://github.com/riteshrao/ncommon/tree/v1.2/NCommon/src/Rules (especially the classes for ValidationResult and ValidationError)
I had the same problem. I create Validation decorator for Specification (code is JAVA).
interface Validator<T>{
Respond validate(T t)
}
class abstract ValidationSpecificationDecorator<T> implements Validator<T> {
Specification<T> spec;
ValidationSpecificationDecorator(Specification<T> spec){
this.spec = spec;
}
public Respond validate(T t) {
Respond respond = new Respond();
if(!spec.IsSatisfiedBy(t){
respond.add(error(t));
}
return respond;
)
public abstract Error error(T t);
}
Not sure about your language, but in Scala there is a concept (structure): Either[A,B]
And in calculations you either return right side (B) - for successful validation (object satisfy business rule) or left side (A) to express reason of failed validation for a given business object.
For the Specification purpose, you could return (for A type) e.g. String (as an error message) or some objects/enums (or anything which is appropriate in your language) representing business reason of why specified object didn't satisfy the specification.
It's similar to your solution but more functional and robust.

magic GET and SET for object initialization question

I would like to know the best practice for a class oriented DDD.
Since i am doing domain validation in custom setters named ChangeX(string x) i might be pushed to use this as property.
public virtual string example { get;
private set; }
However, that not very good since it disable me from using the object initialization feature such as :
new Object { Example = "Some example"
}
So i though why not passing the custom set into the property set ? like this
public virtual string Example { get {
return Example; } set {
ChangeExample(value); } }
Is this can lead to any problems ? it is against best practices ?
Thanks.
Real problem here is using setters as such. Why do You need them?
When You use setters, You lose isolation - You can modify state of objects from outside w/o them knowing that. That leads to procedural code.
In contrast - You should ask objects to do something (not just modify their state) that would eventually might lead to them changing their own state.
I think this solution is fine. One reason to have setters is to make sure your under laying fields never hold incorrect values.

Resources