Anda di halaman 1dari 33

Class%Rela(onships%

SZABIST%Islamabad%
Object%Oriented%Programming%
Associa(on%
•  An%associa(on%is%a%rela(onship%between%two%
classes%
–  Describe%how%instance%of%one%class%refer%to%
instance%of%another%class%
–  Indicated%in%UML%by%connec(ng%two%classes%with%a%
line%(op(onally%labeled)%
Mul(plici(es%
•  Associa(ons%should%indicate%their%
mul(plici(es%%
•  Symbol%near%a%class%B%indicates%“this%many%
instances%of%B”%involved%in%rela(onship%%
Mul(plici(es%II%
•  The%symbol%*%means%“any%number%including%0”%%
–  Each%employee%works%for%exactly%1%company%%
–  Each%company%has%0%or%more%employees%%
Ranges%
•  The%symbol%..%is%used%to%indicate%ranges%%
–  Managers%may%have%0,%1%or%more%PAs%%
–  Each%PA%must%work%for%at%least%one%manager,%but%
maybe%more%%
Java%Example%
•  In%Java,%an%associa(on%is%reflected%by%one%class%
having%variables%referring%to%other%classes%%
%

public class Company {


private String companyName;
private Employee[] staff;

}
%
An%Associa(on%
•  A%MobilePhoneGUI%displays%exactly%one%
MobilePhone%
•  A%MobilePhone%may%(or%may%not)%be%displayed%
by%a%MobilePhoneGUI%%
Aggrega(on%&%Composi(on%
•  Special%forms%of%associa(on%reflec(ng%the%“is%
composed%of”%or%“is%built%from”%rela(onship%%
%
Difference%
•  In%aggrega(on%the%“parts”%have%an%
independent%existence%and%can%be%shared%or%
reassigned%between%aggregates%%
•  In%composi(on,%the%parts%BELONG%to%exactly%
one%aggregate,%are%created%when%the%
aggregate%is%created%and%destroyed%when%the%
aggregate%is%destroyed%%
Java%Example%
•  Consider%a%class%to%represent%a%cylinder%%
–  A%circular%base%and%a%height%%
Circle%class%
public class Circle {
private double radius;
public Circle(double r) {
}
public double area() {
return (Math.PI * radius * radius);
}
}
%
Cylinder%“has%a”%Circle..%%
public class Cylinder {
private Circle base;
private double height;
// constructor with two arguments public
Cylinder(double r, double h) {
%base%=%new%Circle(r);%%
%height%=%h;%
}
%
..%and%uses%its%methods%%
// method for computing volume of cylinder
public double volume() {
// call the area() method
return (base.area() * height);
}
}
%
Construc(ng%aggregate%objects%%
•  If%a%class%A%has%instance%variables%referring%to%
another%class%B,%then%when%and%how%do%the%
objects%of%class%B%get%constructed?%%
–  Internally%\%The%constructor%for%A%constructs%all%
required%instances%of%B%%
–  Externally%\%The%client%passes%instances%of%B%via%
constructor%arguments%%
Internal%Construc(on%%
•  Usual%model%for%the%“filled%diamond”%type%of%
composi(on%%
–  Cylinder%calls%constructor%for%Circle%%
•  In%Java%there%is%no%need%for%the%“parts”%to%be%
destroyed%explicitly%as%this%happens%
automa(cally%%
External%Construc(on%%
%
public%class%MobilePhoneGUI%{%%
%private%MobilePhone%myPhone;%%
%//%Lots%of%GUI%stuff%%
}%
%

•  How%is%myPhone%set?%%
External%Construc(on%
class%Cylinder{%
%
private%Circle%base;%
private%double%height;%
%
Cylinder(Circle%ref,%double%h){%
base%=%ref;% class%Main{%
height%=%h;% %public%sta(c%void%main(String%args[])
%Circle%cc%=%new%Circle(3);%
}}% %Cylinder%c%=%%new%Cylinder(cc,10);%
% %System.out.println(%c.volume());%
%}%
}%
External%Construc(on%II%
//%Constructor%%
public%MobilePhoneGUI%(MobilePhone%mp)%{%%
myPhone%=%mp;%%
}%%
%
•  Recall%
–  This%saves%the%value%of%the%parameter%variable%mp%into%
the%instance%variable%myPhone%%
–  There%must%be%an%already\created%instance%of%
MobilePhone%available%when%the%MobilePhoneGUI%is%
constructed%%
Mutual%Knowledge%
•  How%do%we%ensure%that%two%objects%each%
know%about%the%other?%%
•  This%is%a%par(cular%issue%for%GUI%design%%
–  The%GUI%object%constructs%UI%components%such%as%
bugons%%
–  Users%click%bugons%%
–  GUI%object%needs%to%respond%and%so%the%bugon%
needs%to%know%about%the%GUI%object%%
Listeners%
•  The%standard%mechanism%for%this%is%the%use%of%
listeners%\%each%UI%component%keeps%a%list%of%
“who%is%interested%in%me”%%
%
%
% % %JBugon%close%=%new%JBugon(“C”);% % %
% % %closeBugon.addMouseListener(this);%%
%
Listeners%II%%
%
%
GUI%object%knows%about%the%JBugon%because%it%
created%the%bugon,%and%then%passes%the%bugon%
a%reference%to%itself!%Now%the%bugon%knows%who%
created%it%\%more%importantly%it%knows%who%is%
interested%in%mouse%events!%%
Events%%
•  A%MouseEvent%occurs%whenever%the%user%does%
something%mouse\related%with%a%component,%
for%example%clicks%the%bugon%%
•  When%this%happens:%%
–  The%bugon%no(fies%all%of%its%registered%listeners%
(%in%this%case%it%no(fies%the%GUI%object%that%it%has%
been%clicked%)%
%
%
Aggrega(on/Composi(on%
public%class%Order%%
{%
%
%private%ArrayList<Product>%orders%=%new%
ArrayList<Product>();%
%
%public%void%add(Product%p)%
%{%
%%orders.add(p);%
%}%
}%
%
Aggrega(on/Composi(on%
public%class%Client%extends%Person{%
%%%%String%address%=%"";%
%
%%%%Orders%orders%=%new%Orders();%
%
%%%%public%Client(String%n,%String%sn){%
%%%%%name%=%n;%
%%%%%surName%=%sn;%
%%%%}%
%
%%%%public%String%getAddress(){%
%%%%%return%adress;%
%%%%}%
%%%%public%Orders%getOrders(){%
%%%%%return%this.orders;%%
%%%%}%
}%
Delega(on%
•  It%is%a%technique%where%an%object%expresses%
certain%behavior%to%the%outside%but%in%reality%
delegates%responsibility%for%implemen(ng%that%
behavior%to%an%associated%object.%%
•  Delega(on%emulates%Mul(ple%Inheritance%(MI)%
in%Java%since%Java%was%denied%to%have%MI%from%
the%very%beginning.%
Delega(on%
Code%Example%
public'class'A'{' public'class'B'{'
%public'void'all(){' %public'void'call(){'
% %
%System.out.println("a");/ %System.out.println("B");/
%}% %}%
}% }%

public'class'C'extends'A{'//is'a'
%private%B%b;%//%has%a%
'public'C(){'
% %b%=%new'B();''
%}%
%public'void'call(){'
% %b.call();%
%}%
}%

Write%the%names%of%the%methods%in%the%class%C%?%
Java%Event%Model%
Delega(on%Event%Model%
•  Means%the%use%of%event%listeners%in%the%event%
handling%
•  The%processing%of%an%event%is%delegated%to%a%
par(cular%object%in%the%program%
•  Using%a%separate%class%to%define%an%event%
listener%is%a%common%prac(ce%for%separa(ng%
the%GUI%interface%from%the%implementa(on%of%
its%event%handler%%
Lets%Revisit%Payroll%Calcula(on%
(Polymorphism)%
What%if%
•  An%Hourly%Employee%becomes%a%salaried%one!%

•  Can%we%solve%this%using%only%polymorphism?%
How%Delega(on%solves%this!%

I
Main%
PayStrategy%commission%=%new'
CommissionStrategy(10000,.06);'
% %PayStrategy%hourly%=%new'HourlyStrategy(16.75,40);'
% %%
% %Employee%joe%=%new'Employee();'
% %joe.setPayStrategy(commission);%
% %%
% %joe.setPayStrategy(hourly);%
% %%
% %System.out.println(joe.calculatePay());%

Anda mungkin juga menyukai