Im currently looking through some lecture notes, the lecturer has provide the following code for abstract factory pattern implementation, however he doesn't provide an interface which is what an abstract factory pattern is. There should be no definition of factory methods for an interface
Concrete product classes:
class inductor;
class resistor;
class circuit {
void add_inductor(inductor*);
void add_resistortor(inductor*);
};
Assembler class:
class circuit_simulation {
public:
circuit* make_LR_circuit(circuit_factory& CF){
circuit* cct=CF.make_circuit();
inductor* L=CF.make_inductor(1);
resistor* R=CF.make_resistor(1);
cct->add_inductor(L);
cct->add_resistor(R);
return(cct);
};
Abstract factory base class:
class circuit_factory {
public:
circuit_factory();
virtual circuit* make_circuit() const {return(new circuit);}
virtual resistor* make_resistor(float R) const {return(new
resistor(R));}
virtual inductor* make_inductor(float L) const {return(new
inductor(L));}
};
Main code:
main(){
circuit_factory CF;
circuit_simulation sim;
circuit* cct=sim.make_LR_circuit(CF);
// The circuit “contains” objects of
// the type inductor, resistor etc
}
Aucun commentaire:
Enregistrer un commentaire