D
David student
Guest
Hello All,
Could someone please explain me the difference between Factory method design pattern and Bridge pattern?
Because from my understanding both these design patterns are using to separate interface and implementation:
Decoupling abstraction from implementation. Abstraction separates the client code from the implementation. So, the implementation can be changed without affecting the client code and the client code need not be compiled when the implementation changes.
Factory Method:
Problem without Factory method:
There are cases where we have a library with some classes to implement the client business logic. From the client application we create objects of the library classes to complete the task.
But sometimes, based on the client requirement to incorporate additional functionality, we might need to add additional classes in the library. Then we need to create objects of the new classes in the client application.
So, each time a new change is made at the library side, Client would need to make some corresponding changes at its end and recompile the code.
Using Factory Method:
To avoid this problem, we decouple object creation from client application using Factory method. Client just needs to make call to library’s factory method without worrying about the actual implementation of creation of objects.
So we create Factory method to create objects and move it to the separate implementation file.
Now the implementation file is the only one that requires knowledge of the derived classes. Thus, if a change is made to any derived class, or any new class is added, the implementation file is the only file that needs to be recompiled. Everyone who uses the factory will only care about the interface, which should remain consistent throughout the life of the application.
Client Application interacts -> Factory method and calls--> Implementation
If I take the below sample program, after adding any class or any change in the class is it enough to recompile only Vehicle.cpp file?
And also when creating factory method, do we use static method?
Vehicle.h
#include <iostream>
using namespace std;
enum VehicleType {
VT_TwoWheeler, VT_ThreeWheeler, VT_FourWheeler
};
// Library classes
class Vehicle {
public:
virtual void printVehicle() = 0;
static Vehicle* Create(VehicleType type);
};
class TwoWheeler : public Vehicle {
public:
void printVehicle() {
cout << "I am two wheeler" << endl;
}
};
class ThreeWheeler : public Vehicle {
public:
void printVehicle() {
cout << "I am three wheeler" << endl;
}
};
class FourWheeler : public Vehicle {
public:
void printVehicle() {
cout << "I am four wheeler" << endl;
}
};
Vehicle.cpp (Implementation file)
// Factory method to create objects of different types.
// Change is required only in this function to create a new object type
Vehicle* Vehicle::Create(VehicleType type) {
if (type == VT_TwoWheeler)
return new TwoWheeler();
else if (type == VT_ThreeWheeler)
return new ThreeWheeler();
else if (type == VT_FourWheeler)
return new FourWheeler();
else return NULL;
}
Client.h file
// Client class
class Client {
public:
// Client doesn't explicitly create objects
// but passes type to factory method "Create()"
Client()
{
VehicleType type = VT_ThreeWheeler;
pVehicle = Vehicle::Create(type);
}
~Client() {
if (pVehicle) {
delete[] pVehicle;
pVehicle = NULL;
}
}
Vehicle* getVehicle() {
return pVehicle;
}
private:
Vehicle *pVehicle;
};
// Driver program
int main() {
Client *pClient = new Client();
Vehicle * pVehicle = pClient->getVehicle();
pVehicle->printVehicle();
return 0;
}
Thanks in advance.
Continue reading...
Could someone please explain me the difference between Factory method design pattern and Bridge pattern?
Because from my understanding both these design patterns are using to separate interface and implementation:
Decoupling abstraction from implementation. Abstraction separates the client code from the implementation. So, the implementation can be changed without affecting the client code and the client code need not be compiled when the implementation changes.
Factory Method:
Problem without Factory method:
There are cases where we have a library with some classes to implement the client business logic. From the client application we create objects of the library classes to complete the task.
But sometimes, based on the client requirement to incorporate additional functionality, we might need to add additional classes in the library. Then we need to create objects of the new classes in the client application.
So, each time a new change is made at the library side, Client would need to make some corresponding changes at its end and recompile the code.
Using Factory Method:
To avoid this problem, we decouple object creation from client application using Factory method. Client just needs to make call to library’s factory method without worrying about the actual implementation of creation of objects.
So we create Factory method to create objects and move it to the separate implementation file.
Now the implementation file is the only one that requires knowledge of the derived classes. Thus, if a change is made to any derived class, or any new class is added, the implementation file is the only file that needs to be recompiled. Everyone who uses the factory will only care about the interface, which should remain consistent throughout the life of the application.
Client Application interacts -> Factory method and calls--> Implementation
If I take the below sample program, after adding any class or any change in the class is it enough to recompile only Vehicle.cpp file?
And also when creating factory method, do we use static method?
Vehicle.h
#include <iostream>
using namespace std;
enum VehicleType {
VT_TwoWheeler, VT_ThreeWheeler, VT_FourWheeler
};
// Library classes
class Vehicle {
public:
virtual void printVehicle() = 0;
static Vehicle* Create(VehicleType type);
};
class TwoWheeler : public Vehicle {
public:
void printVehicle() {
cout << "I am two wheeler" << endl;
}
};
class ThreeWheeler : public Vehicle {
public:
void printVehicle() {
cout << "I am three wheeler" << endl;
}
};
class FourWheeler : public Vehicle {
public:
void printVehicle() {
cout << "I am four wheeler" << endl;
}
};
Vehicle.cpp (Implementation file)
// Factory method to create objects of different types.
// Change is required only in this function to create a new object type
Vehicle* Vehicle::Create(VehicleType type) {
if (type == VT_TwoWheeler)
return new TwoWheeler();
else if (type == VT_ThreeWheeler)
return new ThreeWheeler();
else if (type == VT_FourWheeler)
return new FourWheeler();
else return NULL;
}
Client.h file
// Client class
class Client {
public:
// Client doesn't explicitly create objects
// but passes type to factory method "Create()"
Client()
{
VehicleType type = VT_ThreeWheeler;
pVehicle = Vehicle::Create(type);
}
~Client() {
if (pVehicle) {
delete[] pVehicle;
pVehicle = NULL;
}
}
Vehicle* getVehicle() {
return pVehicle;
}
private:
Vehicle *pVehicle;
};
// Driver program
int main() {
Client *pClient = new Client();
Vehicle * pVehicle = pClient->getVehicle();
pVehicle->printVehicle();
return 0;
}
Thanks in advance.
Continue reading...