tag:blogger.com,1999:blog-23939980.post115596820104142887..comments2023-09-25T17:29:29.440+10:00Comments on Java and other things: Swich Statement code smell and PolymorphismAnonymoushttp://www.blogger.com/profile/11568089987006666223noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-23939980.post-91813895988679661002013-09-23T19:55:27.086+10:002013-09-23T19:55:27.086+10:00Mark,
If you're having to do that, it's B...Mark,<br /><br />If you're having to do that, it's BAD design.<br /><br />Your method should accept a String or an int/Integer, NOT an arbitrary object which can be anything. Create two methods, one for String, one for int/Integer if necessary. Don't create one method for Object - that's horrible design.ADTChttps://www.blogger.com/profile/06989222865444336009noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-62511899263827142492010-05-08T01:48:39.212+10:002010-05-08T01:48:39.212+10:00So how do you fix this?
if (obj instanceof Str...So how do you fix this?<br /><br /><br /> if (obj instanceof String) {<br /> // it's a String<br /> } else if (obj instanceof Integer) {<br /> // it's an Integer<br /> }Unknownhttps://www.blogger.com/profile/10250693486265872780noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-61661025859357485452008-09-12T21:12:00.000+10:002008-09-12T21:12:00.000+10:00Hey,Thanks for this - Someone told me they were as...Hey,<BR/><BR/>Thanks for this - Someone told me they were asked this at interview so it's a good one to know!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-23939980.post-24188653020067786212007-10-02T04:50:00.000+10:002007-10-02T04:50:00.000+10:00I think the point here is that a switch statement ...I think the point here is that a switch statement (pretty much) always represents some conceptual commonality. For anonymous, that means that you don't put GetNumberOfSides() or GetRadius() into the common interface. You also wouldn't write a method like this:<BR/><BR/>int GetRadius(shape) <BR/> switch(shape.Type)<BR/> Type.Circle:<BR/> // calculate radius<BR/> Type.Square:<BR/> // What happens here?<BR/><BR/>You can implement as many interfaces as you want, so if something represents a first class concept in your conceptual model, no reason not to elevate it to an interface. Dushan is right of course that it's likely unmanageable to create a single interface for every switch statement, but finding the ones that make sense for your application to avoid switch or big if/then blocks is why design isn't easy.Rob Meyerhttps://www.blogger.com/profile/11148635463970652662noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-30926305433952674372007-08-11T17:58:00.000+10:002007-08-11T17:58:00.000+10:00Dude, inheritance is good if your objects have som...Dude, inheritance is good if your objects have something in common (e.g. all shapes have an area).<BR/><BR/>Obviously, inheritance is not suitable for everything, it isn't a silver bullet to code re-use.<BR/><BR/>If you are interested in specifics of each implementation or your objects do not have much in common you'll need to use something else.Anonymoushttps://www.blogger.com/profile/11568089987006666223noreply@blogger.comtag:blogger.com,1999:blog-23939980.post-33503403500786941062007-08-10T10:03:00.000+10:002007-08-10T10:03:00.000+10:00Obviously a programming genius.if you implement an...Obviously a programming genius.<BR/>if you implement an interface , each shape has to provide functions of the interface.<BR/><BR/>so how many sides does a circle have?<BR/>and whats the radius of a square?Anonymousnoreply@blogger.com