Software Architecture Flow of Control Procedural Programming Flow Business Logic Subroutines Caller's
Software Architecture Flow of Control Procedural Programming Flow Business Logic Subroutines Caller's
of a system is inverted in comparison to procedural programming. In traditional programming the flow of the business logic is controlled by a central piece of code, which calls reusable subroutines that perform specific functions. Using Inversion of Control this "central control" design principle is abandoned. The caller's code deals with the program's execution order, but the business knowledge is encapsulated by the called subroutines. In practice, Inversion of Control is a style of software construction where reusable generic code controls the execution of problem-specific code. It carries the strong connotation that the reusable code and the problem-specific code are developed independently, which often results in a single integrated application. Inversion of Control as a design guideline serves the following purposes: There is a decoupling of the execution of a certain task from implementation. Every system can focus on what it is designed for. The systems make no assumptions about what other systems do or should do. Replacing systems will have no side effect on other systems.
Inversion of Control is implemented using Dependency Injection
Inversion of Control is sometimes facetiously referred to as the "Hollywood Principle: Don't call us, we'll call you", because implementations typically rely on callbacks.
public class ServerFacade { public Object respondToRequest(Object pRequest, IDAO da) { return da.getData(pRequest); } }
Aspect-oriented programming entails breaking down program logic into distinct parts (so-called concerns, cohesive areas of functionality). Nearly all programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting and composing these concerns. But some concerns defy these forms of implementation and are called crosscutting concerns because they "cut across" multiple abstractions in a program. Logging exemplifies a crosscutting concern because a logging strategy necessarily affects every logged part of the system. Logging thereby crosscuts all logged classes and methods.
void transfer(Account fromAcc, Account toAcc, int amount, User user, Logger logger) throws Exception { logger.info("transferring money..."); if (! checkUserPermission(user)){ logger.info("User has no permission."); throw new UnauthorizedUserException(); } if (fromAcc.getBalance() < amount) { logger.info("Insufficient Funds, sorry"); throw new InsufficientFundsException(); } fromAcc.withdraw(amount); toAcc.deposit(amount); //get database connection //save transactions logger.info("Successful transaction."); }
Struts MVC Architecture The model contains the business logic and interact with the persistance storage to store, retrive and manipulate data. The view is responsible for dispalying the results back to the user. In Struts the view layer is implemented using JSP. The controller handles all the request from the user and selects the appropriate view to return. In Sruts the controller's job is done by the ActionServlet.
The following events happen when the Client browser issues an HTTP request. The ActionServlet receives the request. The struts-config.xml file contains the details regarding the Actions, ActionForms, ActionMappings and ActionForwards. During the startup the ActionServelet reads the struts-config.xml file and creates a database of configuration objects. Later while processing the request the ActionServlet makes decision by refering to this object. When the ActionServlet receives the request it does the following tasks. Bundles all the request values into a JavaBean class which extends Struts ActionForm class. Decides which action class to invoke to process the request. Validate the data entered by the user. The action class process the request with the help of the model component. The model interacts with the database and process the request. After completing the request processing the Action class returns an ActionForward to the controller. Based on the ActionForward the controller will invoke the appropriate view. The HTTP response is rendered back to the user by the view component.
Spring MVC Spring MVC helps in building flexible and loosely coupled web applications. The Model-viewcontroller design pattern helps in seperating the business logic, presentation logic and navigation logic. Models are responsible for encapsulating the application data. The Views render response to the user with the help of the model object . Controllers are responsible for receiving the request from the user and calling the back-end services. The figure below shows the flow of request in the Spring MVC Framework.
When a request is sent to the Spring MVC Framework the following sequence of events happen. The DispatcherServlet first receives the request. The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request. The Controller process the request by calling the appropriate service methods and returns a ModeAndView object to the DispatcherServlet. The ModeAndView object contains the model data and the view name. The DispatcherServlet sends the view name to a ViewResolver to find the actual View to invoke. Now the DispatcherServlet will pass the model object to the View to render the result. The View with the help of the model data will render the result back to the user.
Hibernate The hibernate-mapping element is the root element. This document has hbm.xml extension (ie: courses.hbm.xml). The class element is used to map the Java class with the database table. The Java class name is specified using the name attribute of the class element and the database table name is specified using the table attribute of the class element. The meta element is used to create the class description. The id element is used to create the primary key. The name attribute of the id element refers to the property in the Course class and the column attribute refers to the column in the COURSES table. The type attribute holds the hibernate mapping type, this mapping types will convert from Java to SQL data type and vice versa. The generator element within the id element is used to automatically generate the primary key values. When the class attribute of the generator element is set to native, hibernate picks either identity, sequence or hilo algorithm depending upon the capabilities of the underlying database. The property element is used to link a property in the Java class to a column in the database table.
<?xml version="1.0" encoding="UTF-8"?> 02.<!DOCTYPE hibernate-configuration PUBLIC 03."-//Hibernate/Hibernate Configuration DTD 3.0//EN" 04."http://hibernate.sourceforge.net/ hibernate-configuration-3.0.dtd"> 05.<hibernate-configuration> 06. <session-factory> 07. <property name="hibernate.connection.driver_class"> org.hsqldb.jdbcDriver</property> 08. <property name="hibernate.connection.url"> jdbc:hsqldb:hsql://localhost</property> 09. <property name="hibernate.connection.username">sa</property> 10. <property name="connection.password"></property> 11. <property name="connection.pool_size">1</property> 12. <property name="hibernate.dialect"> org.hibernate.dialect.HSQLDialect</property> 13. <property name="show_sql">true</property> 14. <property name="hbm2ddl.auto">create</property> 15. <mapping resource="com/vaannila/course/Course.hbm.xml"/> 16. </session-factory> 17.</hibernate-configuration>
The next step is to create the Hibernate configuration file. On startup, Hibernate looks for a file called hibernate.cfg.xml in the root of the classpath. courses.hbm.xml: defines the object/relational mapping using the XML document. hibernate.cfg.xml: Hibernate configuration file. Defines de database connection Hibernate Console Configuration: defines the configuration file Generate code by selecting the Hibernate Code Generation Configurations option form the toolbar. Create the HibernateUtil class. The HibernateUtil class helps in creating the SessionFactory from the Hibernate configuration file. The SessionFactory is threadsafe, so it is not necessary to obtain one for each thread. Here the static singleton pattern is used to instantiate the SessionFactory.
Singleton Pattern
fill-opacity : 0; } rect{ stroke-width : 6; } path, circle, line{ stroke-width : 2; } text{ fill : #000;
stroke-width : 0; font-weight : bold; text-anchor : middle; font-family : sans-serif; font-size : 25px; } text.visibility{ fill : #000;
Singleton Pattern
public class Singleton { private static final Singleton INSTANCE = new Singleton(); // Private constructor prevents instantiation from other classes private Singleton() { } public static Singleton getInstance() { return INSTANCE; } }