A brief overview on java frameworks

42
A brief overview on java frameworks and their principles

description

This presentation tries to provide a very brief introduction to some of the popular java frameworks and their design philosophies.

Transcript of A brief overview on java frameworks

  • 1. Object Oriented Development Principles and their uses Standard(?) Design Patterns and their roles Patterns in Java and their uses Overview of Spring Framework Evolution of Java EE 6 All the goodies are now inofficial package A brief introduction to JUnit Test Driven Development inJava

2. The special three Encapsulation hiding the concrete implementations Polymorphism objects has more than one form Inheritance reusing functionalities And some of their derived form/application Program to an interface, not to an implementation Favor object composition over inheritanceand much more, most notably, Design Patterns 3. Single Responsibility Principle Open-Close Principle Liskov Substitution Principle Interface Segregation Principle Dependency Inversion Principle 4. A defining characteristics of a framework Its all about moving away the flow of controls to theframeworks. 5. Let us consider a program that perform some simplecommand line query puts What is your name?name = getsprocess_name(name)puts What is your quest?quest = getsprocess_quest(quest) 6. However, in a window system, I would write somethinglike this require tkroot = TkRoot.new()name_label = TkLabel.new() {text "What is Your Name?"}name_label.packname = TkEntry.new(root).packname.bind("FocusOut") {process_name(name)}quest_label = TkLabel.new() {text "What is Your Quest?"}quest_label.packquest = TkEntry.new(root).packquest.bind("FocusOut") {process_quest(quest)}Tk.mainloop() 7. The control of execution has been handed over to thewindowing system. The control is Inverted, the framework calls the coderather than rather than the code calling the framework. This principle is also known as Hollywood Principle. 8. Let us write a software component that provides a list ofmovies directed by a particular director class MovieLister... public Movie[] moviesDirectedBy(String arg) { List allMovies = finder.findAll(); for (Iterator it = allMovies.iterator(); it.hasNext();) { Movie movie = (Movie) it.next();if (!movie.getDirector().equals(arg)) it.remove(); }return (Movie[]) allMovies.toArray(new Movie[allMovies.size()]);} 9. moviesDirectedBy is dependent on the implementation ofthe finder object. Lets make this method completely independent of howall the movies are being stored. So all the method doesis refer to a finder, and all that finder does is know how torespond to the findAll method. 10. It can be done easily by defining an interface public interface MovieFinder { List findAll();} 11. Lets provide a concrete implementation of MovieFinder -class MovieLister...private MovieFinder finder;public MovieLister() {finder = new ColonDelimitedMovieFinder("movies1.txt");} 12. Now we have a new problem how to get an instance ofthe right finder implementation into place 13. The implementation class for the finder isnt linked intothe program at compile time. Instead we want this listerto work with any implementation, and for thatimplementation to be plugged in at some later point. The problem is how that link could be made so that listerclass is ignorant of the implementation class, but can stilltalk to an instance to do its work. 14. The basic idea of the Dependency Injection is to have aseparate object, an assembler, that populates a field inthe lister class with an appropriate implementation for thefinder interface. 15. Type 1 IoC - Interface Injection Type 2 IoC - Setter Injection Type 3 IoC - Constructor Injection 16. Define a setter method for populating finder class MovieLister... private MovieFinder finder;public void setFinder(MovieFinder finder) { this.finder = finder; } 17. Similarly let us define a setter for the filename -class ColonMovieFinder...public void setFilename(String filename) { this.filename = filename;} 18. The third step is to set up the configuration for the files.Spring supports configuration through XML files andalso through code, but XML is the expected way to doit movies1.txt 19. And then the test public void testWithSpring() throws Exception{ApplicationContext ctx = newFileSystemXmlApplicationContext("spring.xml");MovieLister lister = (MovieLister)ctx.getBean("MovieLister");Movie[] movies = lister.moviesDirectedBy("Sergio Leone");assertEquals("Once Upon a Time in the West", movies[0].getTitle());}WHERE DID THE new GO ?! 20. Spring Google Guice created by Google Pico Container Avalon Context and Dependency Injection official Sun Java DIContainer Seasar 21. Assume you have a graphical class with many set...()methods. After each set method, the data of the graphicschanged, thus the graphics changed and thus thegraphics need to be updated on screen. Assume to repaint the graphics you must callDisplay.update(). 22. The classical approach is to solve this by adding morecode. At the end of each set method you write void set...(...) { : : Display.update();} 23. What will happen if there are 20-30 of these set methods? Also whenever a new set-method is added, developersmust be sure to not forget adding this to the end,otherwise they just created a bug. 24. AOP solves this without adding tons of code, instead youadd an aspect -after() : set() {Display.update();} after running any method that is a set pointcut,run the following code. 25. And you define a point cut pointcut set() : execution(* set*(*) ) &&this(MyGraphicsClass) &&within(com.company.*); If a method is named set* (* means any name mightfollow after set), regardless of what the method returns(first asterisk) or what parameters it takes (third asterisk)and it is a method of MyGraphicsClass and this class ispart of the package com.company.*, then this is a set()pointcut. 26. This example also shows one of the big downsides ofAOP. It is actually doing something that manyprogrammers consider an Anti-Pattern. The exact patternis called Action at a distance. Action at a distance is an anti-pattern (a recognizedcommon error) in which behavior in one part of aprogram varies wildly based on difficult or impossible toidentify operations in another part of the program. 27. AspectJ Spring Seasar 28. Object-relational mapping is a programming techniquefor converting data between incompatible type systems inrelational databases and object-oriented programminglanguages. This creates, in effect, a virtual object database that canbe used from within the programming language. Its good for abstracting the datastore out in order toprovide an interface that can be used in your code. 29. Without ORM, we write code like this String sql = "SELECT ... FROM persons WHERE id = 10"; DbCommand cmd = new DbCommand(connection, sql); Result res = cmd.Execute(); String name = res[0]["FIRST_NAME"]; With the help of ORM tools, we can do Person p = repository.GetPerson(10); String name = p.FirstName;Or - Person p = Person.Get(Person.Properties.Id == 10); 30. The SQL is hidden away from logic code. This has thebenefit of allowing developers to more easily supportmore database engines. Developers can focus on writing the logic, instead ofgetting all the SQL right. The code will typically be morereadable as well. 31. Object-relational mapping is the Vietnam of our industry Ted Neward. Developers are struggling for years with the hugemismatch between relational database models andtraditional object models. 32. Granularity more classes than the number ofcorresponding tables. Subtyping Identity primary key vs. object identity and objectequality Associations unidirectional in OOP vs. foreign keys. Data Navigation walking the object graph vs. SQL joins 33. Hibernate the highly popular ORM tool for Java, has acorresponding .NET version too (NHibernate). UsesHQL. Java Persistence API official sun java specification formanaging persistence. 34. Seam Framework AJAX + JSF + JPA + EJB 3.0 + BPM Log4J logging framework for Java JUnit Test Driven Development in Java Maven actually not a framework, more of a buildsystem, but still 35. Wikipedia Personal Website of Martin Fowler Stackoverflow Official Spring Documentation Coding Horror Official Java EE 6 Tutorial 36. Questions?