classes - intermediate

21
Classes - Intermediate Chapter 4

Upload: dusan

Post on 25-Feb-2016

34 views

Category:

Documents


2 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Classes - Intermediate

Classes - IntermediateChapter 4

Page 2: Classes - Intermediate

Chapter 4

4.0 Classes – Intermediate Method overloading Object as parameter Object as method type Array of Object Composite objects Application

Page 3: Classes - Intermediate

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).

Page 4: Classes - Intermediate

Composite Objects: Example 14

WHEELString brandString sizeString typeString warranty

CARString nameString colourString wheel

Page 5: Classes - Intermediate

Composite Objects: Example 25

DATEint dayint monthint year

STUDENTString nameString idint dob

Page 6: Classes - Intermediate

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; . . .

}

Page 7: Classes - Intermediate

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.

Page 8: Classes - Intermediate

Composite Objects: Example 38

Page 9: Classes - Intermediate

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.

Page 10: Classes - Intermediate

Composite Objects: Example 410

Page 11: Classes - Intermediate

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.

Page 12: Classes - Intermediate

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.

Page 13: Classes - Intermediate

Composite Objects: Example 513

Page 14: Classes - Intermediate

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.

Page 15: Classes - Intermediate

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.

Page 16: Classes - Intermediate

Composite Objects: Exercise 116

Write Java Program for the following UML diagram. Apply the composition concepts.

Put the contained class PersoanalInfo in a package named Personal.

Page 17: Classes - Intermediate

17

Page 18: Classes - Intermediate

18

Page 19: Classes - Intermediate

19

Page 20: Classes - Intermediate

Composite Objects: Application20

Page 21: Classes - Intermediate

End of Chapter 0421