Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
41 views50 pages

AProgrammingDoc 1

read

Uploaded by

supunikaushi4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views50 pages

AProgrammingDoc 1

read

Uploaded by

supunikaushi4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 50

Higher Nationals

Internal verification of assessment decisions – BTEC (RQF)

INTERNAL VERIFICATION – ASSESSMENT DECISIONS


Programme title

Assessor Internal Verifier


Unit 20 – Advance Programming
Unit(s)

Assignment title

Student’s name
List which assessment criteria Pass Merit Distinction
the Assessor has awarded.

INTERNAL VERIFIER CHECKLIST

Do the assessment criteria awarded match


those shown in the assignment brief? Y/N

Is the Pass/Merit/Distinction grade awarded


justified by the assessor’s comments on the Y/N
student work?
Has the work been assessed Y/N
accurately?
Is the feedback to the student:
Give details:
• Constructive? Y/N
• Linked to relevant assessment criteria? Y/N
Y/N
• Identifying opportunities for
improved performance?
Y/N
• Agreeing actions?
Does the assessment decision need Y/N
amending?

Assessor signature Date

Internal Verifier signature Date


Programme Leader signature (if required)
Date
Confirm action completed
Remedial action taken
Give details:

Assessor signature Date


Internal Verifier
signature Date
Programme Leader
signature (if required) Date
Higher Nationals - Summative Assignment Feedback Form
Student Name/ID
Unit 20 – Advance Programming
Unit Title
Assignment Number 1 Assessor
Date Received
Submission Date 1st submission
Date Received 2nd
Re-submission Date submission
Assessor Feedback:
LO1 Demonstrate a range of interpersonal and transferable communication skills to a target audience
Pass, Merit & Distinction P1 P2 M1 D1
Descripts

LO2 Apply critical reasoning and thinking to a range of problem-solving scenarios


Pass, Merit & Distinction P3 P4 M2 M3 D2
Descripts

LO3 Discuss the importance and dynamics of working within a team and the impact of team working in
different environments
Pass, Merit & Distinction P5 P6 M4 D3
Descripts

LO4 Examine the need for Continuing Professional Development (CPD) and its role within the workplace
and for higher level learning
Pass, Merit & Distinction P7 P8 M5 D4
Descripts

Grade: Assessor Signature: Date:

Resubmission Feedback:

Grade: Assessor Signature: Date:

Internal Verifier’s Comments:

Signature & Date:

* Please note that grade decisions are provisional. They are only confirmed once internal and external moderation has taken place and grades
decisions have been agreed at the assessment board.
Assignment Feedback
Formative Feedback: Assessor to Student

Action Plan

Summative feedback

Feedback: Student to Assessor


Assessor signature Date

Student signature Date

Pearson Higher Nationals in


Computing
Unit 20 – Advance Programming
General Guidelines

1. A cover page or title page should be attached to your assignment. Use page 1 of this assignment
brief as your cover page and make sure all details are accurately filled.
2. The entire assignment brief should be attached as the first section of your assignment.
3. The assignment should be prepared using a word processing software.
4. The assignment should be word processing in an A4 sized paper.
5. Allow 1” margin on top, bottom and right sides of the paper and 1.25” on the left side (for
binding).

Word Processing Rules

1. The font size should be 12 point, and should be in the style of Time New Roman.
2. Set line spacing to 1.5. Justify all paragraphs.
3. Ensure that all headings are consistent in terms of size and font style.
4. Use footer function on the word processor to insert your name, unit, assignment no, and page
number on each page. This is useful if individual sheets get detached from the submission.
5. Use the spell check and grammar check function of the word processing application to review
the use of language on your assignment.
Important Points:
1. Carefully check carefully the hand in date and the instructions given with the assignment. Late
submissions will not be accepted.
2. Ensure that sufficient time is spent to complete the assignment by the due date.
3. Do not wait till the last minute to get feedback on the assignment. Such excuses will not be
accepted for late submissions.
4. You must be responsible for efficient management of your time.
5. If you are unable to hand in your assignment on time and have valid reasons such as illness, you
may apply (in writing) for an extension.
6. Failure to achieve at least a PASS grade will result in a REFERRAL grade.
7. Non-submission of work without valid reasons will lead to an automatic REFERRAL. You will
then be asked to complete an alternative assignment.
8. If you use other people’s work or ideas in your assignment, it must be properly referenced, using
the HARVARD referencing system, in your text or any bibliography. Otherwise, you’ll be found
guilty of committing plagiarism.
9. If you are caught plagiarising, your grade will be reduced to a REFERRAL or at worst, you could
be excluded from the course.
Student Declaration

