Saturday, May 12, 2012

Using Command Design pattern

public interface Command {
   public void execute();
}
For the most part, commands are immutable and contain instructions that encapsulate a single action that is executed on demand. You might also have a RuntimeCommand that accepts instructions upon execution, but this delves more into the Strategy or Decorator Patterns depending on the implementations.
In my own opinion, I think it's very important to heed the immutable context of a command otherwise the command becomes a suggestion. For instance:
public final class StopServerCommand implements Command {
    private final Server server;

    public StopServerCommand(Server server) { this.server = server; }

    public void execute() {
        if(server.isRunning()) server.stop();
    }
}
public class Application {
    //...
    public void someMethod() {
        stopButton.addActionListener(new ActionListener() {
            public void actionPerformed(Event e) {
                 stopCommand.execute();
            }
        });
    }
}
I personally don't really like commands. In my own experience, they only work well for framework callbacks.
If it helps, think of a command in a metaphorical sense; a trained soldier is given a command by his/her commanding officer, and on demand the soldier executes this command.
http://stackoverflow.com/questions/2015549/using-command-design-pattern

Factory Pattern. When to use factory methods?


I like thinking about design pattens in terms of my classes being 'people,' and the patterns are the ways that the people talk to each other.
So, to me the factory pattern is like a hiring agency. You've got someone that will need a variable number of workers. This person may know some info they need in the people they hire, but that's it.
So, when they need a new employee, they call the hiring agency and tell them what they need. Now, to actually hire someone, you need to know a lot of stuff - benefits, eligibility verification, etc. But the person hiring doesn't need to know any of this - the hiring agency handles all of that.
In the same way, using a Factory allows the consumer to create new objects without having to know the details of how they're created, or what their dependencies are - they only have to give the information they actually want.
public interface IThingFactory
{
    Thing GetThing(string theString);
}

public class ThingFactory : IThingFactory
{
    public Thing GetThing(string theString)
    {
        return new Thing(theString, firstDependency, secondDependency);
    }
}
So, now the consumer of the ThingFactory can get a Thing, without having to know about the dependencies of the Thing, except for the string data that comes from the consumer.
http://stackoverflow.com/questions/69849/factory-pattern-when-to-use-factory-methods

Friday, May 11, 2012

What does the Spring framework do? Should I use it? Why or why not?

What does the Spring framework do? Should I use it? Why or why not?
Spring is a framework that helps you to "wire" different components together. It is most useful in cases where you have a lot of components and you might decide to combine them in different ways, or wish to make it easy to swap out one component for another depending on different settings or environments.
This is what I've always understood "dependency injection" to be.
I would suggest a different definition:
"Dependency injection is where you design your classes so that they expect an outside actor to directly give them the sub-tools they need to do their jobs before anybody starts asking them to do something."
Most of the XML (or annotation-based) stuff is telling spring:
  • When someone asks for a "HammerStore", I want you to create a example.HammerStore and return it result. Cache the result, since there is only one store.
  • When someone asks for a "SomeHammer", I want you to find a "HammerStore" and return the result of its getHammer() method. Do not cache the result, but make a new hammer each time.
  • When someone asks for a "SomeWrench", I want you to create a example.WrenchImpl and take the configuration setting guage and put it into the setWrenchSize() property. Do not cache the result, but create a new one each time.
  • When someone asks for a "OurPlumber", I want to you create an instance of example.PlumberImpl. Put the string "Pedro" into its setName() method, put a "SomeHammer" into its setHammer() method, and put a "SomeHammer" into its setWrench() method. Return the result, but do cache it, since we only have one Pedro.
In this way, Spring lets your connect components, label them, control their lifecycle/caching, and alter behavior based on configuration.
To facilitate [testing] I typically make (at least) two versions of a method : one that uses instance variables, and one that only uses variables that are passed in to the method.
That sounds like a lot of overhead for not a lot of benefit for me. I would suggest that you make your instance variables protected or package-level, and then locate the unit tests inside the same package. That way you can alter or test the instance variables in-place.

