面向对象编程中Association、Aggregation和Composition的区别


  大家在最初接触面向对象编程时,都难免会遇到描述类与类之间关系的三个词:Association、Aggregation和Composition。对于初学者而言,往往很难弄清楚它们之间的区别。现在,我就这三者之间的区别,做一个大致的说明。

从概念上来讲:

  Association描述的是类与类之间一般的关联关系,而Aggregation和Composition都有整体和部分的关系。其中Aggregation中的部分脱离了整体,部分仍然有意义,是共享式的。而Composition中的部分脱离了整体,部分将没有任何意义,是独占式的。

从实现的角度上讲:

  三者中以属性出现时,有着不同的实现方法。其中Association中作为属性出现时,不需要对其进行强制赋值,只要在使用时对其进行初始化即可。Aggregation中作为属性出现时,需要在构造器中通过传递参数来对其进行初始化。Composition作为属性出现时,需要在整体的构造器中创建部分的具体实例,完成对其的实例化。

  下面通过具体的例子来更深刻的理解这三者的区别。先看第一个例子,其中Student类和BasketBall类之间是Association的关系:

public class Student{

private String name;

private int age;

BasketBall aBall;

public Student(String name, int age){

this.name=name;

this.age=age;

}

public void getBall(BasketBall aBall){

this.aBall=aBall;

}

public void play(){

System.out.println("I am playing basketball"+aBall);

}

}

class BasketBall{

private Color aColor;

private int size;

public BasketBall(Color aColor, int size){

this.aColor=aColor;

this.size=size;

}

}

class StudentAdmin{

public static void main(String aa[]){

Student aStudent=new Student("Leo", 25);

BasketBall aBasketBall=new BasketBall(Color.red, 32);

aStudent.getBall(aBasketBall);

aStudent.play();

}

}

  再看一个Aggregation的例子,在这段代码中,Computer类和Monitor类之间是Aggregation的关系:

public class Computer{

private String cpu;

private float weight;

private Monitor aMonitor;

public Computer(String cpu, float weight, Monitor aMonitor){

this.cpu=cpu;

this.weight=weight;

this.aMonitor=aMonitor;

}

public void turnOn(){ System.out.println("I am on."); }

}


class Monitor{

private int inch;

private boolean isFlat;

//no information of computer

public Monitor(int inch, boolean isFlat){

this.inch=inch;

this.isFlat=isFlat;

}

}

class ComputerAdmin{

public static void main(String aa[]){

Monitor aMonitor=new Monitor(15, true);

System.out.println("I do something others here");

Computer aComputer=new Computer(586, 32.0, aMonitor);

System.out.println("Computer is :"+aComputer);

aComputer.turnOn();

}

}

  最后是Composition的例子,与上一段代码不同的是,Computer类和Monitor类之间变成了Composition的关系:

public class Computer{

private String cpu;

private float weight;

private Monitor aMonitor;

public Computer(String cpu, float weight, int inch, boolean isFlat){

this.cpu=cpu;

this.weight=weight;

this.aMonitor=new Monitor(inch, isFlat);

}

public void turnOn(){ System.out.println("I am on."); }

}

class Monitor{

private int inch;

private boolean isFlat;

//no information of computer

public Monitor(int inch, boolean isFlat){

this.inch=inch;

this.isFlat=isFlat;

}

}

class ComputerAdmin{

public static void main(String aa[]){

//Monitor aMonitor=new Monitor(15, true);

Computer aComputer=new Computer(586, 32.0, 15, true);

System.out.println("Computer is :"+aComputer);

aComputer.turnOn();

}

}

  通过以上的说明,相信大家对于Association、Aggregation和Composition三者之间的区别,会产生一个比较清晰的认识。如果你是刚刚接触面向对象编程的初学者,你会发现,这无论对于你工程前期程序类图的设计还是后期具体的代码实现,都是相当有帮助的。


转载自:http://it.cqnews.net/html/2013-12/25/content_29170755.htm

猜你喜欢

转载自blog.csdn.net/czc448969530/article/details/74923236
今日推荐