Post on 23-Feb-2016
description
Classes - IntermediateChapter 4
Chapter 4
4.0 Classes – Intermediate Method overloading Object as parameter Object as method type Array of Object Composite objects Application
Composite Objects3
Composite objects can be define as: Objects that contain other objects or Class that contain other class e.g: a drawing may be composed of graphic primitives,
such as lines, circles, rectangles, text, and so on. With composition, references to the constituent objects
become fields of the containing object. Composited (composed) objects are often referred to as
having a "has a" relationship. e.g: an object of a composite type (e.g. car) "has an"
object of a simpler type (e.g. wheel).
Composite Objects: Example 14
WHEELString brandString sizeString typeString warranty
CARString nameString colourString wheel
Composite Objects: Example 25
DATEint dayint monthint year
STUDENTString nameString idint dob
Composite Objects: Example 2
Public class Student{
String name; int id; int dob; public Student(){ name = “”; id=0; dob = new Date(); }
}
6
Public class Date{
int day; int month; int year; . . .
}
Composite Objects: Example 37
In graphics editors a shape can be basic or complex. An example of a simple shape is a line, where a complex shape is a rectangle which is made of four line objects. Since shapes have many operations in common such as rendering the shape to screen, and since shapes follow a part-whole hierarchy, composite pattern can be used to enable the program to deal with all shapes uniformly.
Composite Objects: Example 38
Composite Objects: Example 49
Many types of manufactured systems, such as computer systems and stereo systems, are composed of individual components and sub-systems that contain components. For example, a computer system can have various chassis that contain components (hard-drive chassis, power-supply chassis) and busses that contain cards. The entire system is composed of individual components (floppy drives, cd-rom drives), busses and chassis.
Composite Objects: Example 410
Composite Objects: Example 511
A coffee cup object of your program could contain coffee. Coffee itself could be a distinct class, which your program
could instantiate. You would award coffee with a type if it exhibits behavior that
is important to your solution. Perhaps it will swirl one way or another when stirred, keep
track of a temperature that changes over time, or keep track of the proportions of coffee and any additives such as cream and sugar.
Composite Objects: Example 512
In the case of Cup and CoffeeCup, a "CoffeeCup is-a Cup.” A CoffeeCup is a more specific kind of Cup. A CoffeeMug is a more specific kind of CoffeeCup. For instance, a CoffeeMug is not only more specific version of
a CoffeeCup, it is also a more specific version of a Cup. Therefore, the is-a relationship exists between CoffeeMug
and Cup: a CoffeeMug is-a Cup.
Composite Objects: Example 513
Composite Objects: Applicability14
Use the Composite pattern when: you want to represent part-whole hierarchies of objects. you want clients to be able to ignore the difference between
compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.
Composite Objects: Consequences15
Benefits It makes it easy to add new kinds of components. It makes clients simpler, since they do not have to know if
they are dealing with a leaf or a composite component.
Liabilities It makes it harder to restrict the type of components of a
composite.
Composite Objects: Exercise 116
Write Java Program for the following diagram. Apply the composition concepts.
Put the contained class PersoanalInfo in a package named Personal.
17
18
19