190 likes | 329 Views
Classes - Intermediate. Chapter 4. Chapter 4. 4.0 Classes – Intermediate Method overloading Object as parameter Object as method type Array of Object Composite objects Application. Composite Objects. Composite objects can be define as: Objects that contain other objects or
E N D
Classes - Intermediate Chapter 4
Chapter 4 • 4.0 Classes – Intermediate • Method overloading • Object as parameter • Object as method type • Array of Object • Composite objects • Application
Composite Objects • 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 2 Public class Student{ String name; int id; int dob; public Student(){ name = “”; id=0; dob = new Date(); } } Public class Date{ int day; int month; int year; . . . }
Composite Objects: Example 3 • 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 4 • 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 5 • 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 5 • 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: Applicability 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: Consequences 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 1 • Write Java Program for the following diagram. Apply the composition concepts. • Put the contained class PersoanalInfoin a package named Personal.