1 / 21

Classes - Intermediate

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

dusan
Télécharger la présentation

Classes - Intermediate

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes - Intermediate Chapter 4

  2. Chapter 4 • 4.0 Classes – Intermediate • Method overloading • Object as parameter • Object as method type • Array of Object • Composite objects • Application

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

  4. Composite Objects: Example 1

  5. Composite Objects: Example 2

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

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

  8. Composite Objects: Example 3

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

  10. Composite Objects: Example 4

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

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

  13. Composite Objects: Example 5

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

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

  16. Composite Objects: Exercise 1 • Write Java Program for the following UML diagram. Apply the composition concepts. • Put the contained class PersoanalInfo in a package named Personal.

  17. Composite Objects: Application

  18. End of Chapter 04

More Related