Saturday, May 26, 2012

Designing with interfaces

Separation of interface and implementation is central to Java's spirit, and the Java interface construct enables you to achieve this separation in your designs. Two major activities of any software system design are identifying parts (the subsystems within a program or system of programs) and specifying the interfaces between the parts. In designing a Java-based system, you should use Java interfaces to represent abstract interfaces -- the ways in which the parts will interact with each other.
So this is how I ended up thinking about Java's interfaces: as the preferred means of communicating with the parts of your program that represent abstractions that may have several implementations. For example, two parts of a program I describe in my September Design Techniques installment, "The Event Generator Idiom", were TelephoneListener and Telephone. In this design, I decided that the "telephone listener" represented an abstraction that could have multiple implementations, but thatTelephone did not. Thus, I made Telephone a class that didn't implement any interfaces, and defined TelephoneListener as an interface. Telephone, an event source, passed events to (communicated with) listeners through the TelephoneListenerinterface.
I see interfaces as a fundamental tool for achieving flexibility in the design of Java-based systems. Any class can provide an implementation of an interface. As long as you don't change the interface itself, you can make all kind of changes to the implementing classes, or plug in new classes, without impacting code that depends only on the interface. Thus, if you have a subsystem that represents an abstraction that may have multiple implementations, whether the subsystem is a single object, a group of objects, an entire Java applet or application, you should define Java interfaces through which the rest of the world communicates with that subsystem. When you use interfaces in this way, you decouple the parts of your system from each other and generate code that is more flexible: more easily changed, extended, and customized.

Friday, May 25, 2012

Inner Join for three tables?

You can use parenthesis:
Code:
Dim strsql2 = "SELECT employe.*, location.*, job.*
 FROM 
  (employe INNER JOIN location
   ON employe.ID_employe = location.ID_employe)
  INNER JOIN job
   ON employe.ID_employe = job.ID_employe 
 WHERE employe.ID_employe = " & Form3.manipulatedRow & " "
Or you can use the old method, preferred by many programmers, including myself:
Code:
Dim strsql2 = "SELECT employe.*, location.*, job.*
 FROM employe, location, job
 WHERE employe.ID_employe = location.ID_employe
   AND employe.ID_employe = job.ID_employe 
   AND employe.ID_employe = " & Form3.manipulatedRow & " "
http://forums.codeguru.com/showthread.php?t=382050

Wednesday, May 23, 2012

SQL SERVER – Union vs. Union All – Which is better for performance?

