12 August 2016

Abstract class vs Interface Design Pattern in Java

Abstract class Design Pattern
Java 2016
package Demo12;

public abstract class School {

 public void displayNote(){
  System.out.println("Please pay ....");//Điểm chung hiển thị cho các class con 
 }
 public abstract String calculatePoint(int point);
}
package Demo12;

public class SchoolA extends School {

 @Override
 public String calculatePoint(int point) {
  if (point > 85) {
   return "A";
  } else if (point > 70) {
   return "B";
  } else if (point > 55) {
   return "C";
  } else {
   return "D";
  }
 }
}
package Demo12;

public class SchoolB extends School{

 @Override
 public String calculatePoint(int point) {
  return new Integer(Math.round(point/ (float)10.0)).toString();
 }

}
package Demo12;

public class Student {

 private String name;
 private int point;
 private School school;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getPoint() {
  return point;
 }

 public void setPoint(int point) {
  this.point = point;
 }

 public School getSchool() {
  return school;
 }

 public void setSchool(School school) {
  this.school = school;
 }

 public void displaỵ̣Name() {
  System.out.println(name + " point " + school.calculatePoint(point));
  school.displayNote(); //Điểm chung không cần thay đổi khi dùng abstract
 }

 public static void main(String[] args) {
  Student s = new Student();
  s.setName("Tony");
  s.setPoint(55);
  s.setSchool(new SchoolB());
  s.displaỵ̣Name();
 }
}
Output
Tony point 6
Please pay ....
Interface Design Pattern
Java 2016
package Demo12;

public interface Univercity {

 public String calculatePoint(int point);
}
package Demo12;

public class UnivercityA implements Univercity {

 @Override
 public String calculatePoint(int point) {
  if (point > 85) {
   return "A";
  } else if (point > 70) {
   return "B";
  } else if (point > 55) {
   return "C";
  } else {
   return "D";
  }
 }

}
package Demo12;

public class UnivercityB implements Univercity{

 @Override
 public String calculatePoint(int point) {
  return new Integer(Math.round(point/ (float)10.0)).toString();
 }

}
package Demo12;

public class Student {
 
 private String name;
 private int point;
 private Univercity univercity;

 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getPoint() {
  return point;
 }
 public void setPoint(int point) {
  this.point = point;
 }
 public Univercity getUnivercity() {
  return univercity;
 }
 public void setUnivercity(Univercity univercity) {
  this.univercity = univercity;
 }
 public void displaỵ̣Name(){
  System.out.println(name +" point "+ univercity.calculatePoint(point));
 }
 public static void main(String[] args) {
  Student s = new Student();
  s.setName("Tony");
  s.setPoint(55);
  s.setUnivercity(new UnivercityB());//or new UnivercityA();
  s.displaỵ̣Name();
 }
 
} 
Output: Tony point 6

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang