关于relationship的疑惑:dependency和association以及aggregation有何区别?

kingdl 2003-12-03 03:09:57
四种relationship在Java代码上都有什么区别?
dependency,association,aggregation,combination
dependency是对对象的只读引用吗?
dependency和association在代码中有什么区别?
association和aggregation到底在代码中有什么区别?
combination是子对象在父对象的constructor中实例化的吗??
请看如下代码,并告诉我都是哪种relationship,谢谢!!
class B{
public int i=0;
}
class A{
private B b1;
private B b2;
private B b3;
private B b5 = new B();//association?
private int n=0;

public A(){
b1 = new B();//combination?
}
void addItem(B f){
n=f.i; //dependency?
}
void changeItem(B c){
B b4 = c;//association?
b4.i++;//c is changed
}
void setList(B e){
b2 = e.clone();//aggregation?
}
void initialize(){
b3 = new B();//what relationship?
}
}
...全文
506 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangjyr 2003-12-05
  • 打赏
  • 举报
回复
其实,完全不必在乎于他们在哪里创建,在建模的这一阶段,重要的是理清他们的逻辑关系,如果两个类的关系是结构上的,他们就属于association范畴,而那些在函数调用时引用到的临时变量,不管他们在哪里创建他们都只是一种依赖,就像我们可能使用工厂(建筑类)的“执行建筑职能”这一方法制造某样产品(商品类),我们不太可能认为商品与工厂在结构上有什么联系吧。
另外,aggragation其实本身就是一种association,只是多了一种“整体/部分”的语义而已,如果两个类满足association,而我们又从逻辑关系中判断他们有整体/部分的关系,那他们就是一种aggregation.
至于combination中提到的“同生”,并不是说他们同时产生(同时创建,时间点),而只是说他们同时存在,同时生存(时间段),所以作为“部分”的类并不一定要在“整体”的构造函数中创建的。
kingdl 2003-12-05
  • 打赏
  • 举报
回复
关于Denpendency和Association的区别,我同意你的看法。
另外,我想:
在类的任何部分,包括Attribute,method,construcotr拥有其他类的实例,都应该算是一种Association。
如果是在Method中实例化另外一个类,应该是Association。
如果在Constructor中实例化另外一个对象,应该是Combination。
如果是在Attribute的Defination时进行实例化,很可能还是一种Aggregation。不过这也不是绝对的。就代码本身而言,Association和Aggregation很难区分。
以上是我对代码的分析得出的结论,不知道你赞不赞同?
期待回答
kingdl 2003-12-05
  • 打赏
  • 举报
回复
现在除了A的构造函数中的那个问题,我基本弄明白了,感谢zhangjyr(Tianium) ,感谢你的热心和关注,佩服你的执著,交个朋友吧,kingdl@263.net。以后一起学习,一起讨论
kingdl 2003-12-05
  • 打赏
  • 举报
回复
楼上,这次你说的我比较赞同
不过在A的构造函数中对B进行实例化应该是combination吧,这样才能同生同死吗!
zhangjyr 2003-12-05
  • 打赏
  • 举报
回复
需要说明的是dependency强调的是一种使用关系。而association是一种结构关系,即只要你将其他类的对象作为你的类的成员,他们就构成了association。aggregation是association的特例,他描述了一种“整体/部分”的结构关系,其语义同association。而combination又是aggregation的变体,他强调整体与部分生命周期的一致,作为部分的成员可以在整体之后创建,但一旦创建,他们就同生共死了。
zhangjyr 2003-12-05
  • 打赏
  • 举报
回复
先前我的确有些东西搞混了,回去查了下书,以下列出示例代码
class D;
class C;
class B{
public int i=0;
}
class A{
private C []c = new C[5];
private D []d;
private B b1;
private B b2;
private B b3;
private B b5 = new B(); //association
private int n=0;

public A(){
b1 = new B(); //association
}
void addItem(B f){
n=f.i; //dependency
}
void changeItem(B c){
B b4 = c; //dependency
b4.i++; //c is changed
}
void setList(B e){
b2 = e.clone();//association
}
void initialize(){
b3 = new B(); //association
for (int i = 0; i < c.length; i++){
c[i] = new C;
} //combination
}
void setArray(D []d1){
d = d1 //aggregation
}
}
kingdl 2003-12-05
  • 打赏
  • 举报
回复
结帖
kingdl 2003-12-05
  • 打赏
  • 举报
回复
好的,谢谢你,非常感谢,交个朋友吧。kingdl@263.net
zhangjyr 2003-12-05
  • 打赏
  • 举报
回复
我没有用rose生成过java代码,但的确生成过C++的代码,rose生成的代码似乎体现不出dependency,因为本来dependency关系当你在函数调用时显式写出参数类型时是可以忽略的,至于association,rose会很确实的为你在该类中生成一个成员变量(不是指针)。
另外在rose里画图时你会发现当你将聚合的那端从by reference改作by value时聚合标志变成了组合标志,这从一个侧面说明了rose是如何看待aggregation和combination的,我不记得rose生成代码时将他们怎么处理,但估计combination会生成数组成员对象。aggregation就不好说了。:)
如果你有rose的话,自己生成一下就什么都清楚了。
kingdl 2003-12-05
  • 打赏
  • 举报
回复
有道理,这次的解释非常清晰,很满意。
不过这样的话,UML模型产生的代码或者是反向工程,都不能很好得结合Java和UML了,现实就是这样的吗?
zhangjyr 2003-12-04
  • 打赏
  • 举报
回复
四中关系是针对整个类来说的,像你这样定义 private B b5 = new B();//aggregation?根本谈不上aggregation或combination,因为单从这一句更本看不出一对多的关系,我现在不多说了,等我回家查一下书再看看能不能给你比较能说明问题的示例代码吧。
kingdl 2003-12-04
  • 打赏
  • 举报
回复
楼上,请问错在哪里呀?
书上没有代码的例子,所以比较困惑,尤其是对于在method中实例化对象到底是哪种关系实在搞不清楚,请指教。
eagle19790214 2003-12-04
  • 打赏
  • 举报
回复
大都搞错了,建议看看rup的书,有详细介绍
kingdl 2003-12-04
  • 打赏
  • 举报
回复
楼上,我不太同意你的观点。
1.根据书上的定义,dependency是get,use then forget所依赖的对象,所以我认为dependency应该是只读引用,不知道对不对。
2.在dependency关系中,A不应负责B的创建。如果A负责B的创建,那么应该是一种association,aggregation or combination。
3.setList()中,b2确实是由A创建的,但是只有setList()被调用的时候,b2才被实例化,所以我不认为是combination,而是一种association.
4.我对我的代码的关系有新的看法,现更改如下:
class B{
public int i=0;
}
class A{
private B b1;
private B b2;
private B b3;
private B b5 = new B();//aggregation?
private int n=0;

public A(){
b1 = new B();//combination?
}
void addItem(B f){
n=f.i; //dependency?
}
void changeItem(B c){
B b4 = c;//association?
b4.i++;//c is changed
}
void setList(B e){
b2 = e.clone();//association?
}
void initialize(){
b3 = new B();//what relationship?maybe association?
}
}
我的代码显示了我所能想到的各种A和B的关系,如果还有别的关系,请帮我补上。
再一次感谢你的热心和关注,让我们共同期待高手的完整答案吧。
zhangjyr 2003-12-04
  • 打赏
  • 举报
