controller must have the same name as component

Advertisements

hash vs encryption

hashing is one way (cannot be reverted), encryption is two-way (can be decrypted)

Hashing is like a meat grinder. You can turn a cow to hamburger, but not the reverse.

 

difference padding/margin

To me the biggest difference between padding and margin is that margins auto-collapse, and padding doesn’t. Consider two elements next to each other each with padding of 1em. This padding is considered to be part of the element, and is always preserved. So you will end up with the content of the first element, followed by the padding of the first element, followed by the padding of the second, followed by the content of the second element. Thus content of the two elements will end up being 2em apart.

Now replace that padding with 1em margin. Margins are considered to be outside of the element, and margins of adjacent items will overlap. So in this example you will end up with the content of the first element followed by 1em of combined margin followed by the content of the second element. So the content of the two elements is only 1em apart.

This can be really useful when you know that you want say 1em of spacing around an element, regardless of what element it is next to.

The other two big differences is that padding is included in the click region and background color/image, but not the margin.

By default I use margin everywhere, except when I have a border or background and want to increase the space inside that visible box.

http://stackoverflow.com/questions/2189452/when-to-use-margin-vs-padding-in-css

封装 if else

public interface Handler {
boolean isSatisfied(Input input);
void handle();
}

public class Condition1Handler implements Handler {
public boolean isSatisfied(Input input) {
return (input.isStatus1 && input.isStatus2) || input.isStatus3;
}

public void handle() {

}
}

caller:

for (Handler h : registeredHandlers) {
if (h.isSatisfied(input)) {
h.handle()
}
}

generics

http://docs.oracle.com/javase/tutorial/java/generics/types.html

bounded type parameters

 public <U extends Number> void inspect(U u){
        System.out.println("T: " + t.getClass().getName());
        System.out.println("U: " + u.getClass().getName());
    }

    public static void main(String[] args) {
        Box<Integer> integerBox = new Box<Integer>();
        integerBox.set(new Integer(10));
        integerBox.inspect("some text"); // error: this is still String!
    }

generic methods

public class Util {
    // Generic static method
    public static <K, V> boolean compare(Pair<K, V> p1, Pair<K, V> p2) {
        return p1.getKey().equals(p2.getKey()) &&
               p1.getValue().equals(p2.getValue());
    }
}
boolean same = Util.<Integer, String>compare(p1, p2);

generic type example?

Box<Integer> integerBox;
public class Box<T> {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
}

The Diamond

In Java SE 7 and later, you can replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box<Integer> with the following statement:

Box<Integer> integerBox = new Box<>();

what?

  • Type as parameters

why use?

  • Stronger type checks at compile time.
  • Enabling programmers to implement generic algorithms.
  • Elimination of casts.The following code snippet without generics requires casting:
    List list = new ArrayList();
    list.add("hello");
    String s = (String) list.get(0);

    When re-written to use generics, the code does not require casting:

    List<String> list = new ArrayList<String>();
    list.add("hello");
    String s = list.get(0);   // no cast