A design pattern is a general design solution to a common software problem.
There are three types of design patterns:
Here are the most common creational patterns:
It's a design pattern that separates the logic from the data. It uses a visitor object to perform operations on different groups of objects, called visitable objects.
Here's an example:
/** * Visitor used to return the net weight * of different items */ public interface Visitor { int visit(Meat visitor); int visit(Fish visitor); } /** * Visitable interface to calculate * the net weight on a visitor */ public interface Visitable { int accept(Visitor visitor); } /** * Contains the algorithms to find out the
It's a design pattern used to store previous states of an object, to be restored at a later time. That's what happens when a text editor undoes and redoes changes.
The memento pattern is composed by three objects:
Here's an example of the pattern:
/**
Here's how to print one-dimensional arrays:
System.out.println(Arrays.toString(arr));
Here's how to print multi-dimensional arrays:
System.out.println(Arrays.deepToString(arr));
In both cases, toString() is called on each element. When the elements of an array are instances of a class that you wrote yourself, make sure to override toString().
In Java the heap is broken down into different generations:
Generations make garbage collection more efficient. Short-lived objects are garbage collected more often, because higher is the chance they lose all the references.
Both System.arraycopy() and Arrays.copyOf() can be used to copy an array.
Here are the peculiarities of System.arraycopy():
Here's an example:
int[] src = {1,2,3}; int[] dest = new int[5]; System.arraycopy(src, 0, dest, 0, 3); System.out.println(Arrays.toString(dest));
Here are the peculiarities of Arrays.copyOf():
In Unix and Linux, a Symbolic link is a file that links another file or directory.
An hard link is a file that links an inode.
An inode is a Unix object that represents a file or directory.
Symbolic link -> Hard link -> inode -> physical location
All files and directories are hard links. When you create a hard link, you create a second file pointing to the same inode.
Here's how to create a hard link:
ln source_file_name link_file_name
Here's how to create a symbolic link:
ln -s source_file_name link_file_name
Both are used to read characters from a stream, but InputStreamReader allow to specify the charset.
In other words, FileReader extends InputStreamReader with some limitations:
public FileReader(String fileName) throws FileNotFoundException { super(new FileInputStream(fileName)); }
Don't use FileReader if you want your code to be portable and avoid corrupted data.
Specify instead the charset, using InputStreamReader:
InputStream inputStream = new FileInputStream(txtFilePath);
It is possible to read and write to a URL as a stream, using java.net.*
Here's an example to download and print a web page:
public void printUrlSource(String urlStr) throws IOException { // Create a URL object URL url = new URL(urlStr); // Open a connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); try ( // Open a stream to read from the URL BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream())); ) { // Print the content line by line String line;
Preferences are used to save and share settings per platform, between multiple sessions or executions. Imagine the case the user want to remember the colour of the background.
First you have to create a preference object from java.util.prefs.Preferences:
Preferences prefs = Preferences.userRoot().node(this.getClass().getName());
Values are stored in nodes. Here's how to write a new value:
prefs.put("MyKey", "My value");
Here's how to read a value (returns the default, if not found):
prefs.get("MyKey", "Default value");
Copyright © 2013 Welcome to the website of Davis Fiore. All Rights Reserved.