Created
December 22, 2013 13:45
-
-
Save anonymous/8082883 to your computer and use it in GitHub Desktop.
Java Interface, Abstract Class and Concrete Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AbstractMathOperation implements MathOperation{ | |
public void preOperate(Integer... values){ | |
System.out.println("This is a pre operation "); | |
} | |
public void postOperate(Integer... values){ | |
System.out.println("This is a post operation "); | |
} | |
public Integer operate(Integer... values){ | |
preOperate(values); | |
Integer result = doOperation(values); | |
postOperate(values); | |
return result; | |
} | |
/** | |
* This method should be implemented by all the sub classes to implement actual logic "middle code" | |
*/ | |
public abstract Integer doOperation(Integer... values); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Add extends AbstractMathOperation{ | |
@Override | |
public Integer doOperation(Integer... values){ | |
Integer i = 0; | |
for(Integer value : values){ | |
i += value; | |
} | |
return i; | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main{ | |
public static final MathOperation ADD = new Add(); | |
public static final MathOperation SUBTRACT = new Subtract(); | |
public static final MathOperation MULTIPLY = new Multiply(); | |
public static final void main(String [] args){ | |
System.out.ptintln(ADD.operate(1,2,3,4,5,6)); | |
System.out.ptintln(SUBTRACT.operate(10,5,2)); | |
System.out.ptintln(MULTIPLY.operate(1,2,3,4,5,6)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface MathOperation{ | |
public Integer operate(Integer ... values); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Multiply extends AbstractMathOperation{ | |
@Override | |
public Integer doOperation(Integer... values){ | |
Integer i = 1; | |
for(Integer value : values){ | |
i *= value; | |
} | |
return i; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Subtract extends AbstractMathOperation{ | |
@Override | |
public Integer doOperation(Integer... values){ | |
Integer i = 0; | |
for(Integer value : values){ | |
i -= value; | |
} | |
return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment