Here is the list of Java Design Patterns. I am putting all together for now, will try to add explanation and code samples shortly -
Creational Patterns
- Abstract Factory
- Builder
- Factory
- Prototype
- Singleton
Structural Patterns
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
Behavioral Patterns
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
Java
Design Patterns, Java
Lets see how we can sort our Employee objects using different criteria like id, name, salary.
SortTest remains the main class that will perform creating and filling objects for us. This time employee class is plain old java classs that holds some data about each employee we create.
Here I have created scenarios or you can say strategies OR classes for sorting as SortById, SortByName, SortBySalary. Each of above class implements comparator interface and overrides compare method, which takes two objects to compare and return eithre -1, 1 or 0.
Note: I have added few helper functions that really helps while coding like print - this takes array list of objects and prints in format I want instead of class id and all that. toString - overridden this function so I can return the class string as required
Here goes the code -
Read more…
Java
comparator, Java, sorting
In Java you can do sorting by implementing 2 interfaces
1) Comparable
2) Comparator
Here we will see sorting example using Comparable Interface. As an example we will be sorting objects of Employee class.
When using comaprable interface make sure the object class
which you are sorting implements comaprable interface and override compareTo method correctly.
As I was more used to Java 1.4, I didnt followed any Java Generics implemetation.
SortTest is main class that will actually creaate an array list and fill it with some random Employee objects.
Employee is class that will implement compareTo method. I have added simple if-else control structure that will decide what to return on the basis of employee id.
less : -1 equal : 0 greater : 1
Here is the code for classes:
Read more…
Java
comparable, comparator, Java, java generics, sorting
Here I’m going to post a class that will extract all valid urls from a web page. My class uses “URLConnectionReader” provided by Sun Tutorial
Class defines 2 constructors.
- One by default returns you the vector containing only text/html url objects from page.
- For the other you can specify the type of urls you want from a page. This is helpful when you want to get all images, videos or any other media urls.
The class also considers relative urls. It returns relative urls with http and host name prefixed.
E.g. If you have urls like “/about.php”, then class will return “http://hostname.domain/about.php”
Read more…
Java
crawler, Java, url
In my last post I mentioned “Config Class”couple of times.. here you will see actual class and its usage. The class is very simple. We just need to provide proper path to config file we going to use.
The Config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| import java.util.*;
import java.util.Properties;
public class Config
{
Properties configFile;
public Config()
{
configFile = new java.util.Properties();
try {
configFile.load(this.getClass().getClassLoader().
getResourceAsStream("myapp/config.cfg"));
}catch(Exception eta){
eta.printStackTrace();
}
}
public String getProperty(String key)
{
String value = this.configFile.getProperty(key);
return value;
}
} |
You can get any property/settings from config with method ‘getProperty’
Here is my config file
#This is comment
mDbUser = myuser
mDbHost = myserver
mDbPwds = mypwd
mDbName = mydb
Usage:
1
2
| cfg = new Config();
dbname = cfg.getProperty("mDbUser"); |
Hope you find this useful..
Java
config, Java