回复
呵呵,我手头没书,可能是弄错了。不过应该说,Aggregation和Comination是association在1对多(逻辑上)的情况下的特例,就你的代码,当类A负责类B对象的创建(new)时(在构造函数中,成员函数中或成员定义时),他们应该是一种依赖(dependency),而相应的,当A不负责B对象的创建时(如类B对象作为参数传入),他们应该是关联吧(association)。
从你的代码中,我们还看到类中有多个类B的对象,并且他们的实例都是由类A,负责创建的(特别的,在setList中,clone函数的调用实际上使得了A也负责对b2的创建)所以,在这里两个类从结构上最符合组合(Combination,前面是我弄错了)关系。
kingdl 2003-12-04
  • 打赏
  • 举报
回复
是呀,是呀。好像单从代码很难分清association和aggregation
kingdl 2003-12-03
  • 打赏
  • 举报
回复
有没有高手给一个比较完整的答案,尤其是针对我给出的代码,指出到底是哪种关系。或者给出示例代码,讲解一下,不胜感激。
kingdl 2003-12-03
  • 打赏
  • 举报
回复
楼上,不好意思,指出你一点错误。
首先,这四种关系是UML中的定义的,不是RUP中的。RUP是Rational Unified Process,是一种Process,和这四种关系基本没有什么关系:)
还有,你弄反了,Combination是同生共死的,是书和书页的关系。Aggregation相当于书架和书的关系。
不过感谢你的热心,谢谢!让我们一起看看其他高手的答案吧!:)
zhangjyr 2003-12-03
  • 打赏
  • 举报
回复
对于RUP中的四种关系,我是这样理解的:只要两个类中存在联系,他们就满足association,
dependency是对其他类的单向依赖,即一旦你的类中引用了其他类(如包含某个类的对象),则我们就可以说他依赖于那个类
至于aggregation和combination,首先他们意味着1对多的关系,aggregation是combination的强化,满足aggregation关系的两个类一般意味着他们将同生共死,结合到代码,如果某个类中有指向另一个类的对象的集合的引用(c++中的指针),那么我们认为他更接近于combination,因为两者是分别构造的。
而如果某类将另一个类的对象的集合作为自己的成员,那么我们认为他更接近于aggregation,因为两者是同时被构造的。释放前者将导致后者的不可用。