I hereby, declare that I know what plagiarism entails, namely to use another’s work and to present it as
my own without attributing the sources in the correct form. I further understand what it means to copy
another’s work.

1. I know that plagiarism is a punishable offence because it constitutes theft.


2. I understand the plagiarism and copying policy of Edexcel UK.
3. I know what the consequences will be if I plagiarise or copy another’s work in any of the
assignments for this program.
4. I declare therefore that all work presented by me for every aspect of my program, will be my
own, and where I have made use of another’s work, I will attribute the source in the correct
way.
5. I acknowledge that the attachment of this document signed or not, constitutes a binding
agreement between myself and Edexcel UK.
6. I understand that my assignment will not be considered as submitted if this document is not
attached to the assignment.

Student’s Signature: Date:


(Provide E-mail ID) (Provide Submission Date)
Higher National Diploma in Business
Assignment Brief
Student Name /ID Number

Unit Number and Title Unit 20 – Advance Programming

Academic Year 2018/19


Unit Tutor

Assignment Title

Issue Date

Submission Date

IV Name & Date

Submission format

The submission is in the form of an individual written report about. This should be written in a
concise, formal business style using single spacing and font size 12. You are required to make use
of headings, paragraphs and subsections as appropriate, and all work must be supported with
research and referenced using the Harvard referencing system. Please also provide an end list of
references using the Harvard referencing system. Please note that this is an activity-based
assessment where your document submission should include evidences of activities carried out
and of team working. To carry out activities given on the brief, you are required to form groups,
comprising not exceeding 15 individuals.

The recommended word count is 4,000–4,500 words for the report excluding annexures. Note
that word counts are indicative only and you would not be penalised for exceeding the word
count.
Unit Learning Outcomes:

Learning Outcomes
By the end of this unit students will be able to:
LO1. Examine the key components related to the object-orientated programming
paradigm, analysing design pattern types.
LO2. Design a series of UML class diagrams.
LO3. Implement code applying design patterns.
LO4 Investigate scenarios with respect to design patterns.
Assignment Brief and Guidance:

Family Dental Care (FDC) is a leading up market dental surgery located in Kandy. It provides all
types of dental treatments to patients which include extractions, nerve fillings, maxillofacial
surgeries (i.e. surgeries involving jaw bone) and sophisticated dental implants. It is visited by
prominent dentists and dental consultants with post graduate qualifications, some of whom are
working at the Faculty of Dental Science at the University of Peradeniya.
Patients consult doctors by appointment. On their first visit, patients are required to register by
entering their personal details such as name, address, national identity card number and
contact number. A small fee is charged from the patient during registration. A separate fee is
charged for each treatment given.
Doctors too must get registered at FDC by providing personal details such as name, address,
date of birth, national ID number and contact number. In addition, consultants must provide the
name of their post graduate qualification along with the country of the University that granted
it and ordinary dentists should indicate the number of years of experience.
FDC consists of four fully equipped surgery rooms so that four patients can be accommodated at
any given time. FDC also contains a dental scan room which can be attended by one patient at a
time. The dental scan machine is operated by one of the dentists of the FDC facility. Normally, a
dentist without appointments for a given time slot (say, between 5 PM and 6 PM) is assigned to
the machine by the manager. When that time slot finishes, another doctor who is free will be
assigned.
The staff of FDC is made up of a manager, four nurses (one for each of the four surgery rooms)
and a receptionist who handles registrations and appointments.
An information system is required to keep track of patients, doctors, appointments, treatments
given to patients and payments. The system must also maintain information about the staff. It
has been decided to use an object oriented approach to design and implement the system.
Task 1
Briefly describe the following Object oriented concepts. Provide diagrams and code snippets to
supplement your explanations. (LO 1)
i) Class
ii) Object
iii) Message
iv) Encapsulation

Task 2
Design a detailed UML class diagram for the Family Dental Care system. Your solution should
demonstrate all three inter-class relationships, namely Association, Inheritance and
Aggregation/composition. The classes should include attributes and methods. (LO 2)

Task 3
i) Find one occurrence of inheritance (i.e. generalization) from your class diagram and
implement it using a suitable object oriented language. Your implementation must
cover the super class and at least two sub-classes. (LO 1)
ii) Explain the term “Polymorphism”. Demonstrate, with the aid of a code snippet, how
polymorphism can be achieved via method redefinition from the “inheritance” code
that you have produced in task 3 – (i). (LO 1)
iii) Find one occurrence of Aggregation/composition in your class diagram and
implement it using a suitable object oriented language. Your implementation must
cover both the “whole” class and component class. Include a main program that
demonstrates how components can be instantiated and stored in the whole. Provide
screen shots (in addition to source code) as evidence of program execution. (LO 1)

