Tuesday, March 22, 2016

Design patterns example in JDK and work experience

Creational:
1. Abstract Factory
Swing pluggable look-and-feel classes
2. Builder
StringBuilder.append
3. Factory Method
Calendar.getInstance()
ResourceBundle.getBundle()
4. Prototype
Object.clone()
5. Singleton
Runtime.getRuntime()
Desktop.getDesktop()

Structural:
1. Adapter
Arrays.asList()
InputStreamReader
2. Bridge
3. Composite
nested containers in AWT/Swing
4. Decorator
BufferedInputStream (FileInputStream)
5. Facade

6. Flyweight
Integer.valueOf (-128 -127)
connection pool
7. Proxy
java.lang.reflect.Proxy
java.rmi.*

Behavioral
1. Chain of Resonsibility
Logger.log
Servlet filters
2. Command
Runnable
3. Interpreter
JavaCC
4. Iterator
All implementations of Iterator
5. Mediator
Executor.execute
Method.invoke
6. Memento

7. Observer
Observable
EventListener
8. State
9. Strategy
Comparator.compare()
Layout Managers in AWT/Swing
10.Template Method
non-abstract methods of AbstractList
11. Visitor
SimpleFileVisitor

Tuesday, March 15, 2016

JQuery

1. ID selector and class selector in jQuery:

$("#LoginTextBox") -- Returns element wrapped as jQuery object with id="LoginTextBox" $(".active") -- Returns all elements with CSS class active.
2. How do you find all selected options of HTML select tag?
You can use following jQuery selector to retrieve all selected options of <select> tag with multiple=true :
$('[name=NameOfSelectedTag] :selected')

$('[name=NameOfSelectedTag] :selected').each(function(selected){
        alert($(selected).text());
});

3.
What is difference between detach() and remove() method in jQuery? (answer)
Though both detach() and remove() method is used to remove a DOM element,Main difference between them is that detach() keep track of the last element detached, so that it can be reattached, while remove() method does keep reference of last removed method. This is one of the many jQuery interview question from DOM manipulation. You can also take a look on appendTo() for adding element into DOM.
4.
How do you add and remove CSS classes to an element using jQuery? (answer)
By using addClass() and removeClass() jQuery methods. 

5.
What is difference between jQuery.get() and jQuery.ajax() method?
ajax() method is more powerful and configurable, allows you to specify how long to wait and how to handle error, get() is a specialization to over ajax just to retrieve some data.

Thursday, March 10, 2016

Java BigDecimal(String) and BigDecimal(double) 区别

将 double 转换为 BigDecimal,后者是 double 的二进制浮点值准确的十进制表示形式。返回的 BigDecimal 的标度是使 (10scale × val)为整数的最小值。
注:
  1. 此构造方法的结果有一定的不可预知性。有人可能认为在 Java 中写入 new BigDecimal(0.1) 所创建的 BigDecimal 正好等于 0.1(非标度值 1,其标度为 1),但是它实际上等于 0.1000000000000000055511151231257827021181583404541015625。这是因为 0.1 无法准确地表示为 double(或者说对于该情况,不能表示为任何有限长度的二进制小数)。这样,传入到构造方法的值不会正好等于 0.1(虽然表面上等于该值)。
  2. 另一方面,String 构造方法是完全可预知的:写入 new BigDecimal("0.1") 将创建一个 BigDecimal,它正好 等于预期的 0.1。因此,比较而言,通常建议优先使用 String 构造方法
  3. 当 double 必须用作 BigDecimal 的源时,请注意,此构造方法提供了一个准确转换;它不提供与以下操作相同的结果:先使用 Double.toString(double) 方法,然后使用 BigDecimal(String) 构造方法,将 double 转换为 String。要获取该结果,请使用 static valueOf(double) 方法。