Monday, October 12, 2015

Improve the knowledge about Abstract Factory Design Patterns

Abstract Factory


Definition
Provide an interface for creating families of related or dependent objects without specifying their concrete classes.







UML class diagram









Participants


    The classes and objects participating in this pattern are:
  • AbstractFactory  (ContinentFactory)
    • declares an interface for operations that create abstract products
  • ConcreteFactory   (AfricaFactory, AmericaFactory)
    • implements the operations to create concrete product objects
  • AbstractProduct   (Herbivore, Carnivore)
    • declares an interface for a type of product object
  • Product  (Wildebeest, Lion, Bison, Wolf)
    • defines a product object to be created by the corresponding concrete factory
    • implements the AbstractProduct interface
  • Client  (AnimalWorld)
    • uses interfaces declared by AbstractFactory and AbstractProduct classes






Structural code in C#


This structural code demonstrates the Abstract Factory pattern creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code.
1.                   
2.          
3.   
4.  using System;
5.   
6.  namespace DoFactory.GangOfFour.Abstract.Structural
7.  {
8.    /// <summary>
9.    /// MainApp startup class for Structural
10.   /// Abstract Factory Design Pattern.
11.   /// </summary>
12.   class MainApp
13.   {
14.     /// <summary>
15.     /// Entry point into console application.
16.     /// </summary>
17.     public static void Main()
18.     {
19.       // Abstract factory #1
20.       AbstractFactory factory1 = new ConcreteFactory1();
21.       Client client1 = new Client(factory1);
22.       client1.Run();
23.  
24.       // Abstract factory #2
25.       AbstractFactory factory2 = new ConcreteFactory2();
26.       Client client2 = new Client(factory2);
27.       client2.Run();
28.  
29.       // Wait for user input
30.       Console.ReadKey();
31.     }
32.   }
33.  
34.   /// <summary>
35.   /// The 'AbstractFactory' abstract class
36.   /// </summary>
37.   abstract class AbstractFactory
38.   {
39.     public abstract AbstractProductA CreateProductA();
40.     public abstract AbstractProductB CreateProductB();
41.   }
42.  
43.  
44.   /// <summary>
45.   /// The 'ConcreteFactory1' class
46.   /// </summary>
47.   class ConcreteFactory1 : AbstractFactory
48.   {
49.     public override AbstractProductA CreateProductA()
50.     {
51.       return new ProductA1();
52.     }
53.     public override AbstractProductB CreateProductB()
54.     {
55.       return new ProductB1();
56.     }
57.   }
58.  
59.   /// <summary>
60.   /// The 'ConcreteFactory2' class
61.   /// </summary>
62.   class ConcreteFactory2 : AbstractFactory
63.   {
64.     public override AbstractProductA CreateProductA()
65.     {
66.       return new ProductA2();
67.     }
68.     public override AbstractProductB CreateProductB()
69.     {
70.       return new ProductB2();
71.     }
72.   }
73.  
74.   /// <summary>
75.   /// The 'AbstractProductA' abstract class
76.   /// </summary>
77.   abstract class AbstractProductA
78.   {
79.   }
80.  
81.   /// <summary>
82.   /// The 'AbstractProductB' abstract class
83.   /// </summary>
84.   abstract class AbstractProductB
85.   {
86.     public abstract void Interact(AbstractProductA a);
87.   }
88.  
89.  
90.   /// <summary>
91.   /// The 'ProductA1' class
92.   /// </summary>
93.   class ProductA1 : AbstractProductA
94.   {
95.   }
96.  
97.   /// <summary>
98.   /// The 'ProductB1' class
99.   /// </summary>
100.              class ProductB1 : AbstractProductB
101.              {
102.                public override void Interact(AbstractProductA a)
103.                {
104.                  Console.WriteLine(this.GetType().Name +
105.                    " interacts with " + a.GetType().Name);
106.                }
107.              }
108.             
109.              /// <summary>
110.              /// The 'ProductA2' class
111.              /// </summary>
112.              class ProductA2 : AbstractProductA
113.              {
114.              }
115.             
116.              /// <summary>
117.              /// The 'ProductB2' class
118.              /// </summary>
119.              class ProductB2 : AbstractProductB
120.              {
121.                public override void Interact(AbstractProductA a)
122.                {
123.                  Console.WriteLine(this.GetType().Name +
124.                    " interacts with " + a.GetType().Name);
125.                }
126.              }
127.             
128.              /// <summary>
129.              /// The 'Client' class. Interaction environment for the products.
130.              /// </summary>
131.              class Client
132.              {
133.                private AbstractProductA _abstractProductA;
134.                private AbstractProductB _abstractProductB;
135.             
136.                // Constructor
137.                public Client(AbstractFactory factory)
138.                {
139.                  _abstractProductB = factory.CreateProductB();
140.                  _abstractProductA = factory.CreateProductA();
141.                }
142.             
143.                public void Run()
144.                {
145.                  _abstractProductB.Interact(_abstractProductA);
146.                }
147.              }
148.            }
149.             
150.             
151.                      
152.                        


