Uploaded by dumi.criss5952

Advance programming Complete

advertisement

1

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

S.M.Nipuna Madhuranga Samarakoon

List which assessment criteria the Assessor has awarded.

Pass Merit

INTERNAL VERIFIER CHECKLIST

Do the assessment criteria awarded match those shown in the assignment brief?

Is the Pass/Merit/Distinction grade awarded justified by the assessor’s comments on the student work?

Has the work been assessed accurately?

Is the feedback to the student:

Give details:

Constructive?

Linked to relevant assessment criteria?

Identifying opportunities for improved performance?

Agreeing actions?

Does the assessment decision need amending?

Assessor signature

Internal Verifier signature

Programme Leader signature (if required)

Y/N

Y/N

Y/N

Y/N

Y/N

Y/N

Y/N

Y/N

Distinction

Date

Date

Date

Confirm action completed

Remedial action taken

Give details:

Assessor signature

Internal

Verifier signature

Date

Date

Advance Programming S.M.N.M Samarakoon Batch-29

Programme Leader signature (if required)

2

Date

Advance Programming S.M.N.M Samarakoon Batch-29

3

Higher Nationals - Summative Assignment Feedback Form

Student Name/ID

S.M.Nipuna Madhuranga Samarakoon

Unit Title

Unit 20 – Advance Programming

Assignment Number

Submission Date

1

Assessor

Date Received

1st submission

Re-submission Date

Date Received 2nd submission

Assessor Feedback:

LO1 Demonstrate a range of interpersonal and transferable communication skills to a target audience

Pass, Merit &

Distinction Descripts

P1 P2 M1 D1

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

Pass, Merit &

Distinction Descripts

P3 P4

P6

M2

M4

M3

D3

D2

LO3 Discuss the importance and dynamics of working within a team and the impact of team working in different environments

Pass, Merit &

Distinction Descripts

P5

LO4 Examine the need for Continuing Professional Development (CPD) and its role within the workplace and for higher level learning

Pass, Merit &

Distinction Descripts

P7 P8 M5 D4

Grade: Assessor Signature: Date:

Resubmission Feedback:

Grade: Assessor Signature:

Internal Verifier’s Comments:

Date:

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.

Advance Programming S.M.N.M Samarakoon Batch-29

Assignment Feedback

Formative Feedback: Assessor to Student

Action Plan

Summative feedback

Feedback: Student to Assessor

4

Advance Programming S.M.N.M Samarakoon Batch-29

5

Date

Date

Assessor signature

Student signature

Pearson Higher Nationals in

Computing

Unit 20 – Advance Programming

Advance Programming S.M.N.M Samarakoon Batch-29

6

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 pag e. 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.

Advance Programming S.M.N.M Samarakoon Batch-29

7

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.

Advance Programming S.M.N.M Samarakoon Batch-29

8

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.

Nipuna2255@gmail.com

Student’s Signature:

(Provide E-mail ID)

21/07/2019

Date:

(Provide Submission Date)

Advance Programming S.M.N.M Samarakoon Batch-29

9

Higher National Diploma in Business

Assignment Brief

S.M.Nipuna Madhuranga Samarakoon Student Name /ID Number

Unit Number and Title

Academic Year

Unit 20 – Advance Programming

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.

Advance Programming S.M.N.M Samarakoon Batch-29

10

LO3. Implement code applying design patterns.

LO4 Investigate scenarios with respect to design patterns.

Assignment Brief and Guidance:

Advance Programming S.M.N.M Samarakoon Batch-29

11

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

Advance Programming S.M.N.M Samarakoon Batch-29

12 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

Advance Programming S.M.N.M Samarakoon Batch-29

13 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

Advance Programming S.M.N.M Samarakoon Batch-29

14 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)

Advance Programming S.M.N.M Samarakoon Batch-29

15

Contents

Task 1 .......................................................................................................................................................... 17

OOP Concepts ......................................................................................................................................... 17

1. Class .................................................................................................................................................... 19

2. Inheritance .......................................................................................................................................... 19

Output: ................................................................................................................................................ 21

3. Objects ................................................................................................................................................ 21

4. Abstraction .......................................................................................................................................... 21

5. Encapsulation ...................................................................................................................................... 21

