Unit 3
Unit 3
1.Objects: All entities involved in the solution design are known as objects. For example,
person, banks, company, and users are considered as objects. Every entity has some attributes
associated with it and has some methods to perform on the attributes.
2.Classes: A class is a generalized description of an object. An object is an instance of a class. A
class defines all the attributes, which an object can have and methods, which represents the
functionality of the object.
3.Messages: Objects communicate by message passing. Messages consist of the integrity of the
target object, the name of the requested operation, and any other action needed to perform the
function. Messages are often implemented as procedure or function calls.
4.Abstraction In object-oriented design, complexity is handled using abstraction. Abstraction is
the removal of the irrelevant and the amplification of the essentials.
Different terms related to object design
5. Encapsulation: Encapsulation is also called an information hiding concept. The data and
operations are linked to a single unit. Encapsulation not only bundles essential information
of an object together but also restricts access to the data and methods from the outside
world.
6. Inheritance: OOD allows similar classes to stack up in a hierarchical manner where the
lower or sub-classes can import, implement, and re-use allowed variables and functions
from their immediate superclasses.This property of OOD is called an inheritance. This
makes it easier to define a specific class and to create generalized classes from specific
ones.
7. Polymorphism: OOD languages provide a mechanism where methods performing similar
tasks but vary in arguments, can be assigned the same name. This is known as
polymorphism, which allows a single interface is performing functions for different types.
Depending upon how the service is invoked, the respective portion of the code gets
executed.
Object Design
• The objects in the system are identified and their details are designed.
• Here, the designer details out the strategy chosen during the system design.
• The emphasis shifts from application domain concepts toward computer concepts.
• The objects identified during analysis are etched out for implementation with an aim to
minimize execution time, memory consumption, and overall cost.
Object design includes the following phases
• Object identification
• Object representation, i.e., construction of design models
• Classification of operations
• Algorithm design
• Design of relationships
• Implementation of control for external interactions
• Package classes and associations into modules
Object Identification
• The first step of object design is object identification. The objects identified in the object–
oriented analysis phases are grouped into classes and refined so that they are suitable for actual
implementation.
• The functions of this stage are −
• Identifying and refining the classes in each subsystem or package
• Defining the links and associations between the classes
• Designing the hierarchical associations among the classes, i.e., the
generalization/specialization and inheritances
• Designing aggregations
Object Representation
Once the classes are identified, they need to be represented using object modelling techniques.
This stage essentially involves constructing UML diagrams.
There are two types of design models that need to be produced −
• Static Models − To describe the static structure of a system using class diagrams and object diagrams.
• Dynamic Models − To describe the dynamic structure of a system and show the interaction between
classes using interaction diagrams and state–chart diagrams.
Classification of Operations
There may be more than one algorithm corresponding to a given operation. Once the alternative
algorithms are identified, the optimal algorithm is selected for the given problem domain. The
metrics for choosing the optimal algorithm are −
• Computational Complexity − Complexity determines the efficiency of an algorithm in terms
of computation time and memory requirements.
• Flexibility − Flexibility determines whether the chosen algorithm can be implemented
suitably, without loss of appropriateness in various environments.
• Understandability − This determines whether the chosen algorithm is easy to understand and
implement.
Design of Relationships
• The main relationships that are addressed comprise of associations, aggregations, and
inheritances.
• The designer should do the following regarding associations −
• Identify whether an association is unidirectional or bidirectional.
• Analyze the path of associations and update them if necessary.
• Implement the associations as a distinct object, in case of many–to-many relationships; or as a link to other
object in case of one–to-one or one–to-many relationships.
• Regarding inheritances, the designer should do the following −
• Adjust the classes and their associations.
• Identify abstract classes.
• Make provisions so that behaviors are shared when needed.
Implementation of Control
• The first step of object design is object identification. The objects identified in the object–
oriented analysis phases are grouped into classes and refined so that they are suitable for actual
implementation.
• The functions of this stage are −
• Identifying and refining the classes in each subsystem or package
• Defining the links and associations between the classes
• Designing the hierarchical associations among the classes, i.e., the
generalization/specialization and inheritances
• Designing aggregations
Object Representation
Once the classes are identified, they need to be represented using object modelling techniques.
This stage essentially involves constructing UML diagrams.
There are two types of design models that need to be produced −
• Static Models − To describe the static structure of a system using class diagrams and object diagrams.
• Dynamic Models − To describe the dynamic structure of a system and show the interaction between
classes using interaction diagrams and state–chart diagrams.
Classification of Operations
There may be more than one algorithm corresponding to a given operation. Once the alternative
algorithms are identified, the optimal algorithm is selected for the given problem domain. The
metrics for choosing the optimal algorithm are −
• Computational Complexity − Complexity determines the efficiency of an algorithm in terms
of computation time and memory requirements.
• Flexibility − Flexibility determines whether the chosen algorithm can be implemented
suitably, without loss of appropriateness in various environments.
• Understandability − This determines whether the chosen algorithm is easy to understand and
implement.
Design of Relationships
• The main relationships that are addressed comprise of associations, aggregations, and
inheritances.
• The designer should do the following regarding associations −
• Identify whether an association is unidirectional or bidirectional.
• Analyze the path of associations and update them if necessary.
• Implement the associations as a distinct object, in case of many–to-many relationships; or as a link to other
object in case of one–to-one or one–to-many relationships.
• Regarding inheritances, the designer should do the following −
• Adjust the classes and their associations.
• Identify abstract classes.
• Make provisions so that behaviors are shared when needed.
Implementation of Control
• Normally you will implement each class as a single contiguous block of attributes-a record structure. Each
attribute has a declared type, which can be a primitive type, such as integer, real or character or can be a
structured value, such as an embedded record structure or a fixed-length array
• A variable that identifies an object must therefore be implemented as a sharable reference and not simply by
copying the values of an object's attributes.
• Translating Classes into C Struct Declarations
• Each class in the design becomes a C struct. Each attribute defined in the class becomes a field of the C
struct.
The structure for the Window class is declared as:
struct Window
{
Length xmin;
Length ymin;
Length xmax;
Length ymax;
};
Some Questions:-
• An object can be allocated statically, automatically (on the stack), or dynamically (on the heap) because C can
compose a pointer to any object, including one embedded within another structure.
Passing Arguments To Methods
• Every method has at least one argument, the implicit self argument.
• In a non-object-oriented language, the argument must be made explicit, of course.
• Methods may also have additional objects as arguments. Some of the arguments may be simple data
values and not objects.
• In passing an object as an argument to a method, a reference to the object must be passed if the
value of the object can be updated within the method.
• If the method is a query that simply extracts information from an object without modifying it, then
a call-by-value mechanism can be used if the language permits.
• Passing all arguments as references is more uniform.
A consistent naming convention for method function
names.
• in C, concatenate the class name, two underscores, and the operation name. (The two underscores
separate the class name from the operation name, each of which may contain single underscores.)
• Passing arguments in C:
• In C, an object should always be passed by pointer.
• Although C permits structures to be passed by value, passing a pointer to an object structure is
usually more efficient and provides a uniform access mechanism for both query and update
operations.
Example and problem
• There are several ways to implement data structures for inheritance in a non-object-oriented language:
• Avoid it. Many applications do not require inheritance. Many other applications have only a few classes
requiring inheritance. Those classes not needing inheritance can be implemented as simple records.
• Flatten the class hierarchy. Use inheritance during design but expand each concrete class as an independent
data structure during implementation. Each inherited operation must be reimplemented as a separate method
on each concrete class. Flattening the hierarchy introduces duplication, but the use of language constructs
such as C macros can help. One useful technique is to group some inherited attributes into a record type and
embed the record in each concrete class to reduce the number of duplicated lines in each declaration.
• Break out separate objects. Instead of inheriting common attributes from a superclass, a group of attributes
can be pulled out of all the subclasses and implemented as a separate object with a reference to it stored
within each subclass. Grouping attributes under a separate type permits a single method to be written to
manipulate them. The subclasses must delegate operations to the referenced object.
Implementing Inheritance in C:
• To handle single inheritance, embed the declaration for the superclass as the first part of each
subclass declaration.
• The first field of each struct is a pointer to a class descriptor object shared by all direct instances of
a class.
• The class descriptor object is a struct containing the class attributes, including the name of the class
(optional) and the methods for the class.
• The class Shape is an abstract class, with concrete subclasses Box and Circle.
The C declarations for classes Shape, Box,
and Circle are as follows:
struct Shape
{
struct ShapeClass * class;
Length x;
Length y:
};
struct Box
{
struct BoxClass * class;
Length x;
Length y;
Length width;
Length height;
};
struct Circle
{
struct CircleClass * class:
Length x;
Length y;
Length radius;
};
Contd…
The first field of each structure is a pointer to the class descriptor for the actual class of each object instance.
This field is needed only if the run-time method resolution is to be done.
Multiple inheritances cannot be implemented using this approach because a subclass which has two
superclasses can align its attributes with both of them.
Implementing Associations