Object-Oriented

Download Report

Transcript Object-Oriented

UMLにおける実体指向
•Object-oriented concepts have been around since the 1970s.
•A variety of programming languages, including C++,
Smalltalk, Java, and Eiffel, implement object-oriented
principles.
•Object-oriented modeling languages followed close behind,
built on earlier modeling environments based on basic
drawing tools and procedural modeling techniques like
structured analysis.
•By the early 1990s formal object-oriented methods such as
the Object Modeling Technique came into common use.
2009年度「専門演習Ⅰ」
2009/11/5
2
Objects and Classes
 An object can be a physical entity, such as a chair or a book.
 An object may also be intangible, such as a job or class
attendance.
Class attendance can be described, monitored, and
reported.
 Anything that you can describe can be represented as an
object, and that representation can be created, manipulated,
and destroyed to represent how you use the real object that
it models.
2009年度「専門演習Ⅰ」
2009/11/5
3
 To use objects, we first need to distinguish between a real
object and the representation of an object.
 In software projects we are nearly always working with
representations of objects.
 The system needs to manage the representation to reflect
how you are managing the real objects.
【例】
 an order-processing system needs to manage
customers, orders, and products.
 The software does not directly manipulate
customers, orders, or products.
 Instead the software creates representations called
abstractions, a general term referring to the use of objects
and classes.
2009年度「専門演習Ⅰ」
2009/11/5
4
 Defining an object has to take into account
information and behavior.
 Each object defines three basic types of information
and two types of behavior.
 Information
 Identity
 Structure
 State
 Behavior
 What it can do.
 What can be done to it.
2009年度「専門演習Ⅰ」
2009/11/5
5
 Encapsulation is another concept that sets the
object-oriented approach to software apart from its
predecessors.
 Encapsulation is a way to organize the many types of
information and behavior described earlier so that
objects can be used as efficiently and effectively as
possible.
 Encapsulation states that when designing an object we
should separate what we know about the object
according to two concepts:
2009年度「専門演習Ⅰ」
2009/11/5
6
 The minimum information needed to use the object.
 an external view that asks only,
"What can I do with this object?"
 The information required to make the object work
properly.
 an internal view that asks,
"How does this thing work?"
2009年度「専門演習Ⅰ」
2009/11/5
7
 Defining individual objects is just the beginning of object
modeling. In order for software objects to get work done,
they need to work together the way the objects they
represent work together.
 【例】
 When a customer orders tickets, the customer has to create
an order and add tickets to that order.
 The software objects that represent the customer, order, and
tickets need to exactly replicate the relationships between the
real-world objects.
 A link is a relationship between two objects.
 An association is a relationship between two classes.
 An association is the definition of a type of link in the
same way that a class is a definition of a type of object.
 Just as an object is an instance of a class, a link is an
instance of an association.
2009年度「専門演習Ⅰ」
2009/11/5
8
 Aggregation is a special type of association used to
indicate that the participating objects are not just
independent objects that know about each other.
 Instead, they are assembled or configured together
to create a new, more complex object.
【例】
 a number of different parts can be combined to create a
physical assembly,such as a car, a boat, or a plane.
 You could also create a logical assembly such as a team
where the parts are not physically connected to one
another but they still operate as a unit.
2009年度「専門演習Ⅰ」
2009/11/5
9
 Composition is used for aggregations in which the life span of the






part depends on the life span of the aggregate object.
The aggregate has control over the creation and destruction of the part.
In short, the member object cannot exist apart from the assembly. The
part object would never exist on its own, in inventory or in a parts store.
Draw this stronger form of aggregation simply by making the
aggregation diamond solid (black). In Figure 4-10, the car example uses
aggregation, the hollow diamond. A car contains an engine as part of
its assembly.
The order example uses composition, the solid diamond. An order is
composed of line items. A line item is simply a place on an order that
records the fact that you want a product, along with the quantity, price,
and possible discount.
Whether the order is ever placed, or the order is deleted, the product
still exists. But a line item only exists as part of an order.
The line items would not continue to exist elsewhere on their own if
the order was deleted.
If the order containing the line items is deleted, the line items cease to
exist along with the order.
2009年度「専門演習Ⅰ」
2009/11/5
10
The term generalization is used in two ways.
1.Creating a generalization is the process of organizing the features of different types of
objects that share the same purpose.
 A generalization is a description of the features shared by a set of objects. We use the
generalization process to create generalizations routinely to organize large amounts of
information.
【例】
 fruits, vegetables and meat are foods, but they are different kinds or types of foods.
Phrases like "kind of" or "type of” are often used to describe a generalization relationship
between classes (for example, an apple is a type of fruit that is a kind of food and so on).
2.You might also hear this type of relationship referred to as inheritance. Many times the
terms generalization and inheritance are used synonymously.
【例】
 An apple is a kind of fruit, then it inherits all of the properties of fruit. Likewise, an
apple is a specialization of fruit because it inherits all of the generalized properties of
fruit and adds some unique properties that make apples special or unique within the
larger group of fruits.
 In the reverse, we could say that the concept "fruit" is a generalization of the facts that
are true for watermelons, apples, peaches, and all types of objects in the group.

2009年度「専門演習Ⅰ」
2009/11/5
11
 Polymorphism is the ability to dynamically choose the method for an
operation at runtime, depending on the type of object responding to
the request.
【例】
 < car and rocket > An application can invoke the accelerate operation
on any car, without having to first know how the individual car will
implement the operation.
 Polymorphism is made possible by two principles:
encapsulation and generalization.
 Encapsulation defines a means of organizing the information about an
object so that the interface/operation is separate from the
implementation/method.
 Generalization defines a means of organizing class features so that
shared features reside in one class and unique features reside in
another class.
 Applied together, these concepts say that an operation could be shared
and defined in a superclass, but that the method for that operation
might.be unique within each subclass.
2009年度「専門演習Ⅰ」
2009/11/5
12
 Polymorphism simply means many ways to accomplish
the same thing.
 A polymorphic operation has a different method in
each class in which it is implemented.
【例】
Figure 4-13 models discounts. All discounts can calculate
an applied discount amount. But not all discounts
implement the calculation the same way.
In this model, subclasses GroupDiscount,
ValueDiscount, QuantityDiscount each inherit the
“calculate applied discount” operation defined in the
Discount class.
 Each subclass provides its own method. It is also valid
to provide some or all of the implementation logic in
the superclass and have the subclasses inherit, and
then override or extend the logic.
2009年度「専門演習Ⅰ」
2009/11/5
13