[Java Design Pattern] [Creational Pattern] - Factory Method
Wednesday, May 9, 2018
Creational Pattern - Factory Method Pattern
Purpose: The Factory Method Pattern gives us a way encapsulate the instancetiation of concrete types. Instead you delcare new direct object, you use Factory class to create a new object.
Example 1:
IVehicle
package com.designpattern.creational.factorymethod;
public interface IVehicle {
void run();
}
Vehicle
package com.designpattern.creational.factorymethod;
public class Vehicle implements IVehicle {
private String color;
@Override
public void run() {
System.out.println("Not Run");
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Bike
package com.designpattern.creational.factorymethod;
public class Bike extends Vehicle {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Bike is running...");
}
}
Car
package com.designpattern.creational.factorymethod;
public class Car extends Vehicle {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Car is running...");
}
}
Motobike
package com.designpattern.creational.factorymethod;
public class Motobike extends Vehicle {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Motobike is running...");
}
}
VechicleFactory
package com.designpattern.creational.factorymethod;
public class VehicleFactory {
public static Vehicle createVehicle(String s) {
Vehicle vehicle = null;
if ("bike".equals(s)) {
vehicle = new Bike();
} else if ("motobike".equals(s)) {
vehicle = new Motobike();
} else if ("car".equals(s)) {
vehicle = new Car();
} else {
vehicle = new Vehicle();
}
return vehicle;
}
}
VehicleMain
package com.designpattern.creational.factorymethod;
public class VehicleMain {
public static void main(String[] args) {
Vehicle bike = VehicleFactory.createVehicle("bike");
bike.run();
Vehicle motobike = VehicleFactory.createVehicle("motobike");
motobike.run();
Vehicle car = VehicleFactory.createVehicle("car");
car.run();
}
}
Result:
Bike is running...
Motobike is running...
Car is running...
Purpose: The Factory Method Pattern gives us a way encapsulate the instancetiation of concrete types. Instead you delcare new direct object, you use Factory class to create a new object.
Factory Method concept UML. (Source: Software Architecture Design Patterns in Java.pdf) |
Example 1:
IVehicle
package com.designpattern.creational.factorymethod;
public interface IVehicle {
void run();
}
Vehicle
package com.designpattern.creational.factorymethod;
public class Vehicle implements IVehicle {
private String color;
@Override
public void run() {
System.out.println("Not Run");
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Bike
package com.designpattern.creational.factorymethod;
public class Bike extends Vehicle {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Bike is running...");
}
}
Car
package com.designpattern.creational.factorymethod;
public class Car extends Vehicle {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Car is running...");
}
}
Motobike
package com.designpattern.creational.factorymethod;
public class Motobike extends Vehicle {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Motobike is running...");
}
}
VechicleFactory
package com.designpattern.creational.factorymethod;
public class VehicleFactory {
public static Vehicle createVehicle(String s) {
Vehicle vehicle = null;
if ("bike".equals(s)) {
vehicle = new Bike();
} else if ("motobike".equals(s)) {
vehicle = new Motobike();
} else if ("car".equals(s)) {
vehicle = new Car();
} else {
vehicle = new Vehicle();
}
return vehicle;
}
}
VehicleMain
package com.designpattern.creational.factorymethod;
public class VehicleMain {
public static void main(String[] args) {
Vehicle bike = VehicleFactory.createVehicle("bike");
bike.run();
Vehicle motobike = VehicleFactory.createVehicle("motobike");
motobike.run();
Vehicle car = VehicleFactory.createVehicle("car");
car.run();
}
}
Result:
Bike is running...
Motobike is running...
Car is running...
Bài liên quan
Comments[ 0 ]
Post a Comment