java - How to implement a method for multiple strategies having completely different logic? -
i find hard give question concret title, i'm not aware of how name type of problem. suggestions welcome.
i have service run logic literally performs same action (eg save thumbnail), contains different logic different providers:
@service public class thumbnailservice { public void saveforprovider1(prov1 url) { } public void saveforprovider2(prov2 url) { //completely different logic provider1; different parameter object "prov2" } }
problem 1: new provider
have create additional method in thumbnailservice
.
now @ specific point, want run methods async:
//basically async wrapper service @service @async public class thumbnailserviceasync { @autowired private thumbnailservice delegator; public class asyncsaveforprovider1(prov1 url) { delegator.saveforprovider1(url); } public class asyncsaveforprovider2(prov2 url) { delegator.saveforprovider2(url); } }
switching aninteger
variable know number stands provider:
int provider; switch (provider) { case 1: thumbserviceasync.asyncsaveforprovider1(url); break; case 2: thumbserviceasync.asyncsaveforprovider2(url); break; default: throw notsupportedexception(); }
problem 2: see i'm writing lots of code sake of delegation specific provider routine. , have touch @ least 3 classes mentioned when introduction additional provider.
question: how optimize process? regarding time more providers may implemented. , not thumbnail routine, furhter methods have implemented every provider, different logic. "create user providerx", or "run cleanup routine providerx".
interface provider { void specificprovidercode(); } public class provider1 implements provider { public void specificprovidercode(){ } } public class providerfactory{ public static provider createprovider(providertype providertype){ if(providertype.equals(providertype.type_1){ return new provider1(); } } } @service public class thumbnailservice { public void saveforprovider(provider provider){ provider.specificprovidercode(); } } @service @async public class thumbnailserviceasync { @autowired private thumbnailservice delegator; public class asyncsaveforprovider(providertype providertype) { provider url = providerfactory.createprovider(providertype); delegator.saveforprovider(url); } }
now when adding new provider there need make changes(to create new 1 specific logic), both services not changed, means callers services not need change code.
Comments
Post a Comment