iv) At FDC, dentists work at the scan machine in turns. At any given time, a dentist may be
either treating a patient or operating the dental scanner. In other words, a doctor plays
two roles at FDC: Treating patients and operating Dental Scanner. If we send a
getDetails() message to a dentist object, the response varies depending on his/her role
at the moment. Which object oriented feature would enable this to be realized? Provide
a detailed explanation and supplement it with appropriate UML diagrams. Provide an
implementation for the design using a suitable object oriented language. Provide screen
shots and source code listing as evidence of program execution. (LO 1) (LO 2)

Task 4
i) What is a “Design pattern” ? How are they documented? How would design patterns
be useful to computer programmers? (LO 1)
ii) FDC owns a very expensive, state of the art dental scan machine (a device far superior
to a traditional dental X-ray machine) manufactured by Toshiba, Japan. FDC will be
owning just one such machine in the foreseeable future. When modeling and
implementing FDC system in software, you must ensure that only one instance of that
machine is created. Which design pattern could come to your assistance? Justify your
answer. Provide a UML diagram and an implementation for the pattern you have
recommended. You may include suitable attributes for the machine such as serial
number, make, country of origin and cost. Your implementation should allow the user
to enter details of the dental scanner and create the sole instance of that machine. Any
attempt to create more than one instance of the scanner must be prevented. Provide
screen shots and source code listing as evidence of program execution.
(LO 2) (LO 3) (LO 4)
iii) The treatments given to patients at FDC can be of two types: elementary and complex.
An elementary treatment is a single procedure such as a simple extraction or a dental
filling which usually requires a single visit.

A complex treatment is a composition of several elementary treatments possibly taking


several visits by the patient, for example, a tooth implant for a severely decayed tooth
may require an antibiotic treatment for curing any pus accumulation, an extraction of
the affected tooth and finally a dental implant of a Titanium tooth.
The users of the system (mainly consultants and dentists) wish to handle both of these
treatments in a uniform way. For example, the message getDetails() sent to an
elementary treatment should provide details of a single procedure while the same
message given to a complex treatment would list details of each elementary treatment
that make up the complex treatment.
Which design pattern is suitable for handling this requirement? Justify your answer.
Support your answer with a context specific UML class diagram. Implement your
design by using a suitable object oriented language. Provide screen shots as evidence of
program execution (in addition to source code listing). (LO 2) (LO 3)
(LO 4)

iv) Patients who need dental scans are kept in a First In First Out queue. Assume that you
have found an already developed Queue container in a software library. It provides
standard queue operations to insert and remove data (known as enque and deque
respectively). However, you need some specific operations such as search() to look up a
particular patient and showAll() to list all the patients in the queue. These additional
operations are not provided by the library unit. Which design pattern would enable you
to obtain the additional functionality required by the queue? Justify your answer.
Supplement your answer with a UML class diagram. Coding is NOT required. (LO 2)
(LO 4)
Acknowledgement
I would like to express my deepest appreciation to ……, the lecturer who provided me the
possibility to complete this assignment with contribution of suggestions and encouragement,
till the end especially in completing this assignment. His readiness to discuss all important
matters at work deserves special attention of. I would like to thank all other teaching stuff staff
of the campus for their cooperation and important support for their valuable teaching and

constant advice which made me to finish this program successfully.


