java - Does this design make sense? -
does below design you? design pattern? how improve if think needs refactoring?
public class fruitfetcher { public static void main(string[] args) { fruitfetcher fetcher = new fruitfetcher(); apple apple = (apple) fetcher.fetch(new fetchapplerequest()); } public fruit fetch(fetchfruitrequest request){ fruit fruit = null; if(request.getfruitname().equals(fetchapplerequest.request_name)){ fruit = new apple(); }else if (request.getfruitname().equals(fetchbananarequest.request_name)){ fruit = new banana(); } return fruit; } } abstract class fetchfruitrequest{ abstract public string getfruitname(); } class fetchapplerequest extends fetchfruitrequest{ static string request_name = "fetch_apple"; @override public string getfruitname() { return request_name; } } class fetchbananarequest extends fetchfruitrequest{ static string request_name = "fetch_banana"; @override public string getfruitname() { return request_name; } } class fruit { } class apple extends fruit{ } class banana extends fruit{ }
in code, clients of fruitfetcher
need upcast fruit
right type, think correct?
edit: answer elite gentleman's question, modified code show reqeust
needs type other simple string.
does getresponse()
in paymentserver
still kind of 'ugly'? how re-fector it?
public class paymentserver { public static void main(string[] args) { paymentserver server = new paymentserver(); //set pin setpinresponse setpinresponse = (setpinresponse) server.getresponse(new setpinrequest("apin")); system.out.println(setpinresponse.issuccess()); //make payment makepaymentresposne makepaymentresponse = (makepaymentresposne) server.getresponse(new makepaymentrequest(new money("5.00)"),"apin")); system.out.println(makepaymentresponse.issuccess()); } public response getresponse(request request){ response aresponse = null; if(request.getrequestname().equals(setpinrequest.request_name)){ aresponse = new setpinresponse(); }else if (request.getrequestname().equals(makepaymentrequest.request_name)){ aresponse = new makepaymentresposne(); } return aresponse; } } abstract class request{ abstract public string getrequestname(); } class setpinrequest extends request{ static string request_name = "set_pin"; private string pin; setpinrequest(string pin){ this.pin = pin; } @override public string getrequestname() { return request_name; } boolean setpin(){ //code set pin return true; } } class makepaymentrequest extends request{ static string request_name = "make_payment"; private money amount; private string pin; makepayment(money amount, string pin){ this.amount = amount; this.pin = pin; } @override public string getrequestname() { return request_name; } } abstract class response { abstract protected boolean issuccess(); } class setpinresponse extends response{ @override protected boolean issuccess() { return true; } } class makepaymentresposne extends response{ @override protected boolean issuccess() { return false; } }
thanks,
sarah
your design pretty close factory pattern. "fetcher" fruit factory, ask fruit of special type , fruit.
the "request" part looks bit complicated. consider using enums:
public enum fruittype{ apple, banana }
public fruitfetcher { public static fruit fetch(fruittype type) { switch(type) { case apple: return new apple(); case banana: return new banana(); } } return null; }
edit - it's still pretty complicated. takes while understand, want achieve , indicator there must easier design.
first - if request
, response
classes offer nothing abstract method declarations, should refactor code code both types interfaces. 1 step towards improved readability.
second - getresponse methd can simplified. don't need (ugly) getname construct - check type of request object:
public response getresponse(request request){ response aresponse = null; if(request instanceof setpinresponse) { aresponse = new setpinresponse((setpinrequest) request); } else if (request instanceof makepaymentresposne) { aresponse = new makepaymentresposne((makepaymentrequest) request); } return aresponse; }
Comments
Post a Comment