写了这么多,不知道有没有表达清楚。
Database System Concepts——数据库系统概念第六版(英文版) 作者: Abraham Silberschatz (Yale University) Henry F. Korth (Lehigh University) S. Sudarshan (Indian Institute of Technology, Bombay) 本书目录: Chapter 1 Introduction 1.1 Database-System Applications 1 1.2 Purpose of Database Systems 3 1.3 View of Data 6 1.4 Database Languages 9 1.5 Relational Databases 12 1.6 Database Design 15 1.7 Data Storage and Querying 20 1.8 Transaction Management 22 1.9 Database Architecture 23 1.10 Data Mining and Information Retrieval 25 1.11 Specialty Databases 26 1.12 Database Users and Administrators 27 1.13 History of Database Systems 29 1.14 Summary 31 Exercises 33 Bibliographical Notes 35 Chapter 2 Introduction to the RelationalModel 2.1 Structure of Relational Databases 39 2.2 Database Schema 42 2.3 Keys 45 2.4 Schema Diagrams 46 2.5 Relational Query Languages 47 2.6 Relational Operations 48 2.7 Summary 52 Exercises 53 Bibliographical Notes 55 Chapter 3 Introduction to SQL 3.1 Overview of the SQL Query Language 57 3.2 SQL Data Definition 58 3.3 Basic Structure of SQL Queries 63 3.4 Additional Basic Operations 74 3.5 Set Operations 79 3.6 Null Values 83 3.7 Aggregate Functions 84 3.8 Nested Subqueries 90 3.9 Modification of the Database 98 3.10 Summary 104 Exercises 105 Bibliographical Notes 112 Chapter 4 Intermediate SQL 4.1 Join Expressions 113 4.2 Views 120 4.3 Transactions 127 4.4 Integrity Constraints 128 4.5 SQL Data Types and Schemas 136 4.6 Authorization 143 4.7 Summary 150 Exercises 152 Bibliographical Notes 156 Chapter 5 Advanced SQL 5.1 Accessing SQL From a Programming Language 157 5.2 Functions and Procedures 173 5.3 Triggers 180 5.4 Recursive Queries 187 5.5 Advanced Aggregation Features 192 5.6 OLAP 197 5.7 Summary 209 Exercises 211 Bibliographical Notes 216 Chapter 6 Formal Relational Query Languages 6.1 The Relational Algebra 217 6.2 The Tuple Relational Calculus 239 6.3 The Domain Relational Calculus 245 6.4 Summary 248 Exercises 249 Bibliographical Notes 254 Chapter 7 Database Design and the E-R Model 7.1 Overview of the Design Process 259 7.2 The Entity-Relationship Model 262 7.3 Constraints 269 7.4 Removing Redundant Attributes in Entity Sets 272 7.5 Entity-Relationship Diagrams 274 7.6 Reduction to Relational Schemas 283 7.7 Entity-Relationship Design Issues 290 7.8 Extended E-R Features 295 7.9 Alternative Notations for Modeling Data 304 7.10 Other Aspects of Database Design 310 7.11 Summary 313 Exercises 315 Bibliographical Notes 321 Chapter 8 Relational Database Design 8.1 Features of Good Relational Designs 323 8.2 Atomic Domains and First Normal Form 327 8.3 Decomposition Using Functional Dependencies 329 8.4 Functional-Dependency Theory 338 8.5 Algorithms for Decomposition 348 8.6 Decomposition Using Multivalued Dependencies 355 8.7 More Normal Forms 360 8.8 Database-Design Process 361 8.9 Modeling Temporal Data 364 8.10 Summary 367 Exercises 368 Bibliographical Notes 374 Chapter 9 Application Design and Development 9.1 Application Programs and User Interfaces 375 9.2 Web Fundamentals 377 9.3 Servlets and JSP 383 9.4 Application Architectures 391 9.5 Rapid Application Development 396 9.6 Application Performance 400 9.7 Application Security 402 9.8 Encryption and Its Applications 411 9.9 Summary 417 Exercises 419 Bibliographical Notes 426 Chapter 10 Storage and File Structure 10.1 Overview of Physical Storage Media 429 10.2 Magnetic Disk and Flash Storage 432 10.3 RAID 441 10.4 Tertiary Storage 449 10.5 File Organization 451 10.6 Organization of Records in Files 457 10.7 Data-Dictionary Storage 462 10.8 Database Buffer 464 10.9 Summary 468 Exercises 470 Bibliographical Notes 473 Chapter 11 Indexing and Hashing 11.1 Basic Concepts 475 11.2 Ordered Indices 476 11.3 B+-Tree Index Files 485 11.4 B+-Tree Extensions 500 11.5 Multiple-Key Access 506 11.6 Static Hashing 509 11.7 Dynamic Hashing 515 11.8 Comparison of Ordered Indexing and Hashing 523 11.9 Bitmap Indices 524 11.10 Index Definition in SQL 528 11.11 Summary 529 Exercises 532 Bibliographical Notes 536 Chapter 12 Query Processing 12.1 Overview 537 12.2 Measures of Query Cost 540 12.3 Selection Operation 541 12.4 Sorting 546 12.5 Join Operation 549 12.6 Other Operations 563 12.7 Evaluation of Expressions 567 12.8 Summary 572 Exercises 574 Bibliographical Notes 577 Chapter 13 Query Optimization 13.1 Overview 579 13.2 Transformation of Relational Expressions 582 13.3 Estimating Statistics of Expression Results 590 13.4 Choice of Evaluation Plans 598 13.5 Materialized Views 607 13.6 Advanced Topics in Query Optimization 612 13.7 Summary 615 Exercises 617 Bibliographical Notes 622 Chapter 14 Transactions 14.1 Transaction Concept 627 14.2 A Simple Transaction Model 629 14.3 Storage Structure 632 14.4 Transaction Atomicity and Durability 633 14.5 Transaction Isolation 635 14.6 Serializability 641 14.7 Transaction Isolation and Atomicity 646 14.8 Transaction Isolation Levels 648 14.9 Implementation of Isolation Levels 650 14.10 Transactions as SQL Statements 653 14.11 Summary 655 Exercises 657 Bibliographical Notes 660 Chapter 15 Concurrency Control 15.1 Lock-Based Protocols 661 15.2 Deadlock Handling 674 15.3 Multiple Granularity 679 15.4 Timestamp-Based Protocols 682 15.5 Validation-Based Protocols 686 15.6 Multiversion Schemes 689 15.7 Snapshot Isolation 692 15.8 Insert Operations, Delete Operations, and Predicate Reads 697 15.9 Weak Levels of Consistency in Practice 701 15.10 Concurrency in Index Structures 704 15.11 Summary 708 Exercises 712 Bibliographical Notes 718 Chapter 16 Recovery System 16.1 Failure Classification 721 16.2 Storage 722 16.3 Recovery and Atomicity 726 16.4 Recovery Algorithm 735 16.5 Buffer Management 738 16.6 Failure with Loss of Nonvolatile Storage 743 16.7 Early Lock Release and Logical Undo Operations 744 16.8 ARIES 750 16.9 Remote Backup Systems 756 16.10 Summary 759 Exercises 762 Bibliographical Notes 766 Chapter 17 Database-System Architectures 17.1 Centralized and Client–Server Architectures 769 17.2 Server System Architectures 772 17.3 Parallel Systems 777 17.4 Distributed Systems 784 17.5 Network Types 788 17.6 Summary 791 Exercises 793 Bibliographical Notes 794 Chapter 18 Parallel Databases 18.1 Introduction 797 18.2 I/O Parallelism 798 18.3 Interquery Parallelism 802 18.4 Intraquery Parallelism 803 18.5 Intraoperation Parallelism 804 18.6 Interoperation Parallelism 813 18.7 Query Optimization 814 18.8 Design of Parallel Systems 815 18.9 Parallelism on Multicore Processors 817 18.10 Summary 819 Exercises 821 Bibliographical Notes 824 Chapter 19 Distributed Databases 19.1 Homogeneous and Heterogeneous Databases 825 19.2 Distributed Data Storage 826 19.3 Distributed Transactions 830 19.4 Commit Protocols 832 19.5 Concurrency Control in Distributed Databases 839 19.6 Availability 847 19.7 Distributed Query Processing 854 19.8 Heterogeneous Distributed Databases 857 19.9 Cloud-Based Databases 861 19.10 Directory Systems 870 19.11 Summary 875 Exercises 879 Bibliographical Notes 883 Chapter 20 DataWarehousing andMining 20.1 Decision-Support Systems 887 20.2 DataWarehousing 889 20.3 Data Mining 893 20.4 Classification 894 20.5 Association Rules 904 20.6 Other Types of Associations 906 20.7 Clustering 907 20.8 Other Forms of Data Mining 908 20.9 Summary 909 Exercises 911 Bibliographical Notes 914 Chapter 21 Information Retrieval 21.1 Overview 915 21.2 Relevance Ranking Using Terms 917 21.3 Relevance Using Hyperlinks 920 21.4 Synonyms, Homonyms, and Ontologies 925 21.5 Indexing of Documents 927 21.6 Measuring Retrieval Effectiveness 929 21.7 Crawling and Indexing the Web 930 21.8 Information Retrieval: Beyond Ranking of Pages 931 21.9 Directories and Categories 935 21.10 Summary 937 Exercises 939 Bibliographical Notes 941 Chapter 22 Object-Based Databases 22.1 Overview 945 22.2 Complex Data Types 946 22.3 Structured Types and Inheritance in SQL 949 22.4 Table Inheritance 954 22.5 Array and Multiset Types in SQL 956 22.6 Object-Identity and Reference Types in SQL 961 22.7 Implementing O-R Features 963 22.8 Persistent Programming Languages 964 22.9 Object-Relational Mapping 973 22.10 Object-Oriented versus Object-Relational 973 22.11 Summary 975 Exercises 976 Bibliographical Notes 980 Chapter 23 XML 23.1 Motivation 981 23.2 Structure of XML Data 986 23.3 XML Document Schema 990 23.4 Querying and Transformation 998 23.5 Application Program Interfaces to XML 1008 23.6 Storage of XML Data 1009 23.7 XML Applications 1016 23.8 Summary 1019 Exercises 1021 Bibliographical Notes 1024 PART EIGHT ADVANCED TOPICS Chapter 24 Advanced Application Development 24.1 Performance Tuning 1029 24.2 Performance Benchmarks 1045 24.3 Other Issues in Application Development 1048 24.4 Standardization 1051 24.5 Summary 1056 Exercises 1057 Bibliographical Notes 1059 Chapter 25 Spatial and Temporal Data andMobility 25.1 Motivation 1061 25.2 Time in Databases 1062 25.3 Spatial and Geographic Data 1064 25.4 Multimedia Databases 1076 25.5 Mobility and Personal Databases 1079 25.6 Summary 1085 Exercises 1087 Bibliographical Notes 1089 Chapter 26 Advanced Transaction Processing 26.1 Transaction-Processing Monitors 1091 26.2 TransactionalWorkflows 1096 26.3 E-Commerce 1102 26.4 Main-Memory Databases 1105 26.5 Real-Time Transaction Systems 1108 26.6 Long-Duration Transactions 1109 26.7 Summary 1115 Exercises 1117 Bibliographical Notes 1119 Chapter 27 PostgreSQL 27.1 Introduction 1123 27.2 User Interfaces 1124 27.3 SQL Variations and Extensions 1126 27.4 Transaction Management in PostgreSQL 1137 27.5 Storage and Indexing 1146 27.6 Query Processing and Optimization 1151 27.7 System Architecture 1154 Bibliographical Notes 1155 Chapter 28 Oracle 28.1 Database Design and Querying Tools 1157 28.2 SQL Variations and Extensions 1158 28.3 Storage and Indexing 1162 28.4 Query Processing and Optimization 1172 28.5 Concurrency Control and Recovery 1180 28.6 System Architecture 1183 28.7 Replication, Distribution, and External Data 1188 28.8 Database Administration Tools 1189 28.9 Data Mining 1191 Bibliographical Notes 1191 Chapter 29 IBM DB2 Universal Database 29.1 Overview 1193 29.2 Database-Design Tools 1194 29.3 SQL Variations and Extensions 1195 29.4 Storage and Indexing 1200 29.5 Multidimensional Clustering 1203 29.6 Query Processing and Optimization 1207 29.7 Materialized Query Tables 1212 29.8 Autonomic Features in DB2 1214 29.9 Tools and Utilities 1215 29.10 Concurrency Control and Recovery 1217 29.11 System Architecture 1219 29.12 Replication, Distribution, and External Data 1220 29.13 Business Intelligence Features 1221 Bibliographical Notes 1222 Chapter 30 Microsoft SQL Server 30.1 Management, Design, and Querying Tools 1223 30.2 SQL Variations and Extensions 1228 30.3 Storage and Indexing 1233 30.4 Query Processing and Optimization 1236 30.5 Concurrency and Recovery 1241 30.6 System Architecture 1246 30.7 Data Access 1248 30.8 Distributed Heterogeneous Query Processing 1250 30.9 Replication 1251 30.10 Server Programming in .NET 1253 30.11 XML Support 1258 30.12 SQL Server Service Broker 1261 30.13 Business Intelligence 1263 Bibliographical Notes 1267

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