Contents
Task 1........................................................................................................................................................19
OOP Concepts........................................................................................................................................19
1. Class...................................................................................................................................................22
2.............................................................................................................................................................23
Inheritance............................................................................................................................................23
Output...............................................................................................................................................25
3. Objects...............................................................................................................................................25
4. Abstraction........................................................................................................................................25
5. Encapsulation....................................................................................................................................25
6. Polymorphism....................................................................................................................................26
Message................................................................................................................................................26
Task 2........................................................................................................................................................27
Task 3........................................................................................................................................................28
Inheritance............................................................................................................................................28
Types of Inheritance..............................................................................................................................28
Single Inheritance:.............................................................................................................................28
Multiple Inheritance:.........................................................................................................................29
Multilevel Inheritance:......................................................................................................................29
Inheritance in Java.............................................................................................................................30
Java Inheritance Syntax.....................................................................................................................30
Java Inheritance Example..................................................................................................................30
Super Keyword..................................................................................................................................31
Super Class – Employee.....................................................................................................................32
Sub classes.........................................................................................................................................33
3.2 Polymorphism..................................................................................................................................33
3.4 Aggregation / Composition..............................................................................................................35
Aggregation.......................................................................................................................................35
Aggregation Example in Java.............................................................................................................35
Why we need Aggregation?...............................................................................................................35
(beginnersbook, 2018).......................................................................................................................35
Composition......................................................................................................................................35
Aggregation vs Composition..............................................................................................................36
3.5 getDetails()......................................................................................................................................36
Polymorphism....................................................................................................................................36
Task 4........................................................................................................................................................38
sage of Design Pattern.......................................................................................................................38
Common platform for developers.....................................................................................................38
Best Practices.....................................................................................................................................38
Types of Design Patterns.......................................................................................................................38
Why are Design Patterns Important?....................................................................................................39
Detailed answers:..................................................................................................................................39
Communication, Learning and Enhanced Insight:..............................................................................39
Decomposing System into Objects :..................................................................................................40
Determining Object Granularity:.......................................................................................................40
Specifying Object Interface :..............................................................................................................40
Specifying Object Implementation:...................................................................................................40
Ensuring right reuse mechanism:......................................................................................................41
Relating run-time and compile time structures:................................................................................41
Designing for change:........................................................................................................................41
4.2 Design pattern could come to assistance & UML diagram and an implementation for the pattern
recommended.......................................................................................................................................42
Singleton design pattern........................................................................................................................42
Structure............................................................................................................................................42
4.3 Suitable Design pattern for handling this requirement & specific UML class diagram and
Implement design..................................................................................................................................44
Adapter Pattern.....................................................................................................................................44
Class Diagram....................................................................................................................................45
Object Adapter Vs Class Adapter.......................................................................................................45
Advantages........................................................................................................................................46
Disadvantages....................................................................................................................................46
4.4 Design pattern enable to obtain the additional functionality required by the queue Supplement
your answer with a UML class diagram.................................................................................................47
Iterator Pattern..................................................................................................................................47
Figure 1......................................................................................................................................................19
Figure 2.....................................................................................................................................................21
Figure 3......................................................................................................................................................23
Figure 4......................................................................................................................................................24
Figure 5......................................................................................................................................................27
Figure 6......................................................................................................................................................28
Figure 7......................................................................................................................................................28
Figure 8......................................................................................................................................................29
Figure 9......................................................................................................................................................29
Figure 10....................................................................................................................................................30
Figure 11....................................................................................................................................................30
Figure 12....................................................................................................................................................31
Figure 13....................................................................................................................................................31
Figure 14....................................................................................................................................................32
Figure 15....................................................................................................................................................33
Figure 16....................................................................................................................................................34
Figure 17....................................................................................................................................................36
Figure 18....................................................................................................................................................37
Figure 19....................................................................................................................................................42
Figure 20....................................................................................................................................................43
Figure 21....................................................................................................................................................43
Figure 22....................................................................................................................................................43
Figure 23....................................................................................................................................................45
Figure 24....................................................................................................................................................46
Figure 25....................................................................................................................................................47
Figure 26....................................................................................................................................................48
Figure 27....................................................................................................................................................48
Figure 28....................................................................................................................................................49
Figure 29....................................................................................................................................................50
Task 1

OOP Concepts

In the class-based object-oriented programming paradigm, “object” refers to a particular


instance of a class where the object can be a combination of variables, functions, and data
structures. A good understanding of OOPs concepts can help in decision making when designing
an application. How you should design an application and what language should be used.

Figure 1OOP Concept

Object Oriented programming is a programming style which is associated with the concepts like
class, object, Inheritance, Encapsulation, Abstraction, Polymorphism.

Object oriented programming


Object Oriented Programming is a programming philosophy based on the principle that
objects are the program's most important part. It allows users to create the objects they
want and then create methods for managing them. The goal of Object-Oriented
Programming is to manipulate such objects to achieve results.
Object-oriented programming is used in a modern programming language such as Java
Advantages of OOP.
 OOP offers easy to understand and a clear modular structure for programs.
 Objects created for Object-Oriented Programs can be reused in other programs.
Thus, it saves significant development cost.
 Large programs are difficult to write, but if the development and designing team
follow OOPS concept then they can better design with minimum flaws.
 It also enhances program modularity because every object exists independently.

Object
An object can be defined as a class instance, and a program may have multiple classnst
ances. An object contains the data as well as the function that works on the dataFor inst
ance-chair, bike, marker, pen, car, table, etc.
(Anon., 2019)
class Lamp {
boolean isOn;

void turnOn() {
isOn = true;
}

void turnOff() {
isOn = false;
}
}

class ClassObjectsExample {
public static void main(String[] args) {
Lamp l1 = new Lamp(); // create l1 object of Lamp class
Lamp l2 = new Lamp(); // create l2 object of Lamp class
}
}

Class
Object are group into classes. A class is template of blueprint for set similar objects from
one class a factory of objects. An object is an instance of a class.
class Lamp {

// instance variable
private boolean isOn;

// method
public void turnOn() {
isOn = true;
}

// method
public void turnOff() {
isOn = false;
}
1. }

Class account objects of the account class

a1
Account
AccountNo AccountNo: 011

Balance Balance: 4000

a2
Withdraw()
AccountNo:02
Deposit()
displayDetails() Balance:8000

Figure 2 class diagram

message passing
A message passing is a command sent to an object from outside. Objects communicate with
each other by passing messages. A message consists of the receiver object name and a methods
name with parameters.
The methods of an object are invoked by means of messages.
Example of messages:
a1.deposit(100); a2.withdraw(200);
 send(channel, message)

Encapsulation.
Linking/binding data operations together within a class and hiding how the object caries out the
operations from the outside world is called encapsulation. Data are declared Public.Access to
data/attributes is allowed only via the methods. Methods form the interface of the object.
This implements information hiding and minimizes side effects/ripple effects that arise with
software updates. It also makes software reuse arias.
METHOS

DATA

1. Class

A class is a collection of method and variables. It is a blueprint that defines the data and behavior
of a type.

Let’s take Human Being as a class. A class is a blueprint for any functional entity which defines
its properties and its functions. Like Human Being, having body parts, performing various
actions.

We can define a class using the class keyword and the class body enclosed by a pair of curly
braces, as shown in the following example:

Figure 3author develop

