Download WholePartPattern

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Software Design Laboratory
Dept of MCA, AMCEC
10. Whole Part Pattern (Structural Decomposition)
Intent: The Whole Part design pattern helps with the aggregation of components that
together form a semantic unit. An aggregation component, the Whole, encapsulates its
constituent components, the parts, organizes their collaboration, and provides a common
interface to its functionality. Direct access to the parts is not possible.
Structure:
Participant Classes:
Client: The task of a client is to access only the services performed by Whole.
Whole: The responsibility of the Whole is to aggregate several smaller objects;
Provides services built on top of part object; Acts as a wrapper around its constituent parts.
Part: Represents a particular object and its services
Example: Creation of the list of professors working in a University
Pattern Instance
Client: Client
Whole: University
Part: Professor
Use Case Diagram:
System
<<include>>
doTask
Client
University
Professor
Software Design Laboratory
Dept of MCA, AMCEC
Sequence Diagram:
: University
: Professor
: Client
1 : doTask()
2 : addUniversity()
sd Add Professors3 : addProfessorList()
4 : addProfessor()
sd showProfessors
5 : showProfessorList()
6 : getProfessor
7 : displayProfessorList
Class Diagram:
WholePart
University
-uName: String
-profList: ArrayList<Professor>
Client
+doTask()
+main()
1
1
+setUName(String): void
+getUName(): String
+addUniversity(String): void
+addProfList(int, String): void
+showProfList(): void
Professor
1
1..*
-profId: int
-profName: String
+setProfId(int): void
+getProfId(): int
+setProfName(String): void
+getProfName(): String
+addProfessor(int, String): void
Software Design Laboratory
Dept of MCA, AMCEC
Collaboration Diagram:
1 : doTask()
3 : addProfessorList()
2 : addUniversity()
: University
4 : addProfessor()
5 : showProfessorList()
: Client
6 : getProfessor
7 : displayProfessorList
Activity Diagram:
Add University
Add Professor List
Option = 1
Option=2
Create Professor List
Option=3
Add Professor
Show Professor List
Show Professor List
Get Professor
Java Code:
//Client.java
package WholePart;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Client {
public static void main(String[] args)throws IOException {
Client client =new Client();
client.doTask();
}
public void doTask()throws IOException {
: Professor
Software Design Laboratory
Dept of MCA, AMCEC
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter the name of the university: ");
String univ=br.readLine();
University newUniv=new University();
newUniv.addUniversity(univ);
int opt=0;
do{
System.out.println("\t\tMenu\n\t(1)--Add Professor
\n\t(2)--Show Professor List \n\t(3)--Exit\n");
System.out.print("Enter the option: ");
opt=Integer.parseInt(br.readLine());
switch(opt){
case 1:
System.out.print("Enter the professor id: ");
int id=Integer.parseInt(br.readLine());
System.out.print("Enter the professor name: ");
String name=br.readLine();
newUniv.addProfList(id, name);
break;
case 2:
newUniv.showProfList();
break;
case 3:
break;
default:System.out.print("Invalid option.");
}
}while(opt!=3);
}
}
//University.java
package WholePart;
import java.util.ArrayList;
public class University {
private String uName;
private ArrayList<Professor> profList=new ArrayList<Professor>();
public void setUName(String univ) {
uName=univ;
}
public String getUName() {
return uName;
}
Software Design Laboratory
Dept of MCA, AMCEC
public void addUniversity(String name){
setUName(name);
}
public void addProfList(int id, String name){
Professor newProf = new Professor();
newProf.addProfessor(id,name);
profList.add(newProf);
}
public void showProfList(){
if(profList.size()==0)
System.out.println("Sorry list is empty");
else{
System.out.println("Professorlist in - "+this.getUName());
for(Professor prof:profList)
System.out.println("Professor Id is:" +
prof.getProfId()+" Professor name is: "+prof.getProfName());
}
}
}
//Professor.java
package WholePart;
public class Professor {
private int profId;
private String profName;
public int getProfId() {
return profId;
}
public void setProfId(int profId) {
this.profId = profId;
}
public String getProfName() {
return profName;
}
public void setProfName(String profName) {
this.profName = profName;
}
public void addProfessor(int id,String name){
setProfId(id);
setProfName(name);
}
}
Output:
Enter the name of the university: VTU
Menu
(1)--Add Professor
(2)--Show Professor List
(3)--Exit
Related documents