c# - Correcting implementation of a Factory Method with parameters -
i'm making solution program builds robots different types of parts(c#). in case have 2 types share attributes , both types of part inherit abstract class part.
until calling new actual button code in interface window , this.
if (type == acuatic) part piecea = new acuaticpart(type,name,price,maxdepth); else part pieceb = new terrestrialpart(type,name,price,terrain,maxtemp);
i know totally wrong design , should implementing factory method. thing don't know if it's ok send parameters factory this:
in interface window:
part piece = _partfactory.createpart(type,name,price,maxdepth,terrain,maxtemp);
in concrete factory:
public class concretepartfactory : partfactory { public override part createpart(type,name,price,maxdepth,terrain,maxtemp) { part mypart = default(part); switch (type) { case "actuatic": mypart = new aquaticpart(type,name,price,maxdepth); break; case "terrestrial": mypart = new terrestrialpart(type,name,price,terrain,maxtemp); break; } return mypart; } }
the thing still have pass attributes , think isn't following open/closed principle, can fix this? thanks!
you may try overload factory method different signatures corresponding input arguments mentioned. in case, implement 2 methods, same name, having adequate , minimal set of arguments return objects of different classes. using approach may drop "type" well
Comments
Post a Comment