260 likes | 283 Views
Dasar Obyek. Oleh : Cosmas Haryawan -- Pengenalan UML – Base on : Sams Teach Yourself UML in 24 Hours, Third Edition. Class ?.
E N D
Dasar Obyek Oleh : Cosmas Haryawan -- Pengenalan UML – Base on : Sams Teach Yourself UML in 24 Hours, Third Edition
Class ? • Think about the things in the world around you. The things that surround you have attributes (properties) and they behave in certain ways. We can think of these behaviors as a set of operations. • You'll also see that things naturally fall into categories (automobiles, furniture, washing machines. . . ). • We refer to these categories as classes. • A class is a category or group of things that have the same attributes and the same behaviors. Sams Teach Yourself UML in 24 Hours, Third Edition
Object ? • Object is a concept or thing with crisp boundaries and meaning for the problem at hand • Object : instance of class • You and I, for example, are instances of the Person class • An object has structure. • it has attributes (properties) • and behavior. An object's behavior consists of the operations it carries out. • Attributes and operations taken together are called features. Sams Teach Yourself UML in 24 Hours, Third Edition
Object ? • As objects in the Person class, you and I each have these attributes: height, weight, and age. • Each of us is unique because of the specific values that each of us has for those attributes. • We also perform these operations: eat, sleep, read, write, talk, go to work, and more. (Or in objectspeak eat(), sleep(), read(), write(), talk(), and goToWork().) • If we were to create a system that deals with information on people—say, a payroll system or a system for a human resources department—we would likely incorporate some of these attributes and some of these operations in our software. Sams Teach Yourself UML in 24 Hours, Third Edition
Class and Object • Class template for making object • The WashingMachine class is a template for creating newinstances of washing machines. Sams Teach Yourself UML in 24 Hours, Third Edition
Object Concept - Abstraction • Abstraction means, simply, to filter out an object's properties and operations until just the ones you need are left. • Program that Simulate how Washing Machine does is need serialNumber? • Program track transaction in laundry is need serialNumber ? Sams Teach Yourself UML in 24 Hours, Third Edition
Object Concept - Inheritance • Washing machines, refrigerators, microwave ovens, toasters, dishwashers, radios, blenders, and irons are all appliances. In the world of object orientation, we would say that each one is a subclass of the Appliance class. Another way to say this is that Appliance is a superclass of all those others. • Appliance is a class that has the attributes onOffSwitch and electricWire, and the operations turnOn() and turnOff(). Thus, if you know something is an appliance, you know immediately that it has the Appliance class's attributes and operations. Sams Teach Yourself UML in 24 Hours, Third Edition
Inheritance… • Object-orientation refers to this relationship as inheritance. Each subclass of Appliance (WashingMachine, Refrigerator, Blender, and so on) inherits the features of Appliance. • It's important to note that each subclass adds its own attributes and operations. • Inheritance doesn't have to stop there. Appliance, for example, is a subclass of the HouseholdItem class. Furniture is another subclass of HouseholdItem, Furniture, of course, has its own subclasses. Sams Teach Yourself UML in 24 Hours, Third Edition
SubClass - SuperClass Sams Teach Yourself UML in 24 Hours, Third Edition
Object Concept - Polymorhpism • Sometimes an operation has the same name in different classes. • For example, you can open a door, you can open a window, and you can open a newspaper, a present, a bank account, or a conversation. In each case you're performing a different operation. • In object-orientation each class "knows" how that operation is supposed to take place. This is called polymorphism • Polymorphism enables the modeler to maintain that terminology without having to make up artificial words to maintain an unnecessary (and unnatural) uniqueness of terms Sams Teach Yourself UML in 24 Hours, Third Edition
Object Concept - Encapsulation • When most people watch a television show, they usually don't know or care about the complex electronics components that sit in back of the TV screen and all the many operations that have to occur in order to paint the image on the screen. The TV does what it does and hides the process from us. • That's the essence of encapsulation: When an object carries out its operations, those operations are hidden Sams Teach Yourself UML in 24 Hours, Third Edition
Why is Encapsulation important? • In the software world, encapsulation helps cut down on the potential for bad things to happen. • In a system that consists of objects, the objects depend on each other in various ways. If one of them happens to malfunction and software engineers have to change it in some way, hiding its operations from other objects means that it probably won't be necessary to change those other objects. • Your computer monitor, in a sense, hides its operations from your computer's CPU. When something goes wrong with your monitor, you either fix the monitor or replace it. You probably won't have to fix or replace the CPU along with it. Sams Teach Yourself UML in 24 Hours, Third Edition
Information Hiding - Interface • Because encapsulation means that an object hides what it does from other objects and from the outside world, encapsulation is also called information hiding. • But an object does have to present a "face" to the outside world so you can run those operations. • The TV, for example, has a set of buttons either on the TV itself or on a remote. • A washing machine has a set of dials that enable you to set temperature and water level. • The TV's buttons and the washing machine's dials are called interfaces. Sams Teach Yourself UML in 24 Hours, Third Edition
Message Sending • In a system, objects work together. They do this by sending messages to one another. One object sends another a message—a request to perform an operation, and the receiving object performs that operation. • When you want to watch a TV show, you hunt around for the remote, and push the On button. What happens? The remote-object sends a message to the TV-object to turn itself on. The TV-object receives this message, knows how to perform the turn-on operation, and turns itself on. • When you want to watch a different channel, you click the appropriate button on the remote, and the remote-object sends a different message—"change the channel"—to the TVobject. • The remote can also communicate with the TV via other messages for changing the volume, muting the volume, etc Sams Teach Yourself UML in 24 Hours, Third Edition
Message Sending… • Most of the things you do from the remote you can also do by getting out of the chair, going to the TV, and clicking buttons on the TV. The interface the TV presents to you (the set of buttons) is obviously not the same interface it presents to the remote (an infrared receiver). Sams Teach Yourself UML in 24 Hours, Third Edition
Associations • Another common occurrence is that objects are typically related to one another in some fashion. For example, when you turn on your TV, in object-oriented terms, you're in an association with your TV. • The "turn-on" association is unidirectional (one-way) • Other associations, like "is married to," are bidirectional. Sams Teach Yourself UML in 24 Hours, Third Edition
Unidirectional Associations • Although you watch too much television, however, it doesn't return the favor. Sams Teach Yourself UML in 24 Hours, Third Edition
Associations… • Sometimes an object might be associated with another in more than one way. • If you and your coworker are friends, that's an example. You're in an "is the friend of" association, as well as an "is the coworker of" association Sams Teach Yourself UML in 24 Hours, Third Edition
Associations… • A class can associate with more than one other class. A person can ride a car and a person can also ride in a bus. Sams Teach Yourself UML in 24 Hours, Third Edition
Multiplicity • Multiplicity is an important aspect of associations among objects. It tells the number of objects in one class that relate to a single object of the associated class. • For example, in a typical college course, the course is taught by a single instructor. The course and the instructor are in a one-to-one association. In a proseminar, however, several instructors might teach the course throughout the semester. In that case, the course and the instructor are in a one-to-many association. • A bicycle rides on two tires (a one-to-two multiplicity), a tricycle rides on three, and an 18-wheeler on 18. Sams Teach Yourself UML in 24 Hours, Third Edition
Aggregation • Think about your computer system. It consists of a CPU box, a keyboard, a mouse, a monitor, a CD-ROM drive, one or more hard drives, a modem, a disk drive, a printer, and possibly some speakers. Inside the CPU box, along with the aforementioned drives, you have a CPU, a graphics card, a sound card, and some other elements you would undoubtedly find it hard to live without. • Your computer is an aggregation • Aggregation is a type of association. An aggregate object consists of a set of component objects. Sams Teach Yourself UML in 24 Hours, Third Edition
Aggregation • Like many other things worth having, the computer is made from a number of different types of components Sams Teach Yourself UML in 24 Hours, Third Edition
Composition • One form of aggregation involves a strong relationship between an aggregate object and its component objects. This is called composition. • The key to composition is that the component exists as a component only within the composite object. For example, a shirt is a composite of a body, a collar, sleeves, buttons and buttonholes. Do away with the shirt and the collar becomes useless. Sams Teach Yourself UML in 24 Hours, Third Edition
Composition… • Sometimes a component in a composite doesn't last as long as the composite itself. • The leaves on a tree can die out before the tree does. If you destroy the tree, the leaves also die Sams Teach Yourself UML in 24 Hours, Third Edition
Can You Explain ? • What is an object? • How do objects work together? • What does multiplicity indicate? • Can two objects associate with one another in more than one way? • What is inheritance? • What is encapsulation? Sams Teach Yourself UML in 24 Hours, Third Edition