0% found this document useful (0 votes)
5 views1 page

Understanding the Factory Pattern in Java

Uploaded by

iheb.zenkri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Understanding the Factory Pattern in Java

Uploaded by

iheb.zenkri
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Factory Pattern

abstract class Vehicle { class Client {


public abstract void printVehicle(); private Vehicle pVehicle;
}
public Client(VehicleFactory factory) {
class TwoWheeler extends Vehicle { pVehicle = [Link]();
public void printVehicle() { }
[Link]("I am two wheeler");
} public Vehicle getVehicle() {
} return pVehicle;
}
class FourWheeler extends Vehicle { }
public void printVehicle() {
[Link]("I am four wheeler"); public class GFG {
} public static void main(String[] args) {
} VehicleFactory twoWheelerFactory =
new TwoWheelerFactory();
interface VehicleFactory {
Vehicle createVehicle(); Client twoWheelerClient =
} new Client(twoWheelerFactory);

class TwoWheelerFactory implements Vehicle twoWheeler = [Link]();


VehicleFactory { [Link]();
public Vehicle createVehicle() {
return new TwoWheeler(); VehicleFactory fourWheelerFactory =
} new FourWheelerFactory();
}
Client fourWheelerClient =
class FourWheelerFactory implements new Client(fourWheelerFactory);
VehicleFactory {
public Vehicle createVehicle() { Vehicle fourWheeler = [Link]();
return new FourWheeler(); [Link]();
} }
} }

You might also like