0% found this document useful (0 votes)
8 views

DesignPattern Assignment 1

Uploaded by

Nainesh Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DesignPattern Assignment 1

Uploaded by

Nainesh Shah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

SHAH NAINESH M.

DesignPattern Assignment-1

Factory Design Pattern

Concept:
● The factory method is a creational design pattern, i.e., related to object creation. The
Factory Method pattern is used to create objects without specifying the exact class of object
that will be created. This pattern is useful when you need to decouple the creation of an
object from its implementation.

● The idea is to create a factory class with a single responsibility to create objects, hiding the
details of class modules from the user.

● So,In Example 1 OS is Generic class and Android,Ios,Windows are it’s subclasses,there is


one osFactory which is responsible for creating the objects and returning the objects also,by
creating osFactory class object at through access its method and passing the string type
parameter which is containing the os name info,which string parameter is correcting it’s
objects are written and output will be displayed on the screen.

● So,In Example 2 CAR is Generic class and Suv_car,Sports_car,Convitable_car are it’s


subclasses,there is one carFactory which is responsible for creating the objects and
returning the objects also,by creating carFactory class object through access it’s method
and passing the string type parameter which is containing the car type info,which string
parameter is correcting it’s objects are written and output will be displayed on the screen.

1
SHAH NAINESH M.

DesignPattern Assignment-1
Example-1

Class Diagram:

2
SHAH NAINESH M.

DesignPattern Assignment-1
Code:
1.OS.java:

package DessignPattern1;

public interface OS {
public void spec();
}

2.Android.java:

package DessignPattern1;

public class Android implements OS{


public void spec() {
System.out.println("Android is MostPowerful..");
}
}

3.IOS.java:

package DessignPattern1;

public class IOS implements OS {


public void spec() {
System.out.println("IOS is Most Secured..");
}
}

4.Windows.java

package DessignPattern1;

public class Windows implements OS {


public void spec() {
System.out.println("I am about to Die..");
}
}

3
SHAH NAINESH M.

DesignPattern Assignment-1
5.osFactory.java

package DessignPattern1;

public class osFactory {


public OS objectCreation(String s) {
if(s.equalsIgnoreCase("android")) {
return new Android();
}else if(s.equalsIgnoreCase("ios")) {
return new IOS();
}else if(s.equalsIgnoreCase("windows")){
return new Windows();
}else {
System.out.println("Enter valid Type..");
}
return null;
}
}

6.Main.java

package DessignPattern1;
import java.util.*;
public class Main {
public static void main(String[] args) {
System.out.println("Name:Shah Nainesh M.\nRoll no.:22BCP496D\n");
Scanner sc=new Scanner(System.in);
System.out.print("Enter Any Os Name:");
String s=sc.nextLine();
osFactory of=new osFactory();
try {
OS obj=of.objectCreation(s);
obj.spec();
}catch(Exception e) {
System.out.println("Please Enter valid Os Name...");
}
finally {
System.out.print("\nThankyou..");
}
}
}

4
SHAH NAINESH M.

DesignPattern Assignment-1
Output Snapshot:

1.If User has Entered Android then,

2.if User has Entered Ios then,

3.If User has Entered Windows then,

4.if User has Enter Anything else then,

5
SHAH NAINESH M.

DesignPattern Assignment-1
Example-2

Class Diagram:

6
SHAH NAINESH M.

DesignPattern Assignment-1
Code:
1.CAR.java:

package SecondEx;

public interface Car {


public void spec();
}

2.Suv_car.java:

package SecondEx;

public class Suv_car implements Car{


public void spec(){
System.out.println("Suv car Having 7 seats..");
}
}

3.Sports_car.java:

package SecondEx;

public class Sports_car implements Car {


public void spec(){
System.out.println("Sports car Having top Speed 200...");
}
}

4.Convientable_car.java

package SecondEx;

public class Convientable_car implements Car{

public void spec() {


System.out.println("Drive Car With Or WithOut roof..");

}
5.carFactory.java

7
SHAH NAINESH M.

DesignPattern Assignment-1
package SecondEx;

public class carFactory {

public Car carCreate(String s) {


if(s.equalsIgnoreCase("Suv")) {
return new Suv_car();
}
else if(s.equalsIgnoreCase("Sports")) {
return new Sports_car();
}
else if(s.equalsIgnoreCase("Convientable")) {
return new Convientable_car();
}
else{
return null;
}
}
}

6.Main.java

package SecondEx;
import java.util.*;
public class Main {
public static void main(String args[]) {
System.out.println("Name:Shah Nainesh M.\nRoll no.:22BCP496D\n");
Scanner sc=new Scanner(System.in);
System.out.print("Enter Any Car Type Name:");
String s=sc.nextLine();
carFactory cf=new carFactory();
try {
Car obj=cf.carCreate(s);
obj.spec();
}catch(Exception e) {
System.out.println("Sorry,More Data Added Soon..");
}finally {
System.out.println("Thank you..");
}
}
}

8
SHAH NAINESH M.

DesignPattern Assignment-1
Output Snapshot:

1.If User has Entered Suv/suv then,

2.if User has Entered Sports/sports then,

3.If User has Entered then,

4.if User has Enter Any other car type then,

You might also like