6. Polymorphism ..................................................................................................................................... 21

Message .................................................................................................................................................. 22

Task 2 .......................................................................................................................................................... 22

Task 3 .......................................................................................................................................................... 24

What is Inheritance? ............................................................................................................................... 24

Types of Inheritance ............................................................................................................................... 24

Single Inheritance: .............................................................................................................................. 24

Multiple Inheritance: .......................................................................................................................... 24

Multilevel Inheritance: ........................................................................................................................ 25

Inheritance in Java .............................................................................................................................. 25

Java Inheritance Syntax:...................................................................................................................... 26

Java Inheritance Example.................................................................................................................... 26

Super Keyword .................................................................................................................................... 27

Super Class – Employee ...................................................................................................................... 28

Sub classes .......................................................................................................................................... 29

3.2 Polymorphism ................................................................................................................................... 29

3.4 Aggregation / Composition ............................................................................................................... 31

Aggregation ......................................................................................................................................... 31

Aggregation Example in Java .............................................................................................................. 31

Why we need Aggregation? ................................................................................................................ 31

Composition ........................................................................................................................................ 31

Aggregation vs Composition ............................................................................................................... 32

3.5 getDetails() .................................................................................................................................... 32

Advance Programming S.M.N.M Samarakoon Batch-29

16

Polymorphism ..................................................................................................................................... 32

Task 4 .......................................................................................................................................................... 33 sage of Design Pattern ............................................................................................................................ 33

Common platform for developers .......................................................................................................... 33

Best Practices ...................................................................................................................................... 34

Types of Design Patterns ........................................................................................................................ 34

Why are Design Patterns Important? ..................................................................................................... 34

Detailed answers: ................................................................................................................................ 34

4.2 Design pattern could come to assistance & UML diagram and an implementation for the pattern recommended ......................................................................................................................................... 36

Singleton design pattern ......................................................................................................................... 36

Structure ............................................................................................................................................. 36

4.3 Suitable Design pattern for handling this requirement & specific UML class diagram and

Implement design ................................................................................................................................... 38

Adapter Pattern ...................................................................................................................................... 38

Adapter Pattern .......................................................................... Ошибка! Закладка не определена.

Class Diagram: ..................................................................................................................................... 39

Object Adapter Vs Class Adapter ........................................................................................................ 39

Advantages: ......................................................................................................................................... 40

Disadvantages: .................................................................................................................................... 40

4.4 Design pattern enable to obtain the additional functionality required by the queue Supplement your answer with a UML class diagram .................................................................................................. 41

Iterator Pattern ................................................................................................................................... 41

Advance Programming S.M.N.M Samarakoon Batch-29

17

Figure 1OOP Concept .................................................................................................................................. 18

Figure 2OOP Concepts ................................................................................................................................ 18

Figure 3author develop ............................................................................................................................... 19

Figure 4Inheritance ..................................................................................................................................... 20

Figure 5class diagram (author developed) ................................................................................................. 23

Figure 6author develop ............................................................................................................................... 24

Figure 7 (author developed) ....................................................................................................................... 24

Figure 8 (author developed) ....................................................................................................................... 25

Figure 9Figure 9: inheritance example 1 (author developed ..................................................................... 28

Figure 10inheritance example 2 (author developed) ................................................................................. 29

Figure 11polymorphism example (author developed) ............................................................................... 30

Figure 12composition example (author developed) .................................................................................. 32

Figure 13class diagram (author developed) ............................................................................................... 33

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.

Advance Programming S.M.N.M Samarakoon Batch-29

18

Figure 1OOP Concept

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

Figure 2OOP Concepts

Advance Programming S.M.N.M Samarakoon Batch-29

19

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: using System; namespace name

{

Figure 3author develop

{ 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:

Advance Programming S.M.N.M Samarakoon Batch-29

20

Figure 4Inheritance

Advance Programming S.M.N.M Samarakoon Batch-29

21

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)

Advance Programming S.M.N.M Samarakoon Batch-29

Task 2

22

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);

Advance Programming S.M.N.M Samarakoon Batch-29

23

Figure 5class diagram (author developed)

Advance Programming S.M.N.M Samarakoon Batch-29

24

Task 3

What is 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).

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.

