Interface vs Enum
Adding and Substation program in MVC Style implemented/Solved in 3 ways
Model 1: Using Interface.
Model 2: Using Enum Extends Interface.
Model 3: Using Enum in Interface.
Which Model is Better/Suggested/Best according to Project Standards....
Suggest Me:
Mode1 : Using Interface.
package Interface;
/**
*
* @author Srikanth Ganji
*/
public interface Operation {
int caluclate(int firstValue,int secondValue);
}
package Interface;
/**
* @author Srikanth Ganji
*/
public class Addition implements Operation {
@Override
public int caluclate(int firstValue, int secondValue) {
return firstValue+secondValue;
}
}
package Interface;
/**
* @author Srikanth Ganji
*/
public class Substraction implements Operation {
@Override
public int caluclate(int firstValue, int secondValue) {
int returnValue = 0;
return firstValue == secondValue ? 0 : ((firstValue > secondValue) ? (firstValue - secondValue) : (secondValue - firstValue));
}
}
package Interface;
/**
* @author Srikanth Ganji
*/
public class Process implements Operation {
private int firstValue;
private int secondValue;
private Operation operation;
public Process(int firstValue, int secondValue, Operation operation) {
this.firstValue = firstValue;
this.secondValue = secondValue;
this.operation = operation;
}
public Process(Operation operation) {
this.operation = operation;
}
public int caluclate() {
return caluclate(firstValue, secondValue);
}
@Override
public int caluclate(int firstValue, int secondValue) {
return operation.caluclate(firstValue, secondValue);
}
}
package Interface;
/**
*
* @author Srikanth Ganji
*/
public class Controller {
public static void main(String[] args) {
int firstValue = 10;
int secondValue = 20;
Process addition = new Process(new Addition());
Process subStraction = new Process(new Substraction());
System.out.println(" Addition Value is : " + addition.caluclate(firstValue, secondValue));
System.out.println(" Substraction Value is : " + subStraction.caluclate(firstValue, secondValue));
}
}
Sample OutPut:
Addition Value is : 30
Substraction Value is : 10
Model 2 : Using Enum Extends Interface.
package enums.practice;
/**
*
* @author Srikanth Ganji
*/
public interface Operator {
int caluclate(int firstValue, int secondValue);
}
package enums.practice;
/**
*
* @author Srikanth Ganji
*/
public enum Process implements Operator {
SUM {
public int caluclate(int firstValue, int secondValue) {
return firstValue + secondValue;
}
},
SUBSTRACTION {
public int caluclate(int firstValue, int secondValue) {
return firstValue == secondValue ? 0 : ((firstValue > secondValue) ? (firstValue - secondValue) : (secondValue - firstValue));
}
}
}
package enums.practice;
/**
*
* @author Srikanth Ganji
*/
public class EnumDao {
private int firstValue;
private int secondValue;
private Operator operator;
public EnumDao(int firstValue, int secondValue, Process process) {
this.firstValue = firstValue;
this.secondValue = secondValue;
this.operator=operator;
}
public int getResult() {
return operator.caluclate(firstValue, secondValue);
}
}
package enums.practice;
/**
*
* @author Srikanth Ganji
*/
public class EnumController {
public static void main(String[] args) {
int firstValue=10;
int secondValue=20;
EnumDao addition=new EnumDao(firstValue, secondValue, Process.SUM);
EnumDao substraction=new EnumDao(firstValue, secondValue, Process.SUBSTRACTION);
System.out.println(" Additon Value is : "+addition.getResult());
System.out.println(" Substraction Value is : "+substraction.getResult());
}
}
Sample Output :
Additon Value is : 30
Substraction Value is : 10
Model 3 :Using Enum in Interface.
package enums.interfaces;
/**
*
* @author Srikanth Ganji
*/
public interface Operation {
enum SUM {
add;
public static int caluclate(int firstValue, int secondValue) {
return firstValue + secondValue;
}
}
enum SUBSTRACT {
sub;
public static int caluclate(int firstValue, int secondValue) {
return firstValue == secondValue ? 0 : ((firstValue > secondValue) ? (firstValue - secondValue) : (secondValue - firstValue));
}
}
}
package enums.interfaces;
/**
*
* @author Srikanth Ganji
*/
public class Controller implements Operation{
public static void main(String[] args) {
int firstValue=10;
int secondValue=20;
System.out.println(" Addition is : "+SUM.caluclate(firstValue, secondValue));
System.out.println(" Substraction is : "+SUBSTRACT.caluclate(firstValue, secondValue));
}
}
Sample Out Put:
Addition is : 30
Substraction is : 10
No comments:
Post a Comment