Tuesday 15 May 2012

c# - How can i improve my class design for the following scenario to improve code -



c# - How can i improve my class design for the following scenario to improve code -

i have set of functions namely a(),b(),c(),d() , have 5 forms namely frome,formf,formg,formh,formi.

all forms utilize some or none or all of functions a(),b(),c(),d().

what doing

created interface isetoffunctions consisting of a(),b(),c() , d() , other properties used forms implementing in every form forme,formf,formg,formh , formi

the problem

1.forme not utilize functions

2.formf uses a(),b(),c().

3.formg uses functions.

4.formi uses b(),c()

public interface isetoffunction { public int neededinallform { get;set;} void a(); void b(); void c(); void d(); } //none of functions needed public class e:form,isetoffunction { public int neededinallform { get;set;} public void a() { } public void b() { } public void c() { } public void d() { } } //only a() , b() needed public class f:form,isetoffunction { public int neededinallform { get;set;} public void a() { } public void b() { } public void c() { } public void d() { } }

note: can have more forms have different combinations set of functions provided.

my current design makes makes code implement functions though not required.

i felt utilize strategy pattern couldn't come solution.

kindly help me out provide tutorial if possible

if functions same, or same in cases, consider creating abstract class inherits form (let's phone call baseform) , implements these functions. have forms e-i inherit baseform. there single "base" implementation functions.

if particular form's function needs work differently, can mark function in base of operations class virtual , override in inheriting class.

public abstract class baseform : form { public int needallinform {get;set;} public void a() { //default implementation here } public void b() { //default implementation here } public void c(){ //default implementation here } public virtual void d() { //default implementation here } } public class f : baseform {} public class g : baseform { public override void d() { //form-specific implementation of d here } }

c# oop design-patterns

No comments:

Post a Comment