Advance Programming S.M.N.M Samarakoon Batch-29

25

Figure 8 (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.

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

Advance Programming S.M.N.M Samarakoon Batch-29

Java Inheritance Syntax:

26

Java Inheritance Example

Advance Programming S.M.N.M Samarakoon Batch-29

27

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:

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

Advance Programming S.M.N.M Samarakoon Batch-29

28

Super Class – Employee

Sub Classes – Doctor, Nurse, Consultant, Receptionist

Figure 9Figure 9: inheritance example 1 (author developed

Advance Programming S.M.N.M Samarakoon Batch-29

29

Sub classes :

(guru99, 2018)

Figure 10inheritance example 2 (author developed)

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.

Advance Programming S.M.N.M Samarakoon Batch-29

30

A reference variable can refer to any object of its declared type or any subtype of its declared type. A reference variable can be declared as a class or interface type.

(tutorialspoint, 2018)

In this example employee class (base class) use has the insert object. And Doctors too. In the doctor class

(child class) override the insert object.

Figure 11polymorphism example (author developed)

Advance Programming S.M.N.M Samarakoon Batch-29

31

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.

Advance Programming S.M.N.M Samarakoon Batch-29

32

Figure 12composition 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.

(geeksforgeeks, 2018)

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).

(webopedia, 2019)

Advance Programming S.M.N.M Samarakoon Batch-29

33

Figure 13class 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.

(techopedia, 2019)

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.

(sourcemaking, 2018) 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.

Advance Programming S.M.N.M Samarakoon Batch-29

34

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

1

2

3

4

Pattern & Description

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.

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.

Behavioral Patterns

These design patterns are specifically concerned with communication between objects.

J2EE Patterns

These design patterns are specifically concerned with the presentation tier. These patterns are identified by Sun Java Center.

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

Advance Programming S.M.N.M Samarakoon Batch-29

35 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.

Advance Programming S.M.N.M Samarakoon Batch-29

36

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/additon 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.

(tutorialspoint, 2018)

Structure

Advance Programming S.M.N.M Samarakoon Batch-29

37

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.

(sourcemaking, 2018)

Advance Programming S.M.N.M Samarakoon Batch-29

38

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.

Advance Programming S.M.N.M Samarakoon Batch-29

39

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:

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:

Advance Programming S.M.N.M Samarakoon Batch-29

40

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.

(geeksforgeeks, 2018)

Advance Programming S.M.N.M Samarakoon Batch-29

41

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:

Advance Programming S.M.N.M Samarakoon Batch-29

42

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.

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:

Advance Programming S.M.N.M Samarakoon Batch-29

43

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:

Advance Programming S.M.N.M Samarakoon Batch-29

(geeksforgeeks, 2018)

44

Advance Programming S.M.N.M Samarakoon Batch-29

45

Bibliography beginnersbook, 2018. beginnersbook. [Online]

Available at: https://beginnersbook.com/2013/05/aggregation/

[Accessed 2019].

blog.usejournal, 2018. blog.usejournal. [Online]

Available at: https://blog.usejournal.com/object-oriented-programming-concepts-in-simple-english-

3db22065d7d0

[Accessed 2019]. geeksforgeeks, 2018. geeksforgeeks. [Online]

Available at: https://www.geeksforgeeks.org/association-composition-aggregation-java/

[Accessed 2019]. geeksforgeeks, 2018. geeksforgeeks. [Online]

Available at: https://www.geeksforgeeks.org/adapter-pattern/

[Accessed 2019]. geeksforgeeks, 2018. geeksforgeeks. [Online]

Available at: https://www.geeksforgeeks.org/iterator-pattern/

[Accessed 2019]. guru99, 2018. guru99. [Online]

Available at: https://www.guru99.com/java-class-inheritance.html

[Accessed 2019]. 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]. tutorialspoint, 2018. tutorialspoint. [Online]

Available at: https://www.tutorialspoint.com/java/java_polymorphism

[Accessed 2019]. tutorialspoint, 2018. tutorialspoint. [Online]

Available at: https://www.tutorialspoint.com/design_pattern/singleton_pattern

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

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

Advance Programming S.M.N.M Samarakoon Batch-29

46

Advance Programming S.M.N.M Samarakoon Batch-29

Download