Output
ProductB1 interacts with ProductA1
ProductB2 interacts with ProductA2







Real-world code in C#


This real-world code demonstrates the creation of different animal worlds for a computer game using different factories. Although the animals created by the Continent factories are different, the interactions among the animals remain the same.
1.                   
2.   
3.  using System;
4.   
5.  namespace DoFactory.GangOfFour.Abstract.RealWorld
6.  {
7.    /// <summary>
8.    /// MainApp startup class for Real-World
9.    /// Abstract Factory Design Pattern.
10.   /// </summary>
11.   class MainApp
12.   {
13.     /// <summary>
14.     /// Entry point into console application.
15.     /// </summary>
16.     public static void Main()
17.     {
18.       // Create and run the African animal world
19.       ContinentFactory africa = new AfricaFactory();
20.       AnimalWorld world = new AnimalWorld(africa);
21.       world.RunFoodChain();
22.  
23.       // Create and run the American animal world
24.       ContinentFactory america = new AmericaFactory();
25.       world = new AnimalWorld(america);
26.       world.RunFoodChain();
27.  
28.       // Wait for user input
29.       Console.ReadKey();
30.     }
31.   }
32.  
33.  
34.   /// <summary>
35.   /// The 'AbstractFactory' abstract class
36.   /// </summary>
37.   abstract class ContinentFactory
38.   {
39.     public abstract Herbivore CreateHerbivore();
40.     public abstract Carnivore CreateCarnivore();
41.   }
42.  
43.   /// <summary>
44.   /// The 'ConcreteFactory1' class
45.   /// </summary>
46.   class AfricaFactory : ContinentFactory
47.   {
48.     public override Herbivore CreateHerbivore()
49.     {
50.       return new Wildebeest();
51.     }
52.     public override Carnivore CreateCarnivore()
53.     {
54.       return new Lion();
55.     }
56.   }
57.  
58.   /// <summary>
59.   /// The 'ConcreteFactory2' class
60.   /// </summary>
61.   class AmericaFactory : ContinentFactory
62.   {
63.     public override Herbivore CreateHerbivore()
64.     {
65.       return new Bison();
66.     }
67.     public override Carnivore CreateCarnivore()
68.     {
69.       return new Wolf();
70.     }
71.   }
72.  
73.   /// <summary>
74.   /// The 'AbstractProductA' abstract class
75.   /// </summary>
76.   abstract class Herbivore
77.   {
78.   }
79.  
80.   /// <summary>
81.   /// The 'AbstractProductB' abstract class
82.   /// </summary>
83.   abstract class Carnivore
84.   {
85.     public abstract void Eat(Herbivore h);
86.   }
87.  
88.   /// <summary>
89.   /// The 'ProductA1' class
90.   /// </summary>
91.   class Wildebeest : Herbivore
92.   {
93.   }
94.  
95.   /// <summary>
96.   /// The 'ProductB1' class
97.   /// </summary>
98.   class Lion : Carnivore
99.   {
100.                public override void Eat(Herbivore h)
101.                {
102.                  // Eat Wildebeest
103.                  Console.WriteLine(this.GetType().Name +
104.                    " eats " + h.GetType().Name);
105.                }
106.              }
107.             
108.              /// <summary>
109.              /// The 'ProductA2' class
110.              /// </summary>
111.              class Bison : Herbivore
112.              {
113.              }
114.             
115.              /// <summary>
116.              /// The 'ProductB2' class
117.              /// </summary>
118.              class Wolf : Carnivore
119.              {
120.                public override void Eat(Herbivore h)
121.                {
122.                  // Eat Bison
123.                  Console.WriteLine(this.GetType().Name +
124.                    " eats " + h.GetType().Name);
125.                }
126.              }
127.             
128.              /// <summary>
129.              /// The 'Client' class
130.              /// </summary>
131.              class AnimalWorld
132.              {
133.                private Herbivore _herbivore;
134.                private Carnivore _carnivore;
135.             
136.                // Constructor
137.                public AnimalWorld(ContinentFactory factory)
138.                {
139.                  _carnivore = factory.CreateCarnivore();
140.                  _herbivore = factory.CreateHerbivore();
141.                }
142.             
143.                public void RunFoodChain()
144.                {
145.                  _carnivore.Eat(_herbivore);
146.                }
147.              }
148.            }
149.             
150.                    
151.                        


Output
Lion eats Wildebeest
Wolf eats Bison


Improve knowledge about Java - how to remove a particular character from a string

Following example shows hoe to remove a character from a particular position from a string with the help of removeCharAt(string,position) method.
public class Main {
   public static void main(String args[]) {
      String str = "this is Java";
      System.out.println(removeCharAt(str, 3));
   }
   public static String removeCharAt(String s, int pos) {
      return s.substring(0, pos) + s.substring(pos + 1);
   }
}

Result:

The above code sample will produce the following result.
thi is Java

Improve knowledge about Java - how to compare two strings

Following example compares two strings by using str compareTo (string) , str compareToIgnoreCase(String) and str compareTo(object string) of string class and returns the ascii difference of first odd characters of compared strings .
public class StringCompareEmp{
   public static void main(String args[]){
      String str = "Hello World";
      String anotherString = "hello world";
      Object objStr = str;

      System.out.println( str.compareTo(anotherString) );
      System.out.println( str.compareToIgnoreCase(anotherString) );
      System.out.println( str.compareTo(objStr.toString()));
   }
}

Result:

The above code sample will produce the following result.
-32
0
0

Monday, October 5, 2015

Created edit servlet for edit values in database and add values in to database








Improve knowledge about Java - Documentation Comments

The Java language supports three types of comments:
CommentDescription
/* text */The compiler ignores everything from /* to */.
// textThe compiler ignores everything from // to the end of the line.
/** documentation */This is a documentation comment and in general its called doc comment. The JDK javadoc tool uses doc comments when preparing automatically generated documentation.
This tutorial is all about explaining Javadoc. We will see how we can make use of Javadoc for generating useful documentation for our Java code.

What is Javadoc?

