String Match & Observer & Decorator & A* Search Algorithm & RW Lock

String Match

Rabin-Karp

原理很简单,就是计算出pattern的hash值,然后截取text里面的substring,计算hash值。一般O(m+n)。n是text长度,m是pattern长度。但是worst case是O(mn)。当有多个pattern的时候比较好用,因为一个hashtable可以存多个hash值,一起挨个比较。该方法多用于判断抄袭。

Aho-Corasick

本质上是trie tree,实际上制作状态机,比较复杂。

KMP


Observer Design Pattern

http://javarevisited.blogspot.com/2011/12/observer-design-pattern-java-example.html

import java.util.ArrayList;

interface Observer {
       public void update(float interest);
}

interface Subject {
       public void registerObserver(Observer observer);

       public void removeObserver(Observer observer);

       public void notifyObservers();
}

class Loan implements Subject {
       private ArrayList<Observer> observers = new ArrayList<Observer>();
       private String type;
       private float interest;
       private String bank;

       public Loan(String type, float interest, String bank) {
              this.type = type;
              this.interest = interest;
              this.bank = bank;
       }

       public float getInterest() {
              return interest;
       }

       public void setInterest(float interest) {
              this.interest = interest;
              notifyObservers();
       }

       public String getBank() {
              return this.bank;
       }

       public String getType() {
              return this.type;
       }

       @Override
       public void registerObserver(Observer observer) {
              observers.add(observer);

       }

       @Override
       public void removeObserver(Observer observer) {
              observers.remove(observer);

       }

       @Override
       public void notifyObservers() {
              for (Observer ob : observers) {
                     System.out
                                  .println("Notifying Observers on change in Loan interest rate");
                     ob.update(this.interest);
              }

       }

}

class Newspaper implements Observer {
       @Override
       public void update(float interest) {
              System.out.println("Newspaper: Interest Rate updated, new Rate is: "
                           + interest);
       }
}

class Internet implements Observer {
       @Override
       public void update(float interest) {
              System.out.println("Internet: Interest Rate updated, new Rate is: "
                           + interest);
       }
}

public class ObserverTest {

       public static void main(String args[]) {
              // this will maintain all loans information
              Newspaper printMedia = new Newspaper();
              Internet onlineMedia = new Internet();

              Loan personalLoan = new Loan("Personal Loan", 12.5f,
                           "Standard Charterd");
              personalLoan.registerObserver(printMedia);
              personalLoan.registerObserver(onlineMedia);
              personalLoan.setInterest(3.5f);

       }
}

Decorator Design Pattern

http://javarevisited.blogspot.com/2011/11/decorator-design-pattern-java-example.html

// Component on Decorator design pattern
public abstract class Currency {
 String description = "Unknown currency";

 public String getCurrencyDescription() {
  return description;
 }
 public abstract double cost(double value);
}

// Concrete Component
public class Rupee extends Currency {
double value;
 public Rupee() {
  description = "indian rupees";
 }
 public double cost(double v){
  value=v;
  return value;
 }
}

//Another Concrete Component
public class Dollar extends Currency{
double value;
 public Dollar () {
  description = "Dollar”;
 }
public double cost(double v){
 value=v;
  return value;
 }
}

// Decorator
public abstract class Decorator extends Currency{
 public abstract String getDescription();
}

// Concrete Decorator
public class USDDecorator extends Decorator{
 Currency currency;
 public USDDecorator(Currency currency){
  this.currency = currency;
 }
 public String getDescription(){
  return currency.getDescription()+" ,its US Dollar";
 }
}

//Another Concrete Decorator
public class SGDDecorator extends Decorator{
 Currency currency;
 public SGDDecorator(Currency currency){
  this.currency = currency;
 }
 public String getDescription(){
  return currency.getDescription()+" ,its singapore Dollar";
 }
}

public class CurrencyCheck {
 public static void main(String[] args) {
  // without adding decorators
  Currency curr = new Dollar();
  System.out.println(curr.getDescription() +" dollar. "+curr.cost(2.0));

  //adding decorators
  Currency curr2 = new USDDecorator(new Dollar());
  System.out.println(curr2.getDescription() +" dollar. "+curr2.cost(4.0));
Currency curr3 = new SGDDecorator(new Dollar());
  System.out.println(curr3.getDescription() +" dollar. "+curr3.cost(4.0));
}

A* Search Algorithm

对dijkstra算法的改进,加入了估算距离,所以如果估算距离小于实际距离,能省掉很多节点遍历。A×用于一个点到另一个点,dijstra能求出一个点到所有点。


Reader Writer Problem

你可能感兴趣的:(String Match & Observer & Decorator & A* Search Algorithm & RW Lock)