目录
学习笔记
抽象类与接口时Java中的一个核心概念,是所有设计模式的综合体现,包括日后学习过程之中也会接触系统提供的接口和抽象类。在接口与抽象类都能使用的情况下优先使用接口,因为接口可以避免单继承的一个实现。
案例分析一(获取类信息)
定义一个ClassName的接口,接口之中只有一个方法getClassName();设计一个类Company,该类能够实现getCalssName(),功能是获取该类名称。
interface IClassName{
public abstract String getClassName() ; //数据一定要返回
}
class Company implements IClassName{
public String getClassName(){
return "Company" ;
}
}
public class JavaDemo{
public static void main(String[] args){
IClassName company = new Company() ;
System.out.println(company.getClassName()) ;
}
}
接口前一定要追加字母“I”
案例分析二(绘图处理)
考虑一个绘图的标准,并且可以根据不同的图形来绘制;
interface IGraphical{ // 定义绘图标准
public void paint() ; // 绘图
}
class Point{
private double x ;
private double y ;
public Point(double x, double y){
this.x = x ;
this.y = y ;
}
public double getX(){
return this.x ;
}
public double getY(){
return this.y ;
}
}
class Triangle implements IGraphical{ //绘制三角形
private Point [] x ; // 保存第一条边的坐标
private Point [] y ; // 保存第二条边的坐标
private Point [] z ; // 保存第三条边的坐标
public Triangle(Point [] x, Point[] y, Point [] z){
this.x = x ;
this.y = y ;
this.z = z ;
}
public void paint(){
System.out.println("绘制第一条边,开始坐标:[" + this.x[0].getX() + "," + this.x[0].getY() + "]" ) ;
System.out.println("绘制第一条边,结束坐标:[" + this.x[1].getX() + "," + this.x[1].getY() + "]" ) ;
System.out.println("绘制第二条边,开始坐标:[" + this.y[0].getX() + "," + this.y[0].getY() + "]" ) ;
System.out.println("绘制第二条边,结束坐标:[" + this.y[1].getX() + "," + this.y[1].getY() + "]" ) ;
System.out.println("绘制第三条边,开始坐标:[" + this.z[0].getX() + "," + this.z[0].getY() + "]" ) ;
System.out.println("绘制第三条边,结束坐标:[" + this.z[1].getX() + "," + this.z[1].getY() + "]" ) ;
}
}
class Circular implements IGraphical{
private double radius ;
public Circular(double radius){
this.radius = radius ;
}
public void paint(){
System.out.println("以半径为" + this.radius + "绘制圆") ;
}
}
class Factory{
public static IGraphical getInstance(String className, double ... args){
if("triangle".equalsIgnoreCase(className)){
return new Triangle(
new Point [] {new Point(args[0],args[1]), new Point(args[2],args[3])}, // x
new Point [] {new Point(args[4],args[5]), new Point(args[6],args[7])}, // y
new Point [] {new Point(args[8],args[9]), new Point(args[10],args[11])} //z
);
}else if ("circular".equalsIgnoreCase(className)){
return new Circular(args[0]);
}else{
System.out.println("图形出错,无法绘制") ;
return null ;
}
}
}
public class JavaDemo{
public static void main(String[] args){
new Factory().getInstance("Triangle", 0.0, 0.0, 0.0,1.0, 0.0,1.0, 1.0,0.0, 1.0,0.0,0.0,0.0).paint();
new Factory().getInstance("circular", 2).paint();
}
}
案例分析三(图形)
定义Shape,用来表示一般的图形。Shape具有抽象方法area和perimeter,分别用来计算形状的面积和周长。使定义一些二维形状,这些类均为shape的子类。
abstract class AbstractShape{
public abstract double area() ;
public abstract double perimeter() ;
}
class Circular extends AbstractShape{
private double radius ;
public Circular(double radius){
this.radius = radius ;
}
public double area(){
return 3.14 * this.radius * this.radius ;
}
public double perimeter(){
return 3.14 * 2 * this.radius ;
}
}
class Rectangle extends AbstractShape{
private double length ;
private double width ;
public Rectangle(double length, double width){
this.length = length ;
this.width = width ;
}
public double area(){
return this.length * this.width ;
}
public double perimeter(){
return 2*this.length + 2*this.width ;
}
}
class Factory{
public static AbstractShape getInstance(String ClassName, double ... args){
if ("圆形".equals(ClassName)){
return new Circular(args[0]) ;
}else if("矩形".equals(ClassName)){
return new Rectangle(args[0], args[1]);
}else{
System.out.println("输入不支持") ;
return null ;
}
}
}
public class JavaDemo{
public static void main(String[] args){
AbstractShape graphA = new Factory().getInstance("圆形", 2.0);
System.out.println(graphA.area()) ;
System.out.println(graphA.perimeter()) ;
AbstractShape graphB = new Factory().getInstance("矩形", 2.0, 3.0);
System.out.println(graphB.area()) ;
System.out.println(graphB.perimeter()) ;
}
}
使用工厂设计模式主要特点使完全隐藏子类。