Search

Abstract Factory DesignPattern-1

Abstract Factory Design Pattern

  • In software development, a factory is the location in the code at which objects are constructed. 
  • The intent in employing the pattern is to insulate the creation of objects from their usage. This allows for new derived types to be introduced with no change to the code that uses the base class.
  • The essence of the Abstract Factory method Pattern is to "Provide an interface for creating families of related or dependent objects without specifying their concrete classes".
  • For Example:
    • Address Factory has no of factories like:
      • US Address Factory which implements the Address Interface.
      • French Address Factory which implements the Address interface.
      • Can have many more country specific factories….
  • When we create the French Address using the method implemented in the French Address Factory class it returns the Object reference to the Abstract class of Address.
  • The Abstract Address class holds the abstract method named get Country() which is implemented separately by each implementing class

JSTL Example

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

pageEncoding="ISO-8859-1"%>


 

<%@ taglib prefix="c" uri="/x" %>


 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>


 

    <c:out value="From JSTL Core Tag"></c:out>

    <br/>

    <c:catch> What is here?? </c:catch>

    <c:set var="counter" value="1"></c:set>

    <c:forEach begin="1" end="5" step="1">

        <c:out value="${counter}"> </c:out>

        <c:set var="counter" value="${counter}}"> </c:set>

    </c:forEach>

</body>

</html>

Steps for using JSTL

1. Download jstl.jar from

http://www.java2s.com/Code/Jar/JKL/Downloadjstljar.htm


 

2. Download standard.jar from

http://www.java2s.com/Code/Jar/JKL/Downloadstandardjar.htm


 

3. Extract and add the tlds in WEB-INF/lib folder by copy paste.


 

4. Edit Web.xml    for all such tlds


 

<jsp-config>

<taglib>

    <taglib-uri> /x </taglib-uri>

    <taglib-location> /WEB-INF/lib/c.tld </taglib-location>

</taglib>

</jsp-config>


 

5. Now in the jsp file add


 

<%@ taglib prefix="c" uri="/x" %>


 

6. Start using tags


 

    e.g. <C:out value="output this"> </c:out>

JSON Sample


 

<html>

<body>

<h2>JSON Object Creation in JavaScript</h2>

<p>

Name: <span id="jname"></span><br />

Age: <span id="jage"></span><br />

Address: <span id="jstreet"></span><br />

Phone: <span id="jphone"></span><br />

</p>


 

<script type="text/javascript">

var JSONObject= {

"name":"John Johnson",

"street":"Oslo West 555",

"age":33,

"phone":"555 1234567"};

document.getElementById("jname").innerHTML=JSONObject.name

document.getElementById("jage").innerHTML=JSONObject.age

document.getElementById("jstreet").innerHTML=JSONObject.street

document.getElementById("jphone").innerHTML=JSONObject.phone

</script>


 

</body>

</html>

Abstract Keyword

  • Abstract Keyword used for method declaration which doesnt have any implementation.
  • Abstract classes have abstract methods not having method implementation in the abstract class but in subclass.
  • When a class doent need to get instiated then that class is used as an abstract class. But this class is available for others to get exextended and use functionality.
  • The best example is the shape class.
  • The shape class having the method called area in its declaration.
  • which is different for all shapes class like it is different for squre, triangle, circle. each class inherits the shape class contains the functionality of that class ut it has to implement all "abstract methods" of the Shape class.
  • Abstract class needs to be extended first to instanciatiate.
  • Implementation

abstract ClassName()

{


 

abstract returntype MethodName1(Arg);

//This method needs to be iimplemented if this class is inherited.


 

ret_type MethodName2();

MethodName2()

{


 

}


 

}


 

  • Example:


 

abstract class Shape{

    abstract void draw();

}

class Rectangle extends Shape{

    void draw(){

            System.out.println("Drawing Rectangle");

         }

}

class Traingle extends Shape{

    void draw(){

System.out.println("Drawing Traingle");

         }

}

class AbstractDemo{

public static void main(String args[]){

Shape s1=new Rectangle();

s1.draw();

s1=new Traingle();

s1.draw();

}

}

Result:

Drawing Rectangle

Drawing Traingle

JQuery Example

JQuery Example:

<html>
<head>
<title>jQuery Hello World</title>
 
<script type="text/javascript" src="jquery-1.4.2.js"></script>
 
</head>
 
<script type="text/javascript">
 
$(document).ready(function(){
 $("#flag").html("Hello World !! (display due to jQuery)");
});
 
</script>
<body>
<font color=red> 
Hello World !! (display due to HTML)
</font>
<font color=blue>
<div id="flag">
</div>
</font>
</body>
</html>