In: Computer Science
Read the following specification and then answer the questions that follow. Specification:
A soccer league is made up of at least four soccer teams. Each soccer team is composed of seven(7) to eleven(11) players, and one player captains the team. A team has a name and a record of wins and losses. Players have a number and a position. Soccer teams play games against each other. Each game has a score and a location. Teams are sometimes lead by a coach. A coach has a level of accreditation and a number of years of experience, and can coach multiple teams. Coaches and players are people, and people have names and addresses.
Question: Carry out an initial object oriented design for the above specification.
You must identify and write down. Classes that you think will be required [5 marks]
Their attributes and behaviours [2 marks]
Any inheritance relationships you can identify [3 marks]
Any other relationships (aggregation or otherwise between the classes) [2 marks]
Object–Oriented Design (OOD): involves implementation of the conceptual model produced during object-oriented analysis. In OOD, concepts in the analysis model, which are technology−independent, are mapped onto implementing classes, constraints are identified and interfaces are designed, resulting in a model for the solution domain, i.e., a detailed description of how the system is to be built on concrete technologies.
The implementation details generally include −
Object:
An object is a real-world element in an object–oriented environment that may have a physical or a conceptual existence. Each object has −
Identity that distinguishes it from other objects in the system.
State that determines the characteristic properties of an object as well as the values of the properties that the object holds.
Behavior that represents externally visible activities performed by an object in terms of changes in its state.
Object-oriented programming (OOP) languages are designed to overcome these problems.
Objects can be modelled according to the needs of the application. An object may have a physical existence, like a customer, a car, etc.; or an intangible conceptual existence, like a project, a process, etc.
Class :
A class represents a collection of objects having same characteristic properties that exhibit common behavior. It gives the blueprint or description of the objects that can be created from it. Creation of an object as a member of a class is called instantiation. Thus, object is an instance of a class.
The constituents of a class are −
A set of attributes for the objects that are to be instantiated from the class. Generally, different objects of a class have some difference in the values of the attributes. Attributes are often referred as class data.
A set of operations that portray the behavior of the objects of the class. Operations are also referred as functions or methods.
As an example, suppose you wish to write a computer soccer games . It is quite difficult to model the game in procedural-oriented languages. But using OOP languages, you can easily model the program accordingly to the "real things" appear in the soccer games.
Every player is a different person, but they all have something in common: they can perform the same actions, such as running or passing, and they share certain features, like a number and a position. The same thing could be said for coaches and the rest of the staff. Each one of them will have different combinations, but they all follow the same model.
A class is a model, a blueprint, a template that describe some
features. More precisely, a class represents data, usually with
variables called fields, and behaviors, represented by functions,
usually called methods. For example, a class Player
could have a field called Role
to represent its role,
or position, on the actual field of the game, a Name
,
to represent his name. As behavior it could have a method
Pass(Player)
, that would make him pass the ball to
some other player.
ex:
class player
{
Text name
Text address
pass (player Teammate)
{
//body of method
}
}
A class can be visualized as a three-compartment box, as illustrated:
In other words, a class encapsulates the static attributes (data) and dynamic behaviors (operations that operate on the data) in a box.
Class Members: The data members and member functions are collectively called class members.
Object:
If a class is a model what are the actual players? They are called objects or instances of the class. In the previous example, the argument teamMate was an object. To create an object from a class you instantiate, or create, an object.
For example, the object of the class Player
smith has the Name
and the
address
here player is class and name and address are objects
coach is class and name,age,address are objects.
ex:class SoccerPlayer { // classname
private:
int number; // Data members (variables)
string name;
int x, y;
public:
void run(); // Member functions
void kickBall();
}
Encapsulation
Encapsulation is the process of binding both attributes and methods together within a class. Through encapsulation, the internal details of a class can be hidden from outside. It permits the elements of the class to be accessed from outside only through the interface provided by the class.
For instance, to simulate the ability to run, the class
Player
has a method called Run()
, but it
also has a field called Legs
. This field represents
the condition of the legs of the player: how tired they are and
their health, that is to say, whether they are injured. The outside
world does not need to know that the Player
has
Legs
and how they operate.
Data Hiding:
Typically, a class is designed such that its data (attributes) can be accessed only by its class methods and insulated from direct outside access. This process of insulating an object’s data is called data hiding or information hiding.
Abstraction
Abstraction refers to hiding the details of the implementation from the outside the class.
As an example, the object Man of the class
Coach
call the method Run()
of
John, an object of the class Player
.
It does not know what John must do to actually
run. It just needs to know that an object of the class
Player
has the method Run()
.
Inheritance :
Inheritance is the mechanism that permits new classes to be created out of existing classes by extending and refining its capabilities. The existing classes are called the base classes/parent classes/super-classes, and the new classes are called the derived classes/child classes/subclasses
As an example, you notice that the Player
,
Coach
and Staff
classes have a name and a
salary, so you create a parent class called Person
and
you made them inherit from it. Notice that you just keep creating
only an object of the class Player
,
Coach
, etc. you don’t need to explicitly create an
object of the class Person
.
types of inheritance
Multiple Inheritance − A subclass derives from more than one super-classes.
Multilevel Inheritance − A subclass derives from a super-class which in turn is derived from another class and so on.
Interface :
Interface, also known as protocol, is an alternative to inheritance for two unrelated classes to communicate with each other. An interface defines methods and (often, but not always) values. Every class that implements the interface must provide a concrete method for each method of the interface.
For example, you want to simulate ejection, or dismissal. Since
only players and coaches can be ejected from the field, you cannot
make it a method of the parent class that represents people. So you
create an interface Ejectable
with a method
Ejection()
and make Player
and
Coach
implement it.
Association:
An association simply describes any kind of working relation. The two object are instances of completely unrelated classes, and none of the objects controls the lifecycle of the other one. They just collaborate to accomplish their own goals.
Imagine that you want to add the effect of the audience on the
players; in real life the audience is made of people, but in our
simulation, they are not children of the class Person
.
You simply want to make that if the object
HomePeople of the class Audience
is
cheering then John is playing better. So
HomePeople can affect the behavior of
John, but neither HomePeople nor
John can control the lifecycle of the other.
Aggregation :
An aggregation describes a relationship in which one object belongs to another object, but they are still potentially independent. The first object does not control the lifecycle of the second.
In a team sport, all objects of class Player
belong
to an object of Team
, but they don’t die just because
they are fired. They could be unemployed for a while or change
Team
.
This kind of relationship is usually described as has-a
(or is-part-of), or on the inverse
belongs-to. In our example the object
Winners of Team
has-a
John of Player
, or on the inverse
John belongs-to
Winners.