max
max

Reputation: 11

UML and java classes

I do need your valuable help. A few weeks ago I developed a small program in Java. Probably a wrong approach, but I didn't tackle the problem with UML in mind, but on the basis of a given idea I built a fully functional piece of software which lives up to expectations.

The program itself consist of two classes and several methods.

Now I need to draw the corresponding UML class diagram and it seems a very knotty problem for me.

Since the UML class diagram aims at showing the source code dependencies between classes, I'm wondering how I can draw a class diagram based on the following code.

.................................................................

import javax.swing.JPanel; class WavPanel extends JPanel {

        List<Byte> audioBytes;
        List<Line2D.Double> lines;

        public WavPanel() {
            super();
            setBackground(Color.black);
            resetWaveform();
        }

        public void resetWaveform() {
            audioBytes = new ArrayList<Byte>();
            lines = new ArrayList<Line2D.Double>();
            repaint();
        }
}

................................................................

In a nutshell, the WavPanel class is an example of inheritance. The JPanel class (part of a package) acts as a superclass. How can I solve the problem? My concern is that JPanel is a class which is part of a package.

Furthermore, in order to draw a good class diagram am I wrong in thinking that maybe I had to do the UML diagram first and then write the associated code?

The fact that I wrote several methods (instead of classes) and just a few classes, can that be a problem with UML? If so it would mean reengineering the entire software again.

Thanks in advance . Any help will be highly appreciated.

Upvotes: 1

Views: 1519

Answers (2)

duffymo
duffymo

Reputation: 308743

You're not thinking about UML properly.

If your application works, then the fact that the UML is causing you difficulty should not be a concern.

UML is a communication notation, nothing more.

It's not a requirement to do the UML before coding. UML is not a requirement at all. Thinking is required. If creating UML helps you to think, by all means do it. But it's not required.

Your solution is simple: either import JPanel into your UML model and add inheritance to the class diagram, or create a stand-in for the sake of the diagram.

The program itself consist of two classes and several methods.

No, the program consists of two classes that were written by you plus several core Java classes (e.g., JPanel, List, etc.) Are you suggesting that it's important to include every class in your class diagrams, even the Java core classes? I would disagree with that. Perhaps noting that your WavPanel inherits from JPanel would be useful, but I don't add java.lang.String to any UML diagrams that I create.

I wouldn't consider UML to be worth the effort for a solution that consisted of five classes or fewer. Those I can keep in my head. UML tends to help more when I have a larger problem than five classes.

Upvotes: 1

rownage
rownage

Reputation: 2404

Yes, you should always do the planning before the programming. However, all is not lost.

You can still use JPanel in your UML, just show the inheritance (using the white diamond) for subclasses that use JPanel. I like to draw all my UML diagrams using UMLet, especially because (in the future, when you do UML first ;)) you can export your diagrams into Eclipse!

Methods and their arguments are specified inside the classes in a UML diagram. You don't really care about what each method DOES for a class diagram, just its characteristics for the most part. Focus on show the relationships between classes, even if they are libraries. Don't worry about all the individual methods in a library though...some API somewhere has probably taken care of that.

Do you need more help on UML itself? Let me know and I can add to this answer to clarify any other questions you have on UML in general, or consult this Wikipedia article on class diagrams.

Upvotes: 1

Related Questions