Javadoc is a tool which comes with JDK and it is used for generating Java code documentation in HTML format from Java source code which has required documentation in a predefined format.
Following is a simple example where red part of the code represents Java comments:
/**
* The HelloWorld program implements an application that
* simply displays "Hello World!" to the standard output.
*
* @author  Zara Ali
* @version 1.0
* @since   2014-03-31 
*/
public class HelloWorld {
    public static void main(String[] args) {
        /* Prints Hello, World! on standard output.
        System.out.println("Hello World!");
    }
}
You can include required HTML tags inside the description part, For example, below example makes use of <h1>....</h1> for heading and <p> has been used for creating paragraph break:
/**
* <h1>Hello, World!</h1>
* The HelloWorld program implements an application that
* simply displays "Hello World!" to the standard output.
* <p>
* Giving proper comments in your program makes it more
* user friendly and it is assumed as a high quality code.
* 
*
* @author  Zara Ali
* @version 1.0
* @since   2014-03-31 
*/
public class HelloWorld {
    public static void main(String[] args) {
        /* Prints Hello, World! on standard output.
        System.out.println("Hello World!");
    }
}

The javadoc Tags:

The javadoc tool recognizes the following tags:
TagDescriptionSyntax
@authorAdds the author of a class.@author name-text
{@code}Displays text in code font without interpreting the text as HTML markup or nested javadoc tags.{@code text}
{@docRoot}Represents the relative path to the generated document's root directory from any generated page{@docRoot}
@deprecatedAdds a comment indicating that this API should no longer be used.@deprecated deprecated-text
@exceptionAdds a Throws subheading to the generated documentation, with the class-name and description text.@exception class-name description
{@inheritDoc}Inherits a comment from the nearestinheritable class or implementable interfaceInherits a comment from the immediate surperclass.
{@link}Inserts an in-line link with visible text label that points to the documentation for the specified package, class or member name of a referenced class. T{@link package.class#member label}
{@linkplain}Identical to {@link}, except the link's label is displayed in plain text than code font.{@linkplain package.class#member label}
@paramAdds a parameter with the specified parameter-name followed by the specified description to the "Parameters" section.@param parameter-name description
@returnAdds a "Returns" section with the description text.@return description
@seeAdds a "See Also" heading with a link or text entry that points to reference.@see reference
@serialUsed in the doc comment for a default serializable field.@serial field-description | include | exclude
@serialDataDocuments the data written by the writeObject( ) or writeExternal( ) methods@serialData data-description
@serialFieldDocuments an ObjectStreamField component.@serialField field-name field-type field-description
@sinceAdds a "Since" heading with the specified since-text to the generated documentation.@since release
@throwsThe @throws and @exception tags are synonyms.@throws class-name description
{@value}When {@value} is used in the doc comment of a static field, it displays the value of that constant:{@value package.class#field}
@versionAdds a "Version" subheading with the specified version-text to the generated docs when the -version option is used.@version version-text

Example:

Following program uses few of the important tags available for documentation comments. You can make use of other tags based on your requirements.
The documentation about the AddNum class will be produced in HTML file AddNum.html but same time a master file with a name index.html will also be created.
import java.io.*;

/**
* <h1>Add Two Numbers!</h1>
* The AddNum program implements an application that
* simply adds two given integer numbers and Prints
* the output on the screen.
* <p>
* <b>Note:</b> Giving proper comments in your program makes it more
* user friendly and it is assumed as a high quality code.
*
* @author  Zara Ali
* @version 1.0
* @since   2014-03-31
*/
public class AddNum {
   /**
   * This method is used to add two integers. This is
   * a the simplest form of a class method, just to
   * show the usage of various javadoc Tags.
   * @param numA This is the first paramter to addNum method
   * @param numB  This is the second parameter to addNum method
   * @return int This returns sum of numA and numB.
   */
   public int addNum(int numA, int numB) {
      return numA + numB;
   }

   /**
   * This is the main method which makes use of addNum method.
   * @param args Unused.
   * @return Nothing.
   * @exception IOException On input error.
   * @see IOException
   */
   public static void main(String args[]) throws IOException
   {

      AddNum obj = new AddNum();
      int sum = obj.addNum(10, 20);

      System.out.println("Sum of 10 and 20 is :" + sum);
   }
}
Now, process above AddNum.java file using javadoc utility as follows:
$ javadoc AddNum.java
Loading source file AddNum.java...
Constructing Javadoc information...
Standard Doclet version 1.7.0_51
Building tree for all the packages and classes...
Generating /AddNum.html...
AddNum.java:36: warning - @return tag cannot be used in method with void return type.
Generating /package-frame.html...
Generating /package-summary.html...
Generating /package-tree.html...
Generating /constant-values.html...
Building index for all the packages and classes...
Generating /overview-tree.html...
Generating /index-all.html...
Generating /deprecated-list.html...
Building index for all classes...
Generating /allclasses-frame.html...
Generating /allclasses-noframe.html...
Generating /index.html...
Generating /help-doc.html...
1 warning
$
You can check all the generated documentation here: AddNum. If you are using JDK 1.7 then javadoc does not generate a great stylesheet.css, so I suggest to download and use standard stylesheet fromhttp://docs.oracle.com/javase/7/docs/api/stylesheet.css