[Java Design Pattern] - Interface
Sunday, October 8, 2017
Interface
"An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface." - tutorialspoint
Note:
1. We cannot instantiate an interface.
Example:
2. An interface does not contain any constructor.
3. All of methods in an interface are abstract. That mean we cannot implement methods.
4. The variable of an interface are must be declared both static and final.
Example:
Incorrect:
Correct
public static final int MAX = 100;
5. An interface is not extended by a class. It only implemented by a class.
6. An interface can extend multiple interfaces.
The example how to use Interface and benifit for your business.
ISearch.java
package com.desginpattern.learn.chapter3.interfaces;public interface ISearch<T> {
/**
* Linear Search
* @param index
* @return
*/
public T linearSearch(String index);
/**
* Binary Search
* @param index
* @return
*/
public T binarySearch(String index);
}
Search.java
package com.desginpattern.learn.chapter3.helper;
import java.util.ArrayList;
import java.util.List;
import com.desginpattern.learn.chapter3.interfaces.ISearch;
import com.designpattern.learn.chapter3.person.Employee;
public class Search implements ISearch<Employee> {
private List<Employee> employes;
public Search(List<Employee> employes) {
super();
this.employes = employes;
}
@Override
public Employee linearSearch(String index) {
for (Employee employee : employes) {
if (employee.getName().equals(index)) {
return employee;
}
}
return null;
}
@Override
public Employee binarySearch(String index) {
List<Employee> temp = new ArrayList<Employee>();
temp = orderByName();
int left = 0;
int middle = temp.size()/2;
int right = temp.size() - 1;
if (index.compareTo(temp.get(left).getName()) < 0 ||
index.compareTo(temp.get(right).getName()) > 0) {
return null;
}
while (left != right) {
// index > middle
if (index.compareTo(temp.get(middle).getName()) > 0) {
left = middle;
} else if (index.compareTo(temp.get(middle).getName()) < 0) {
right = middle;
} else {
return temp.get(middle);
}
}
return null;
}
private List<Employee> orderByName() {
List<Employee> temp = new ArrayList<Employee>();
temp = this.employes;
Employee emTemp = new Employee();
for (int i = 0; i < this.employes.size(); i++) {
for (int j = 0; j < this.employes.size(); j++) {
if (temp.get(i).getName().compareTo(temp.get(j).getName()) < 0) {
emTemp = temp.get(i);
temp.set(i, temp.get(j));
temp.set(j, emTemp);
}
}
}
return temp;
}
public void setEmployes(List<Employee> employes) {
this.employes = employes;
}
}
SearchTest.java
package com.desginpattern.learn.chapter3.helper;
import java.util.ArrayList;
import java.util.List;
import com.desginpattern.learn.chapter3.interfaces.ISearch;
import com.designpattern.learn.chapter3.category.CategoryA;
import com.designpattern.learn.chapter3.person.Employee;
public class SearchTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp1 = new Employee();
Employee emp2 = new Employee();
Employee emp3 = new Employee();
CategoryA cate1 = new CategoryA(35, 0.3);
CategoryA cate2 = new CategoryA(35, 0.3);
// initialize emp1
emp1.setName("Tri");
emp2.setSalaryCaculator(cate1);
// initialize emp2
emp2.setName("Minh");
emp2.setSalaryCaculator(cate1);
// initialize emp3
emp3.setName("Nhan");
emp3.setSalaryCaculator(cate2);
// employee who you want to search in List.
Employee index = new Employee();
index.setName("Nhan");
index.setSalaryCaculator(cate1);
List<Employee> empList = new ArrayList<Employee>();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
ISearch search = new Search(empList);
Object result1 = search.binarySearch(index.getName());
Object result2 = search.linearSearch(index.getName());
if (result1 != null) {
System.out.println("Found this empolyee: " + result1.toString());
} else {
System.out.println("Not found this employee in List by Binary Search");
}
if (result2 != null) {
System.out.println("Found this employee: " + result1.toString());
} else {
System.out.println("Not found this employee in List by Linear Search");
}
}
}
Bài liên quan
Comments[ 0 ]
Post a Comment