Design Pattern - Factory

Factory

1
2
3
4
5
6
7
8
9
10
11
{  
//real case invoking
invokeSharedCode(new BikeFactory());
invokeSharedCode(new CarFactory());
}

//object being created depending on factory type, and behavior is defined in interface also
public static void invokeSharedCode(TransportFactory factory){
Transport transport = factory.create();
System.out.println(transport.drive());
}

core concept

Shared code interface not hardcoded with typeA or typeB,
but typeA and B all extend the same factory and initialization using same function.

After create, we can invoke the same name function provided by typeA or B.

Reward Makes Perfect
0%