Java Reference
In-Depth Information
// Create a file filter to exclude any .SYS file
FileFilter filter = file -> {
if (file.isFile()) {
String fileName = file.getName().toLowerCase();
if (fileName.endsWith(".sys")) {
return false;
}
}
return true;
};
// Pass the filter object to listFiles() method
// to exclude the .sys files
File[] list = dir.listFiles(filter);
for (File f : list) {
if (f.isFile()) {
System.out.println(f.getPath() + " (File)");
}
else if (f.isDirectory()) {
System.out.println(f.getPath() + " (Directory)");
}
}
}
}
C:\WINDOWS (Directory)
...
The Decorator Pattern
Suppose you need to design classes for a bar that sells alcoholic drinks. The available drinks are rum, vodka, and
whiskey. It also sells two drink flavorings: honey and spices. You have to design classes for a Java application so that
when a customer orders a drink, the application will let the user print a receipt with the drink name and its price.
What are the things that you need to maintain in the classes to compute the price of a drink and get its name? You
need to maintain the name and price of all ingredients of the drink separately. When you need to print the receipt, you
will concatenate the names of all ingredients and add up the prices for all ingredients. One way to design the classes
for this application would be to have a Drink class with two instance variables: name and price . There would be a class
for each kind of drink; the class would inherit from the Drink class. Some of the possible classes would be as follows:
Drink
Rum
Vodka
Whiskey
RumWithHoney
 
Search WWH ::




Custom Search