java - Interface method referencing a concrete class as parameter causes coupling? -
i thinking programming interfaces , not concrete classes, had doubt: should interface method able hold references concrete classes?
suppose following scenarios:
1)
public interface abstype1 { public boolean method1(int a); // it's ok, primitive types here }
2)
public interface abstype2 { public boolean method2(myclass a); // think have coupling here }
should choose different design here in order avoid latter? e.g.
public interface myinterface {} // yes, empty public classe myclass implements myinterface { // identical previous "myclass" } public interface abstype2 { public boolean method2(myinterface a); // better (as long // interface stable) }
but there's still doesn't convince me... feel uncomfortable declaring empty interface, though saw else doing so. maybe , abstract class work better here?
i little bit confused.
edit:
ok, i'll try more specific making example. let's i'm desining shopcart , want of course add items cart:
public interface shopcart { public void addarticle(article a); }
now, if article concrete class, if implementation changes on time? why think of making interface, again, it's not suitable @ least @ semantic level because interfaces should specify behaviours , article has none (or none... guess it's sort of entity class).
so, i'm ending right conclusion making article abstract class in case best thing... think it?
i use interfaces because composition better inheritance. "should interface method able hold references concrete classes ?", why shouldn't? classes within package coupled, it's fact , common use technique. when marked relation in interface see on classes dependent implementation. dependency or composition relations not inheritance avoid abstract class.
Comments
Post a Comment