If I have this class
import java.util.Scanner;
public class SimpleCalc {
private int x;
private int y;
private Scanner scan;
public SimpleCalc() {
scan = new Scanner(System.in);
System.out.print("Please Enter The First Number: ");
this.x = scan.nextInt();
System.out.print("Please Enter The Second Number: ");
this.y = scan.nextInt();
}
}
This class uses composition concept because it has "scan" object from another class "Scanner".
But what if I declared the "scan" object inside a method like this:
public SimpleCalc() {
Scanner scan = new Scanner(System.in);
System.out.print("Please Enter The First Number: ");
this.x = scan.nextInt();
System.out.print("Please Enter The Second Number: ");
this.y = scan.nextInt();
}
Does that considered composition concept?
In another way: does composition concept applies to just classes or also applies to methods?
If your question is, in the context of design pattern and specifically related to Composite Design Patter, then the short answer is NO.
As GOF has described:
"Compose objects into tree structure to represent part-whole hierarchies.Composite lets client treat individual objects and compositions of objects uniformly".
So this doesn't apply for methods.