This article is completely re-written with better example SQL SERVER – Difference Between Union vs. Union All – Optimal Performance Comparison. I suggest all of my readers to go here for update article.
UNION
The UNION command is used to select related information from two tables, much like the JOIN command. However, when using the UNION command all selected columns need to be of the same data type. With UNION, only distinct values are selected.
UNION ALL
The UNION ALL command is equal to the UNION command, except that UNION ALL selects all values.
The difference between Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all rows from all tables fitting your query specifics and combines them into a table.
A UNION statement effectively does a SELECT DISTINCT on the results set. If you know that all the records returned are unique from your union, use UNION ALL instead, it gives faster results.
Example:
Table 1 : First,Second,Third,Fourth,Fifth
Table 2 : First,Second,Fifth,Sixth
Result Set:
UNION: First,Second,Third,Fourth,Fifth,Sixth (This will remove duplicate values)
UNION ALL: First,First,Second,Second,Third,Fourth,Fifth,Fifth,Sixth,Sixth (This will repeat values)
Reference : Pinal Dave (http://blog.SQLAuthority.com)

What is the difference between Join and Union?


UNION puts lines from queries after each other, while JOIN makes a cartesian product and subsets it -- completely different operations. Trivial example of UNION:
mysql> SELECT 23 AS bah
    -> UNION
    -> SELECT 45 AS bah;
+-----+
| bah |
+-----+
|  23 | 
|  45 | 
+-----+
2 rows in set (0.00 sec)
similary trivial example of JOIN:
mysql> SELECT * FROM 
    -> (SELECT 23 AS bah) AS foo 
    -> JOIN 
    -> (SELECT 45 AS bah) AS bar
    -> ON (33=33);
+-----+-----+
| bah | bah |
+-----+-----+
|  23 |  45 | 
+-----+-----+
1 row in set (0.01 sec)
http://stackoverflow.com/questions/905379/what-is-the-difference-between-join-and-union 

REST and SOAP

Both methods are used by many of the large players. It's a matter of preference. My preference is REST because it's simpler to use and understand.
SOAP:
  • SOAP builds an XML protocol on top of HTTP or sometimes TCP/IP.
  • SOAP describes functions, and types of data.
  • SOAP is a successor of XML-RPC and is very similar, but describes a standard way to communicate.
  • Several programming languages have native support for SOAP, you typically feed it a web service URL and you can call its web service functions without the need of specific code.
  • Binary data that is sent must be encoded first into a format such as base64 encoded.
  • Has several protocols and technologies relating to it: WSDL, XSDs, SOAP, WS-Addressing
Representational state transfer (REST):
  • REST need not be over HTTP but most of my points below will have an HTTP bias.
  • REST is very lightweight, it says wait a minute, we don't need all of this complexity that SOAP created.
  • Typically uses normal HTTP methods instead of a big XML format describing everything. For example to obtain a resource you use HTTP GET, to put a resource on the server you use HTTP PUT. To delete a resource on the server you use HTTP DELETE.
  • REST is a very simple in that it uses HTTP GET, POST and PUT methods to update resources on the server.
  • REST typically is best used with Resource Oriented Architecture (ROA). In this mode of thinking everything is a resource, and you would operate on these resources.
  • As long as your programming language has an HTTP library, and most do, you can consume a REST HTTP protocol very easily.
  • Binary data or binary resources can simply be delivered upon their request.
There are endless debates on REST vs SOAP on google.
My favorite is this one.

http://stackoverflow.com/questions/209905/rest-and-soap

Why do so few people use Maven? Are there alternative tools?

I like Maven and use it almost exclusively in my company. But I'd justify my reasons for doing so:
  1. I run a team of developers and we need structure. I need most of my developers to follow a certain set of conventions down to project layouts. This is to make it easier for handovers when attrition occurs.
  2. Maven archetypes are a major blessing in this regard. The seniors would just create specific archetypes for certain project templates which all developers just base their projects on. There's a really simple generic one for those cases where we didn't manage to cater for. Checking out an ant-based layout from SVN is less intuitive in this regard as the developer will still need to remove the original .svn metadata once it's checked out.
  3. Maven helps us remain IDE-agnostic. I don't want to base any of my hiring policies on what IDE the developer prefers to use. At least 2 major IDEs (Eclipse and NetBeans) have pretty good integration to Maven already such that a pom.xml file already is the definition of an IDE project. I can tell any new developer that he can use whatever IDE he prefers as long as the build system itself is based on Maven.
  4. I found that the development bootstrap process for picking up things midway was drastically cut down when I switched to Maven. New developers, even those unfamiliar with the project at hand, was able to at least compile, package and deploy within half hour of briefing them of the project. Support staff (who are typically not as technically adept) were able to concentrate on troubleshooting and not fret about building the project, which is an unnecessary irritation.
All in all, Maven suits my needs perfectly. I do agree to a certain extent that an independent developer who has his own workflow and project management practices may not derive much benefit from it, but none of us over here work in a void by ourselves.

http://stackoverflow.com/questions/1077477/why-do-so-few-people-use-maven-are-there-alternative-tools

Tuesday, May 22, 2012

Difference between final, finally and finalize()

  • final ? constant declaration.
  • finally ? The finally block always executes when the try block exits, except System.exit(0) call. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling ? it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.
  • finalize() ? method helps in garbage collection. A method that is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.