using System;
namespace name
{
public class SampleHelloWorld
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
2.
Inheritance

Inheritance is a feature of object-oriented programming that allows code reusability when a class
includes property of another class. Considering HumanBeing a class, which has properties like
hands, legs, eyes, mouth, etc, and functions like walk, talk, eat, see etc.

Man and Woman are also classes, but most of the properties and functions are included in
HumanBeing. Hence, they can inherit everything from class HumanBeing using the concept of
Inheritance. Here is a code example:
Figure 4Inheritance
Output

3. Objects

My name is Yann, and I am an instance/object of class Man. When we say, Human Being, Man
or Woman, we just mean a kind, you, your friend, and I. We are the forms of these classes. We
have a physical existence while a class is just a logical definition. We are the objects.

The syntax for creating an object of a class Man:

4. Abstraction

Abstraction means, showcasing only the required things to the outside world while hiding the
details. Continuing our example, Human Being’s can talk, walk, hear, eat, but the details of the
muscles mechanism and their connections to the brain are hidden from the outside world.

The concept of abstraction focuses on what an object does, instated of how an object is
represented or “how it works.” Thus, data abstraction is often used for managing large and
complex programs.

5. Encapsulation

Encapsulation means that we want to hide unnecessary details from the user. For example, when
we call from our mobile phone, we select the number and press call button. But the entire
process of calling or what happens from the moment we press or touch the call button to the
moment we start having a phone conversation is hidden from us.
6. Polymorphism

Polymorphism is a concept, which allows us to redefine the way something works, by either
changing how it is done or by changing the parts used to get it done. This can be done in two
ways, overloading and overriding.

If we walk using our hands, and not legs, here we will change the parts used to perform
something. Hence this is called Overloading.

(blog.usejournal, 2018)

Message
Message passing, in computer terms, refers to the sending of a message to a process which can be an
object, parallel process, subroutine, function or thread. This message can be used to invoke another
process, directly or indirectly. Message passing is especially useful in object-oriented programming
(OOP) and parallel programming when a single message (in the form of a signal, data packet

String user_name;
String user_type;
Dashboard dbord = new Dashboard(user_name, user_type);
Task 2

Figure 5class diagram (author developed)


Task 3

Inheritance

Inheritance is a mechanism in which one class acquires the property of another class. For
example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields
and methods of the existing class. Hence, inheritance facilitates Reusability and is an important
concept of OOPs.

Types of Inheritance
There are Various types of inheritance in Java:

Single Inheritance:

In Single Inheritance one class extends another class (one class only).

Figure 66author
(authordevelop
developed)
In above diagram, Class B extends only Class A. Class A is a super class and Class B is a Sub-class.

Multiple Inheritance:

In Multiple Inheritance, one class extending more than one class. Java does not support multiple
inheritance.

Figure 7 (author developed)

As per above diagram, Class C extends Class A and Class B both.

Multilevel Inheritance:

In Multilevel Inheritance, one class can inherit from a derived class. Hence, the derived class
becomes the base class for the new class.
Figure 8

Inheritance in Java

 In Java, when an "Is-A" relationship exists between two classes we use Inheritance
 The parent class is termed super class and the inherited class is the sub class
 The keyword "extend" is used by the sub class to inherit the features of super class
 Inheritance is important since it leads to reusability of code

Java Inheritance Syntax

Figure 9

Java Inheritance Example

Figure 10
Figure 11

Super Keyword

The super keyword is similar to "this" keyword.

The keyword super can be used to access any data member or methods of the parent class.

Super keyword can be used at variable, method and constructor level.

Syntax:

Figure 12

In this example from the scenario Employee class and Doctor, Nurse, Consultant, Receptionist are an
occurrence of inheritance.

Super Class – Employee


Sub Classes – Doctor, Nurse, Consultant, Receptionist
3.2 Polymorphism

Polymorphism is the ability of an object to take on many forms. The most common use of
polymorphism in OOP occurs when a parent class reference is used to refer to a child class
object.

Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java,
all Java objects are polymorphic since any object will pass the IS-A test for their own type and
for the class Object.

It is important to know that the only possible way to access an object is through a reference
variable. A reference variable can be of only one type. Once declared, the type of a reference
variable cannot be changed.

The reference variable can be reassigned to other objects provided that it is not declared final.
The type of the reference variable would determine the methods that it can invoke on the object.
3.4 Aggregation / Composition
Aggregation

Aggregation is a special form of association. It is a relationship between two classes like


association, however its a directional association, which means it is strictly a one way
association. It represents a HAS-A relationship.

Aggregation Example in Java

For example consider two classes Student class and Address class. Every student has an address so
the relationship between student and address is a Has-A relationship. But if you consider its vice
versa then it would not make any sense as an Address doesn’t need to have a Student necessarily.
Lets write this example in a java program.

Student Has-A Address

Why we need Aggregation?

To maintain code re-usability. To understand this lets take the same example again. Suppose
there are two other classes College and Staff along with above two classes Student and Address. In
order to maintain Student’s address, College Address and Staff’s address we don’t need to use
the same code again and again. We just have to use the reference of Address class while defining
each of these classes.

(beginnersbook, 2018)
Composition

Composition is a restricted form of Aggregation in which two entities are highly dependent on
each other.

 It represents part-of relationship.


 In composition, both the entities are dependent on each other.
 When there is a composition between two entities, the composed object cannot exist
without the other entity.
Figure 13composition example (author developed)

Aggregation vs Composition

1. Dependency: Aggregation implies a relationship where the child can exist


independently of the parent. For example, Bank and Employee, delete the Bank and the
Employee still exist. whereas Composition implies a relationship where the child cannot
exist independent of the parent. Example: Human and heart, heart don’t exist separate to
a Human
2. Type of Relationship: Aggregation relation is “has-a” and composition is “part-of”
relation.
3. Type of association: Composition is a strong Association whereas Aggregation is a
weak Association.

3.5 getDetails()
Polymorphism
Generally, the ability to appear in many forms. In object-oriented programming, polymorphism refers to a
programming language's ability to process objects differently depending on their data type or class. More
specifically, it is the ability to redefine methods for derived classes. For example, given a base class
shape, polymorphism enables the programmer to define different area methods for any number of derived
classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area
method to it will return the correct results. Polymorphism is considered to be a requirement of any true
object-oriented programming language (OOPL).
Figure 14class diagram (author developed)
Task 4

A design pattern is a repeatable solution to a software engineering problem. Unlike most program-specific
solutions, design patterns are used in many programs. Design patterns are not considered finished
product; rather, they are templates that can be applied to multiple situations and can be improved over
time, making a very robust software engineering tool. Because development speed is increased when
using a proven prototype, developers using design pattern templates can improve coding efficiency and
final product readability.

Design patterns represent the best practices used by experienced object-oriented software developers.
Design patterns are solutions to general problems that software developers faced during software
development. These solutions were obtained by trial and error by numerous software developers over
quite a substantial period of time.

sage of Design Pattern

Design Patterns have two main usages in software development.

Common platform for developers

Design patterns provide a standard terminology and are specific to particular scenario. For
example, a singleton design pattern signifies use of single object so all developers familiar with
single design pattern will make use of single object and they can tell each other that program is
following a singleton pattern.

Best Practices

Design patterns have been evolved over a long period of time and they provide best solutions to
certain problems faced during software development. Learning these patterns helps
unexperienced developers to learn software design in an easy and faster way.

Types of Design Patterns

As per the design pattern reference book Design Patterns - Elements of Reusable Object-
Oriented Software , there are 23 design patterns which can be classified in three categories:
Creational, Structural and Behavioral patterns. We'll also discuss another category of design
pattern: J2EE design patterns.

S.N Pattern & Description


1 Creational Patterns
These design patterns provide a way to create objects while hiding the creation
logic, rather than instantiating objects directly using new operator. This gives
program more flexibility in deciding which objects need to be created for a given
use case.
2 Structural Patterns
These design patterns concern class and object composition. Concept of inheritance
is used to compose interfaces and define ways to compose objects to obtain new
functionalities.
3 Behavioral Patterns
These design patterns are specifically concerned with communication between
objects.
4 J2EE Patterns
These design patterns are specifically concerned with the presentation tier. These
patterns are identified by Sun Java Center.
Table 1

Why are Design Patterns Important?

Short: They help us solve recurring design problems. Note that : design patterns don’t solve the
problem themselves, they help us solve the problem.

Detailed answers:
Communication, Learning and Enhanced Insight:

 Over the last decade design patterns have become part of every developer’s
vocabulary. This really helps in communication. One can easy tell another
developer on the team, “I’ve used Command pattern here” and the other
developer understands not just the design, but can also easily figure out the
rationale behind it. Design Patterns really help in learning, esp. when you are new
on a project. Also this helps in providing developers with better insight about
parts of the application or 3rd party frameworks they use.

Decomposing System into Objects :

 The hard part about OO Design is finding the appropriate objects and
decomposing a system. One has to think about encapsulation, granularity,
dependencies, flexibility, performance, evolution, reusability and so on. They all
influence decomposition, often in conflicting ways. Design Patterns really helps
identify less obvious abstractions. These objects are seldom found during analysis
or even the early design, they’re discovered later in the course of making a design
more flexible and reusable.

Determining Object Granularity:

 One thing I struggle a lot with is finding the right level of abstraction and granularity. Design
patterns helps in coming up with objects with different levels of granularity that makes sense.

Specifying Object Interface :

 Identifying the right interface and the relationship between various interface is not a one-shot
activity. Usually it takes several iterations to identify the right composition of interfaces. Forget
interfaces, most of the times, coming up with a method signature can also be quite tricky. Design
Patterns really helps in this area.

Specifying Object Implementation:

 How should we implement an Object? Given an interface there could be multiple concrete classes
of that type, each one can have very different implementations. Design Patterns provide guidance
like Program to an interface (type) not an implementation (concrete class) which can result in
really good OO code.
Ensuring right reuse mechanism:

 When to use Inheritance, when to use Composition, when to use Parameterized Types? Is
delegation the right design decision in this context? There are various questions that comes to a
programmer’s mind when they are trying to design highly reusable and maintainable code.
Knowledge of design patterns can really come handy when making such decisions.

Relating run-time and compile time structures:

 An object-oriented program’s run-time structure often bares little resembles to this code structure.
Sometimes looking at the code does not give us the insights into run-time structure. Knowledge
of design patterns can make some of the hidden structure obvious.

Designing for change:

 We all know that lack of continuous refactoring and design that doesn’t take change into account
risks major redesign in the future. Over the years we’ve also learnt that big upfront designs can’t
standup against the constant change/addition of software requirements. We’ve leant grouping
elements with similar change life cycle together yields in far more flexible and extendable design.
If we think some behavior or element of behavior is most likely to change, we try to abstract that
behavior in one place. While we understand these concepts are important design patterns really
make it possible to design such systems. Each design pattern lets some aspect of the system
structure vary independently of other aspects, thereby making a system more robust to a
particular kind of change.
4.2 Design pattern could come to assistance & UML diagram and an implementation for
the pattern recommended
Singleton design pattern

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes
under creational pattern as this pattern provides one of the best ways to create an object. This
pattern involves a single class which is responsible to create an object while making sure that
only single object gets created. This class provides a way to access its only object which can be
accessed directly without need to instantiate the object of the class.

Structure

Figure 15

Make the class of the single instance responsible for access and "initialization on first use". The single
instance is a private static attribute. The accessor function is a public static method.
Figure 16

Figure 17

Figure 18

4.3 Suitable Design pattern for handling this requirement & specific UML class diagram
and Implement design
Adapter Pattern

This pattern is easy to understand as the real world is full of adapters. For example consider a
USB to Ethernet adapter. We need this when we have an Ethernet interface on one end and USB
on the other. Since they are incompatible with each other. we use an adapter that converts one to
other. This example is pretty analogous to Object Oriented Adapters. In design, adapters are used
when we have a class (Client) expecting some type of object and we have an object (Adaptee)
offering the same features but exposing a different interface.

To use an adapter:
1. The client makes a request to the adapter by calling a method on it using the target interface.
2. The adapter translates that request on the adaptee using the adaptee interface.
3. Client receive the results of the call and is unaware of adapter’s presence.

Definition

The adapter pattern convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn’t otherwise because of incompatible
interfaces.

Class Diagram

Figure 19

The client sees only the target interface and not the adapter. The adapter implements the target interface.
Adapter delegates all requests to Adapter.

Object Adapter Vs Class Adapter


The adapter pattern we have implemented above is called Object Adapter Pattern because the adapter
holds an instance of adaptee. There is also another type called Class Adapter Pattern which use
inheritance instead of composition but you require multiple inheritance to implement it.
Class diagram of Class Adapter Pattern:

Figure 20

Here instead of having an adaptee object inside adapter (composition) to make use of its
functionality adapter inherits the adaptee. Since multiple inheritance is not supported by many
languages including java and is associated with many problems we have not shown
implementation using class adapter pattern.

Advantages

 Helps achieve reusability and flexibility.


 Client class is not complicated by having to use a different interface and can use
polymorphism to swap between different implementations of adapters.

Disadvantages

 All requests are forwarded, so there is a slight increase in the overhead.


 Sometimes many adaptations are required along an adapter chain to reach the type which
is required.

Figure 21

4.4 Design pattern enable to obtain the additional functionality required by the queue
Supplement your answer with a UML class diagram
Iterator Pattern
Iterator Pattern is a relatively simple and frequently used design pattern. There are a lot of data
structures/collections available in every language. Each collection must provide an iterator that lets it
iterate through its objects. However while doing so it should make sure that it does not expose its
implementation.
Suppose we are building an application that requires us to maintain a list of notifications. Eventually,
some part of your code will require to iterate over all notifications. If we implemented your collection of
notifications as array you would iterate over them as:
Figure 22

And if it were some other collection like set, tree etc. way of iterating would change slightly. Now, what
if we build an iterator that provides a generic way of iterating over a collection independent of its type.

Figure 23

Iterator pattern lets us do just that. Formally it is defined as below:


The iterator pattern provides a way to access the elements of an aggregate object without
exposing its underlying representation.
Class Diagram

Figure 24

Here we have a common interface Aggregate for client as it decouples it from the implementation of your
collection of objects. The Concrete Aggregate implements createIterator() that returns iterator for its
collection. Each Concrete Aggregate’s responsibility is to instantiate a ConcreteIterator that can iterate
over its collection of objects. The iterator interface provides a set of methods for traversing or modifying
the collection that is in addition to next()/hasNext() it can also provide functions for search, remove etc.
Let’s understand this through an example. Suppose we are creating a notification bar in our application
that displays all the notifications which are held in a notification collection. NotificationCollection
provides an iterator to iterate over its elements without exposing how it has implemented the collection
(array in this case) to the Client (NotificationBar).

The class diagram would be:


Figure 25
Bibliography

Alexander, 2010. Git Hub. [Online] Available at: https://github.com/dieuph/

Anon., 2019. guru99. [Online] Available at: https://www.guru99.com/java-oops-concept.html [Accessed


2019].

Anon., 2014. web.cs.ucla.edu. [Online] Available at: http://web.cs.ucla.edu/~palsberg/paper/toplas06.pdf

Anon., 2015. xenonstack. [Online] Available at: https://www.xenonstack.com/insights/what-is-test-


driven-development

Anon., 2017. javapapers.com. [Online] Available at: https://javapapers.com/oops/association-


aggregation-composition-abstraction-generalization-realization-dependency/

Jenkov, J., 2016. jenkov.com. [Online] Available at: http://tutorials.jenkov.com/java-unit-testing/simple-


test.html

MartinFowler, 2015. [Online] Available at: https://martinfowler.com/bliki/SelfTestingCode.html

Noble, J., 2019. homepages.ecs. [Online] Available at:


http://homepages.ecs.vuw.ac.nz/~kjx/papers/classify.pdf

sourcemaking, 2018. sourcemaking. [Online] Available at: https://sourcemaking.com/design_patterns


[Accessed 2019]

sourcemaking, 2018. sourcemaking. [Online] Available at:


https://sourcemaking.com/design_patterns/singleton [Accessed 2019].

techopedia, 2019. [Online]. webopedia, 2019. polymorphism. [Online]

Available at: https://www.webopedia.com/TERM/P/polymorphism.html

You might also like