http://programmers.stackexchange.com/questions/92393/what-does-the-spring-framework-do-should-i-use-it-why-or-why-not

Why should you use an ORM?

Making data access more abstract and portable. ORM implementation classes know how to write vendor-specific SQL, so you don't have to.

http://stackoverflow.com/questions/448684/why-should-you-use-an-orm

Why do we use hibernate annotation?

It is not something important as in "mandatory". It's a different possibility, with strength and weaknesses.
Advantages:
  • Compile-time checking : writing in Java (instead of Xml) is very user-friendly in the IDE nowadays. No more typos discovered when starting your application (incremental compilation), not that much to remember (completion)...
  • Localized with the code (class level) : instead of having to open two files (java and xml) to get the full story, with one annotated java file, you open only one file. This is less repetitive, faster in the long run.
  • Localized with the code (method or field level) : because the annotation go on a method (or field), there is no need to specify the method it belongs to. That redondant information is not given, which is shorter, and always coherent (even after a code refactoring for example). Maintenance is so much faster.
  • Tools (javadoc, other tools using reflection) can use the annotations for some other requirements.
  • Annotations are newer than the xml, the team used the input they had received at the time to provide better default values. Xml has some, but can't change much for compatibility reasons. Often, with the annotations technology, you write no annotation at all, and it works. Imagine the time-saving, especially during development.
http://stackoverflow.com/questions/1367187/why-do-we-use-hibernate-annotation

Thursday, May 10, 2012

Authentication and Authorization

Authentication
An authentication system is how you identify yourself to the computer. The goal behind an authentication system is to verify that the user is actually who they say they are.
There are many ways of authenticating a user. Any combination of the following are good examples.

Password based authentication
Requires the user to know some predetermined quantity (their password).


  • Advantages: Easy to impliemnt, requires no special equipemnt.
  • Disadvantages: Easy to forget password. User can tell another user their password. Password can be written down. Password can be reused.
  • Device based authentication
    Requires the user to posses some item such as a key, mag strip, card, s/key device, etc.


  • Advantages: Difficult to copy. Cannot forget password. If used with a PIN is near useless if stolen.
  • Disadvantages: Must have device to use service so the user might forget it at home. Easy target for theft. Still doesn't actually actively identify the user.

  • Biometric Authentication


  • My voice is my passport. Verify me. This is from the movie sneakers and demonstrates one type of biometric authentication device. It identifies some physical charactistic of the user that cannot be seperated from their body.
    Retina Scanners:


  • Advantages: Accurately identifies the user when it works.
  • Disadvantages: New technology that is still evolving. Not perfect yet.

  • Hand Scanners:


  • Advantages: Difficult to seperate from the user. Accurately identifies the user.
  • Disadvantages: Getting your hand stolen to break into a vault sucks a lot more than getting your ID card stolen.
  • Authorization
    Once the system knows who the user is through authentication, authorization is how the system decides what the user can do.
    A good example of this is using group permissions or the difference between a normal user and the superuser on a unix system.
    There are other more compicated ACL (Access Control Lists) available to decide what a user can do and how they can do it. Most unix systems don't impliment this very well (if at all.)

    http://www.acm.uiuc.edu/workshops/security/auth.html

    Error: no 'server' JVM at '...'

    Some workstation or server will not come with server JVM after you install JDK on it. When you run server JVM by 'java -server MyApp', you will get the error such as:
    Error: no `server' JVM at `C:\Program Files\Java\jre1.6.0\bin\server\jvm.dll'.
    There is a little trick to get the server JVM up and running:
    1. Copy 'server' folder from the JDK's JRE's bin folder (example: C:\Program Files\Java\jdk1.6.0\jre\bin\server)
    2. Paste the 'server' folder to JRE's bin folder(example: C:\Program Files\Java\jre1.6.0\bin)
    3. Done
    http://thiamteck.blogspot.com/2007/10/error-no-server-jvm-at.html