RubyDubee

  • Archive
  • RSS

Tiny MVC for J2EE

I know, you already know how to write MVC pattern using Servlet/JSP. But I didn’t and I know many of you still don’t know it! People use frameworks even if they don’t want that much complicated setup. For smaller webapps, you don’t need frameworks, they make your app heavy. So mind it and let’s start writing our own MVC pattern using Servlet/JSP. YaY!!

First of all, you need a Controller, yes only one! Let’s call it a FrontController(because some frameworks do). All the requests should come to this controller and it will then delegate these requests to individual Action(again!). Actions take care of processing a request and dispatching it to the JSP, which acts as a view.

Let’s add our FrontController in web.xml

and now lets write our FrontController, Action and a sample AccountAction which will display user’s name through account.jsp.

Here we map a request to its corresponding action, I have done this using requestURI(), you can use different techniques as well, like sending hidden parameter “action”. But this way looks much cooler to me.

AccountAction process your business logic, it will communicate with your Database layer and create a bean of your data(or may be multiple beans). The request will now be dispatched to the jsp file(your View) and along with it goes your data(bean) which is set on the request through setAttribute(..)

Try this! Extending this application is very easy and maintaining it will be even more easier! Happy Coding!

    • #Java
    • #MVC
    • #Design Patterns
  • 1 month ago
  • Comments
  • Permalink
  • Share
    Tweet

Observer and Ducks

If you have implemented Observer pattern in any statically typed language, you know that you need an explicit Observer class/interface for the pattern, which will be extended by the actual concrete classes and some ‘update’ kind of method should be overridden in order to propagate the observations.Basically taking advantage of inheritance.

Here comes a duck type language like Ruby, as there are no types to be declared you can pass in any object to the observable and let it call the update method, you don’t need any parent Observer class. If the method is available in the object it will be called and the observation will be accomplished.

Now we can extend this more, The observable object will call any method it wants and ruby’s method_missing will take care of the methods that the individual observers don’t want to define. Cool eh? Check out this simple example:

So I quote this again “If it walks like a duck and quacks like a duck, it must be a duck.”

    • #ruby
    • #design patterns
  • 6 months ago
  • 1
  • Comments
  • Permalink
  • Share
    Tweet

About

I am Pradyumna and You can read my Rubyish Resume (once it is done) to know more about me!

Pages

  • My Rubyish Resume

Twitter

loading tweets…

I Dig These Posts

See more →
  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr