patternjavaMinor
Did I properly encapsulate this package?
Viewed 0 times
thispackageproperlydidencapsulate
Problem
My goal is to make File_Path_Handler constructor the only thing a user can access. Oh, is it too coupled? but the main question is: is File_Path_Handler the only accessible part of this package?
The handler:
The display:
The ActionLi
The handler:
package file_path_handler;
//| File_Path_Handler
//| - a component that lets the user pick a file,
//| then the file path will be displayed accordingly
//| imports
import javax.swing.JPanel; //? the base class
import java.awt.BorderLayout; //? the organizing layout
public class File_Path_Handler extends JPanel {
private File_Path_Load_Button button;
private File_Path_Display display; //? the UI part
private File_Path_Load_Button_Click_Listener trigger;
private File_Path_Relay relay; //? the internal logic part
//| constructor
public File_Path_Handler( final String button_label, final String default_display_label ) {
this.display = new File_Path_Display( default_display_label ); //? the UI part
this.relay = new File_Path_Relay( display );
this.trigger = new File_Path_Load_Button_Click_Listener( relay ); //? the logic that wires all the UI together
this.button = new File_Path_Load_Button( button_label, trigger ); //? the other UI part
this.setLayout( new BorderLayout() );
this.add( button, BorderLayout.WEST );
this.add( display, BorderLayout.CENTER );
}
}The display:
package file_path_handler;
// imports
import javax.swing.JTextField; //? the base class
//| File_Path_Display
//| - displays the current selected file
class File_Path_Display extends JTextField { //? the public interface will be the JPanel that houses this couplet, this can stay hidden in the package
public File_Path_Display( final String default_label ) {
this.setText( default_label );
this.setEnabled( false );
}
public void update_text( final String file_path ) {
this.setText( file_path );
}
}The ActionLi
Solution
package file_path_handler;Package names should be in the format
tld.organization.application.package. For example like this:package com.company.application.package;
package com.github.username.application;
package com.gmail.username.application;This can be ignored for test applications and similar, but the moment you release code "into the wild" you should make sure that it is in a correct package.
//| File_Path_Handler
//| - a component that lets the user pick a file,
//| then the file path will be displayed accordinglyWhat is this? You should always use JavaDoc to document your code. Not some obscure self-made format that is incompatible with everything.
If in doubt, look at the documentation of the language you're using. Never forget, you're not the first to use that language, you're not the first who needs whatever you need, somebody else already did it and they did it better in 99% of the cases.
//| imports
import javax.swing.JPanel; //? the base class
import java.awt.BorderLayout; //? the organizing layoutAgain, these comments are not helpful in any way and should be left out.
public class File_Path_Handler extends JPanel {This violates the Java Naming Conventions which state the classes should be
UpperCamelCase.private File_Path_Load_Button button;This violates the Java Naming Conventions which state that the variables should be
lowerCamelCase.Also prefixing every variable with the class name is not helpful and not a good idea. It only clutters up everything and having a "common prefix" for variables is the whole idea of classes in the first place.
public File_Path_Handler( final String button_label, final String default_display_label ) {This violates the Java Naming Conventions which states the functions should be
lowerCamelCase with the exception of constructors which mimic the class name. Also variable names.Code Snippets
package file_path_handler;package com.company.application.package;
package com.github.username.application;
package com.gmail.username.application;//| File_Path_Handler
//| - a component that lets the user pick a file,
//| then the file path will be displayed accordingly//| imports
import javax.swing.JPanel; //? the base class
import java.awt.BorderLayout; //? the organizing layoutpublic class File_Path_Handler extends JPanel {Context
StackExchange Code Review Q#45777, answer score: 5
Revisions (0)
No revisions yet.