Table of contents
LRU cache
aviatar expression uses LRU cache
- com.googlecode.aviator.utils.LRUMap
Use the linked list map: LinkedHashMap
to rewrite the removeEldestEntry method (delete the oldest element): the current size is greater than the maximum size:maxCapacity. That is, it conforms to LRU (Least Recently Used), that is, the least recently used, that is, select the one that has not been used for the longest time to be eliminated.
public class LRUMap<K, V> extends LinkedHashMap<K, V> {
static final long serialVersionUID = -1L;
private final int maxCapacity;
public LRUMap(final int maxCapacity) {
super(16, 0.75f, true);
if (maxCapacity <= 0) {
throw new IllegalArgumentException("Invalid maxCapacity: " + maxCapacity);
}
this.maxCapacity = maxCapacity;
}
@Override
public V remove(final Object key) {
return super.remove(key);
}
@Override
public int size() {
return super.size();
}
@Override
public V put(final K k, final V v) {
return super.put(k, v);
}
@Override
public V get(final Object k) {
return super.get(k);
}
@Override
protected boolean removeEldestEntry(final java.util.Map.Entry<K, V> eldest) {
return this.size() > this.maxCapacity;
}
}
Java dynamic proxy and asm dynamic proxy
easy-rule uses ordinary java dynamic proxy to implement rule engine; aviatar uses asm to dynamically generate expression class has been executed
java dynamic proxy
Proxy classes all inherit Proxy. Due to java's single inheritance, if you want to extend a class, you must base it on the interface; use the internal reflection mechanism of java to achieve
asm dynamic proxy
ASM is a Java bytecode manipulation framework. It can modify existing classes or dynamically generate classes in binary form, and directly generate new .class files
Visitor mode
Features of visirot mode
The visitor pattern decouples the data structure from the operations on the structure, so that the set of operations can evolve relatively freely.
The visitor pattern is suitable for systems where the data structure is relatively stable and the algorithm is easy to change . Because the visitor pattern makes it easy to increase algorithmic operations. If the system data structure objects are easy to change and new data objects are often added, it is not suitable to use the visitor mode.
The advantage of the visitor pattern is that it is easy to add operations, because adding operations means adding new visitors. The visitor pattern concentrates relevant behaviors into a visitor object, and its changes do not affect the system data structure. The disadvantage is that it is difficult to add new data structures.
Example code:
Different cars are visited by different groups of people:
Accessed object abstraction
public interface Car {
String getName();
/**
* 接受访问者的访问
* @param visitor 访问者对象
*/
void accept(Visitor visitor);
}
visitor
public interface Visitor {
void visitElectricityCar(ElectricityCar electricityCar);
void visitOilCar (OilCar oilCar);
}
specific interviewee object
public class ElectricityCar implements Car {
private String name;
public ElectricityCar(String name){
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public void accept(Visitor visitor) {
visitor.visitElectricityCar(this);
}
}
public class OilCar implements Car {
private String name;
public OilCar(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public void accept(Visitor visitor) {
visitor.visitOilCar(this);
}
}
- specific visitor
public class SaleVisitor implements Visitor{
@Override
public void visitElectricityCar(ElectricityCar electricityCar) {
System.out.println("销售查看详情:" + electricityCar.getName());
}
@Override
public void visitOilCar(OilCar oilCar) {
System.out.println("销售查看详情:" + oilCar.getName());
}
}
public class ConsumerVisitor implements Visitor{
@Override
public void visitElectricityCar(ElectricityCar electricityCar) {
System.out.println("消费者查看价格:" + electricityCar.getName());
}
@Override
public void visitOilCar(OilCar oilCar) {
System.out.println("消费者查看价格:" + oilCar.getName());
}
}