Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am beginner and trying to learn by studying material online. I just draw a diagram and want to show you so that you please put me on the right path.
Question-1: Is that drawn correctly
Question-2: How to implement this diagram into Java Code
Trying to build SiteTemplate that has 3 sub classes e.g. (1) different elements like modal, buttons, combo box, table etc (2) Java Script element like error checker and messages (3) all URLs that'll be used in project so that if the site move from one server to another we just change URLs and it start work again
I am trying to that if I inherit SiteTemplate class then have access to all methods of sub classes and their child classes
Best Regards
if I inherit SiteTemplate class then have access to all methods of sub classes and their child classes
No, the fact SiteTemplate is inherited directly or indirectly by other classes does not allow SiteTemplate to have access to the methods of these classes.
In fact this is in the reverse direction, the child classes inherits the public/protected methods of the inherited class(es).
If SiteTemplate correspond to an element of a site your generalizations are right, but what I said above still apply. May be also SiteTemplate is an interface and in this case the generalizations are realizations. In Java you use extends for generalizations and or implements for realizations.
SiteTemplate by default does not know the classes inheriting/realizing it, to make it explicitly knowing them is a bad architecture.
If you want to say a SiteTemplate is composed by any number of ProjectURL and JSElement and HTMLElement the generalizations are wrong and you can use aggregation (or even composition) :
that allows SiteTemplate to access to the elements composing it, and then to apply on these instance the public operations their classes define.
In Java they are attribute, and because the number of instances are unknown you use collections.
Warning, do not name class at plural, so ProjectURL etc whatever these classes have several instances, this is why in my answer I do not use plural
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to make a UML class diagram. By hand, not generated from code. The problem: UML is a very broad specification - I can't map all Java features to how they should look like in the diagram. There are associations, dependencies, aggregations, compositions. They are all well-documented, but not specifically for Java, so:
When should I use which type of connection?
How to handle inner classes (static or not)?
What about static, final, abstract methods/fields? I think I must make it bold/italic/underlined, but how do I map those together?
Abstract classes, Final classes, Enums?
too broad. Try asking for a specific application.
Just nest them.
Use according stereotypes.
Abstract classes are shown with name in italics. Use stereotype for <<final>>. There's a <<enumeration>> meta type.
I use draw.io when I need to make the UML and/or other diagrams. Everything there is manual, you just drag the design and then write the fields,functions, class, etc.
If you don't know when to use connections, classes, etc then go back to learning then try again once you know good enough.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm working on what's basically my first GUI program in Java and it's been working well for me so far. Everything runs smoothly, however I want to make sure I use and understand solid programming principles.
For my GUI class I have about 300 lines of code (which I've come to understand is pretty small). I've been using SWT and this GUI one window for now. I have four tab items that each contain a different set of widgets for use. I intend on having a class for each tab to take care of their respective back end requirements.
As I've searched existing questions I've found the Single responsibility principle referenced on several occasions. To my understanding, one window per class would fit this principle. When (if ever) would I break a GUI into multiple classes outside of multiple windows?
Design principle are guidelines for writing good code. Single responsibility principle says we should have only single source of change for a class.
How do we know what is the source of change in a class ?
What can change in a class ?
Answers to these questions lies with the team directly in touch with the end users. For this it is important to reach to the team interacting with the client as soon as possible with a basic simple design. It is quite obvious , we will be asked to add more new features or add new requirements.
The process above will let us know what is the set of responsibilities that our class is performing is changing. We must put those set of behaviors in separate class(s). Now our existing class should communicate with the newly created classes via. Abstractions. This is dependency inversion. Now our class no longer is dependent upon the entities which change or which can potentially change with high probability. In the abstractions only the behaviors needed by the our old class are put. Implementation details are put in newly created concrete classes which extend the Abstraction class we have created.
From the very beginning, trying to figure out all the responsibilities and putting them in separate classes even when (they may never possibly change) will make the code scattered.
Large classes are verbose. They are not browsable, they have high risk of getting affected with changes un intentionally.
Regarding your specific question on when you would want to break out stuff into a separate class:
Let's assume you write an address book. You would probably want to present a contact's details in various places of the application. Or present multiple contacts at once. This would be accomplished by writing a separate class, like ContactDetailsPanel.
In general, most of the usual patterns apply to GUI classes as well: don't repeat yourself, single responsibility, and so on. One pattern I would like to point out when writing GUI code is MVC: Model-View-Controller. It's basically about separating business logic, presentation, and data.
You might want to take a look at what kind of things get separate classes in SWT, too.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it considered bad form to include a subclass in the same .java file as the superclass? Any inherent advantages or disadvantages of this as opposed to separating them into their own files?
Is it considered bad form to include a subclass in the same .java file as the superclass?
Having the sub class in same .java file that contains a super class is not considered as bad but not recommended. But this opinion varies from person to person so everyone has own opinion.
Any inherent advantages or disadvantages of this as opposed to separating them into their own files?
You know that a single .java file can have only one public class. As from JLS
This restriction implies that there must be at most one such type per
compilation unit. This restriction makes it easy for a Java compiler
to find a named class within a package. In practice, many programmers
choose to put each class or interface type in its own compilation
unit, whether or not it is public or is referred to by code in other
compilation units.
so yes definitely, it can effect the inheritance if you want the sub class to be public as well.
There is no advantage or disadvantage as such.
The main reason behind putting different classes in separate .java files is code organization and maintainability. It is always considered a good practice to put irrelevant classes separately.
For example imagine a scenario when you have a large set of Class and you want to search a particular Class it would be much easier if that Class is written in a separate file rather than incorporating it with other Classes in a same file.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Im new to Java, so i hope you guys will teach me a thing or two. Here we go:
1)Why would i use constructors? I got so many issues with contructors, because i dont know why and how to apply them and i really need practice, before i fully can understand it.
2)How would your constructor look like, when you got:
2 classes - Vehicles and Car. Car needs access to variables, in the Vehicles class, so i can start working on methods with certain variables(that belongs to Vehicles class), from the Car class. I just need to see some examples.
3)I got this from another question(thanks again, for helping me out):
public Vehicles(Car frame) {
this.testingobj = frame;
}
It works, but why does work and what does it do? I use it on every class i create. It seem like it can contact my Car class, when i press a JButton, to activate a method in Vehciles class. Is there anything i should consider in contrast of question )2?
Thanks for your help and time!
A class definition is like a blueprint. It defines the API for the class and what it can do. But a blueprint is just a blueprint. It isn't useful until you actually have something to work with. It's nice to read all the specs on a Bentley; it is even nicer to see one and drive it.
This is where a constructor comes in. It creates a new instance of the class by reserving space in memory for it and initializing it so that it is in a proper state ready to do all the things its class definition promises its clients it can do.
As for your example, I think you need to understand object-oriented design before you worry about the mechanics of constructors. Why would Car need to access a Vehicle? A car is a vehicle.
You need to think about what you are trying to accomplish and understand how objects work together to get things done. Then you can worry about constructors and ultimately Android development.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
To design a project and draw a UML class diagram, what must the class diagram include?
Suppose our classes have textfields, buttons etc. Must they be included as members?
Suppose we need to perform some form validations, and we intend to perform it by passing data obtained from a form to a "validator" object, must it be also included in the class diagram?
I received some opinions from colleagues that a class diagram is for design phase and must not include objects like I mentioned above. However when the project completes, won't there be a large number of objects we did not draw in the class diagram?
UML is a language. The way you use it is up to you.
Ideally you will have multiple documents. The reason you will need multiple documents is because the most important tip of documentation writing is to restrict yourself to one perspective per document.
You want a static representation of objects -> don't talk about files
You want to show relations between objects -> don't talk about data flow.
You get the idea. As long as you are clear with what the purpose of the document is and consistent to the legend, UML can tell any story.
For your specific question:
Since you're creating a class diagram (a static representation of system objects), the important bits will likely be what goes into each object/class (not the input fields of the form itself, but the structure of the object those fields are eventually saved to), and how they relate to other objects.
You can include the validator object and connect it to the objects its validating, but modeling how it's validating, when it's validating, or the protocol with which they communicate is not relevant for this specific view.
Generally in UML diagrams, you exclude extraneous data. Depending on how in-depth you want to be, things such as a UI controls and getter/setter methods are usually excluded.
On the other hand, your Validator object should be defined as a control class in your UML diagram, as it has a responsibility and purpose within your system.