Bookmark Topic Watch Topic
  • New Topic

urgent. program not oo style?  RSS feed

 
nian zhang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi guys, i got a question like that:

Exercise 2 � The Copy Factory
Task: Refactor the code in exercise 2 to make better use of OO concepts and to a standard you consider acceptable for production code.

the code:

public class CopyFactory {

public CopyFactory() {
}


public Object createCopy(Object source) {
if (source instanceof Integer) {
return new Integer(((Integer)source).intValue());
}
if (source instanceof Long) {
return new Long(((Long)source).longValue());
}
if (source instanceof Float) {
return new Float(((Float)source).floatValue());
}
return null;
}

}





package com.systemsunion.precheck.exercise2;

import junit.framework.TestCase;

/**
*/
public class TestCase_CopyFactory extends TestCase {

public TestCase_CopyFactory(String name) {
super(name);
}

protected void setUp() throws Exception {
_factory = new CopyFactory();
}

protected void tearDown() throws Exception {
}

public void testInteger() throws Exception {
Integer source = new Integer(2);
Object copy = _factory.createCopy(source);
assertTrue("Wrong instance", copy instanceof Integer);
assertEquals("Should be equal", source, copy);
assertNotSame("Should be new instance", source, copy);
}

public void testLong() throws Exception {
Long source = new Long(2);
Object copy = _factory.createCopy(source);
assertTrue("Wrong instance", copy instanceof Long);
assertEquals("Should be equal", source, copy);
assertNotSame("Should be new instance", source, copy);
}

public void testFloat() throws Exception {
Float source = new Float(2.34f);
Object copy = _factory.createCopy(source);
assertTrue("Wrong instance", copy instanceof Float);
assertEquals("Should be equal", source, copy);
assertNotSame("Should be new instance", source, copy);

}

private CopyFactory _factory;
}

class TestCase_CopyFactory is a junit test, the TestCase is a included lib.

any suggestions for make the program more oo style?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
The easiest way, although not binary compatible, would be to replace the instanceof checks with overloading.
 
nian zhang
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Report post to moderator
Originally posted by Ilja Preuss:
The easiest way, although not binary compatible, would be to replace the instanceof checks with overloading.


what do you mean? can you give more detail?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Report post to moderator
Don't have one createCopy method for all different types, but one per type.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
This clearly seems to be a homework question posted in search of a solution, so I'll close it here.
 
It is sorta covered in the JavaRanch Style Guide.
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!