Factory method:
Factory method is a java method return is the same class
object.
Factory method is an idea provided by ‘factory method design
pattern”.
Ex code :-
class A
{
private
A()
{
}
void
m1()
{
System.out.println("m1-A");
}
static
A getRef() //Factory Method
{
A
a=new A();
return
a;
}
}
class Test
{
public
static void main(String[] args)
{
A
a=A.getRef();
a.m1();
}
}
Output:-
m1-A
There are two
types of factory method:
1.
Static factory method
2.
Instance factory method
1.
Static
factory method :
It is a static method return the same class object
Ex:-
Class
c=Class.forName("A");
NumberFormate
nf=NumberFormate.getInstance(--);
DateFormat
df=DateFormat.getDateInstance(--);
ResourcBundle
rb=ResourcBundle.getBundle(--);
2.
Instance factory method :
If any instance method, return the same class object then that instance
method is called as “Instance Factory method”.
Ex:-
Majority of string class method are instance factory methods.
Ex:-
class Test
{
public static void
main(String[] args)
{
String
str="Sai Software Solution";
String
str1=str.concat("Hyderabad");
String
str2=str.trim();
String
str3=str.substring(4,14);
String
str4=str.toUpperCase();
//----
//----
//----
System.out.println(str);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println(str4);
}
}
Output:-
Sai Software Solution
Sai Software SolutionHyderabad
Sai Software Solution
Software S
SAI
SOFTWARE SOLUTION
Note:-
Design pattern is a system, it will provide problem
definition and solution for that problem in order to solve design problem which
are getting in java application.
No comments:
Post a Comment