Docsity

Understanding Object-Oriented Programming and Design Patterns - Prof. Gurung, Assignments of Programming Languages

Toggle sidebar
bg1
Contents
P1 Examine the characteristics of the object-orientated paradigm as well as the various class
relationships.....................................................................................................................................3
Object Oriented Programming (OOP).............................................................................................3
Objects and Classes......................................................................................................................3
Object.......................................................................................................................................3
Class.........................................................................................................................................3
Mains benefits of Object-Oriented Programming are;.................................................................4
Some main characteristics of OOP are;.......................................................................................4
Abstraction.......................................................................................................................................5
Encapsulation...................................................................................................................................5
Inheritance........................................................................................................................................6
Polymorphism..................................................................................................................................6
Method overriding.......................................................................................................................6
Method Overloading....................................................................................................................6
Class relationship.........................................................................................................................7
The class relationship exists in those characteristics of OOPs....................................................7
M1 Determine a design pattern from each of the creational, structural and behavioral pattern
types.................................................................................................................................................8
Design pattern..................................................................................................................................8
Creational Patterns...........................................................................................................................9
Structural Patterns............................................................................................................................9
Behavioral Patterns........................................................................................................................10
D1 Analyses the relationship between the object-orientated paradigm and design patterns.........11
Abstract factory pattern (Creational Patterns):..........................................................................12
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e

Partial preview of the text

Download Understanding Object-Oriented Programming and Design Patterns - Prof. Gurung and more Assignments Programming Languages in PDF only on Docsity!

Contents

D2: Analyze how a class diagram can be derived from a given code scenario using a UML tool M4 Reconcile the most appropriate design pattern from a range with a series of given scenarios. ........................................................................................................................................................ 58

  • relationships..................................................................................................................................... P1 Examine the characteristics of the object-orientated paradigm as well as the various class
  • Object Oriented Programming (OOP).............................................................................................
    • Objects and Classes......................................................................................................................
      • Object.......................................................................................................................................
      • Class.........................................................................................................................................
    • Mains benefits of Object-Oriented Programming are;.................................................................
    • Some main characteristics of OOP are;.......................................................................................
  • Abstraction.......................................................................................................................................
  • Encapsulation...................................................................................................................................
  • Inheritance........................................................................................................................................
  • Polymorphism..................................................................................................................................
    • Method overriding.......................................................................................................................
    • Method Overloading....................................................................................................................
    • Class relationship.........................................................................................................................
    • The class relationship exists in those characteristics of OOPs....................................................
  • types................................................................................................................................................. M1 Determine a design pattern from each of the creational, structural and behavioral pattern
  • Design pattern..................................................................................................................................
  • Creational Patterns...........................................................................................................................
  • Structural Patterns............................................................................................................................
  • Behavioral Patterns........................................................................................................................
  • D1 Analyses the relationship between the object-orientated paradigm and design patterns.........
    • Abstract factory pattern (Creational Patterns):..........................................................................
    • Composite Patters (Structural Patterns):....................................................................................
    • Observer pattern (Behavioral patterns):.....................................................................................
  • Presentation....................................................................................................................................
  • Part 2..............................................................................................................................................
  • P2 Design and build class diagrams using a UML tool.................................................................
    • Class Diagram............................................................................................................................
  • M2 Define class diagrams for specific design patterns using a UML tool....................................
  • Part 3..............................................................................................................................................
  • Screenshot......................................................................................................................................
  • M3 Develop code that implements a design pattern for a given purpose......................................
  • D3 Evaluate the use of design patterns for the given purpose specified.......................................
  • Part 4..............................................................................................................................................
  • pattern types................................................................................................................................... Discuss a range of design patterns with relevant examples of creational, structural and behavioral
    • Design Patterns..........................................................................................................................
      • Creational...............................................................................................................................
      • Structural................................................................................................................................
      • Behavioral..............................................................................................................................
  • justification of your choices........................................................................................................... D4 Critically evaluate a range of design patterns against the range of given scenarios with
    • Justification for selecting MVC pattern.....................................................................................
  • References......................................................................................................................................

Creation of an object as a member of a class is called instantiation. Thus, object is an instance of a class. The constituents of a class are −

  • A set of attributes for the objects that are to be instantiated from the class. Generally, different objects of a class have some difference in the values of the attributes. Attributes are often referred as class data.
  • A set of operations that portray the behavior of the objects of the class. Operations are also referred as functions or methods. Mains benefits of Object-Oriented Programming are;  Maintainable Object-Oriented Paradigm methods make code more maintainable. Identifying the source of errors becomes easier because objects are self-contained. The principles of good methods design contribute to a system’s maintainability.  Reusable since objects contain both data and functions that act on data, objects can be thought of as self-contained “boxes”. This makes it easy to reuse code in new systems.  Scalable Object-Oriented applications are more scalable than their structured programming roots. As an object’s interface provides a roadmap for reusing an object, it also provides you with all the information you need to replace the object without affecting others. This makes it easy to replace old and inefficient code with faster algorithms.  Secured As encapsulation hides the internal software code implementation insides a class and hides internal data of inside object it provides good security of data. Some main characteristics of OOP are;  Abstraction: only exposing high level public methods for accessing an object  Encapsulation: containing information in an object, exposing only selected information

 Inheritance: child classes inherit data and behaviors from parent class  Polymorphism: many methods can do the same task Abstraction Abstraction is to focus on the important and discard the irrelevant. In simple way it can be defined as making user interacts with only selected attributes and methods of an objects. It uses simplified high-level tools to access a complex object. For an example; You don’t have to know all the details of how the engine works to drive a car. A driver only uses a small selection of tools: like gas pedal, brake, steering wheel, blinker. The engine is hidden from the driver. The benefits of abstraction are summarized below:  Simple, high level user interfaces  Complex code is hidden  Security as only displaying selected pieces of data, and only allowing data to be accessed through classes and modified through methods  Easier software maintenance  Code updates rarely change abstraction Encapsulation Encapsulation can be simply defined as containing all the important information inside an object and only exposing selected information to the other. Attributes and behaviors are defined by code inside the class template. It hides the internal software code implementation inside class and hides internal data if inside objects. For an example; Let’s use a car as a metaphor for encapsulation. The information the car shares with the outside world, using blinkers to indicate turns, are public interfaces. In contrast, the engine is hidden under the hood. The benefits of encapsulation are summarized here:  Adds security: Only public methods and attributes are accessible from the outside

The benefits of Polymorphism are:  Objects of different types can be passed through the same interface  Method overriding  Method overloading

Class relationship

This reusability is possible due to the relationship b/w the classes. Object-oriented programming generally supports 4 types of relationships that are: inheritance, association, composition, and aggregation. All these relationships are based on “is a” relationship, “has-a” relationship, and “part-of” relationship.

The class relationship exists in those characteristics of OOPs

Inheritance is an “IS-a” type of relationship. The “IS-A” relationship is based on inheritance which can be of two type Class Inheritance or Interface Inheritance. Inheritance is a part-child relationship where we create a new class by using the existing class code. It is just like saying that “A is a type of B”. for example, is “Apple is fruit”, “Lamborgini is a car”. Composition is a "part-of" relationship. Simply composition means mean the use of instance variables that are references to other objects. In a composition relationship, both entities are interdependent of each other for example “engine is part of the car”, “heart is part of the body”. Association is a “has-a” type relationship. Association establishes the relationship b/w two classes using their objects. Association relationships can be one to one, One to many, many to one, and many to many. For example, suppose we have two classes then these two classes are said to be a “has-a” relationship if both of these entities share each other’s object for some work and at the same time they can exist without each other’s dependency or both have their lifetime. Aggregation is based is on a "has-a" relationship. An aggregation is a special form of association. In association, there is not any classes (entity) work as the owner but in aggregation one entity work as owner. In aggregation, both entities meet for some work and then get separated. Aggregation is a one-way association.

Generally, these four types of relationships (Inheritance, Composition, Association, and Aggregation) are used in OOPS. If you have any doubts or questions then mention them in the comment section. M1 Determine a design pattern from each of the creational, structural and behavioral pattern types. Design pattern A design pattern is a general reusable solution to a commonly occurring problem within a given context. What does that mean? In simple terms, programmers often encounter the same problem repeatedly. Rather than have everyone come up with their own solution to common programming issues, we use a best practice type solution that has been documented and proven to work. It is a template or description of how to solve problems that can be used in many situations. Here the word general is important as we cannot just copy and paste a design pattern into our code. (sourcemaking,

Benefits of using Design pattern in application development;  It uses object-oriented concepts like decomposition, inheritance and polymorphism.  00Appropriate design patterns used in development of applications make development fast and easily documented.  The benefit of design patterns lies in reusability and extensibility of the already developed applications.  They are more sophisticated and advance approaches than basic data structures such as arrays, linked lists, and binary trees.  These are more sophisticated and sophisticated approaches than basic data structures such as arrays, linked lists, and binary trees They improve software development process. There are lots of design pattern that are currently being discovered among them most used common three design pattern are as follow;

As a simple example, consider how multiple inheritance mixes two or more classes into one. The result is a class that combines the properties of its parent classes. This pattern is particularly useful for making independently developed class libraries work together.  Adapter Match interfaces of different classes  Bridge Separates an object’s interface from its implementation  Composite A tree structure of simple and composite objects  Decorator Add responsibilities to objects dynamically  Facade A single class that represents an entire subsystem  Flyweight A fine-grained instance used for efficient sharing  Private Class Data  Restricts accessor/mutator access  Proxy An object representing another object Behavioral Patterns Behavioral patterns describe interactions between objects and focus on how objects communicate with each other. They can reduce complex flow charts to mere interconnections between objects of various classes. Behavioral patterns are also used to make the algorithm that a class uses simply another parameter that is adjustable at runtime. Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. Behavioral patterns describe not just patterns of objects or classes but also the patterns of communication between them.  Chain of responsibility A way of passing a request between a chains of objects

Command Encapsulate a command request as an object  Interpreter A way to include language elements in a program  Iterator Sequentially access the elements of a collection  Mediator Defines simplified communication between classes  Memento Capture and restore an object's internal state  Null Object Designed to act as a default value of an object  Observer A way of notifying change to a number of classes  State Alter an object's behavior when its state changes  Strategy Encapsulates an algorithm inside a class  Template method Defer the exact steps of an algorithm to a subclass  Visitor Defines a new operation to a class without change D1 Analyses the relationship between the object-orientated paradigm and design patterns. In my view, design pattern and OOP are not necessarily related but it so happens that design patterns are common approaches for solving problems that come upon OOP. Knowing about design patterns can save some time when determining how to approach a situation. In the Java and .Net frameworks programmers use design pattern all the time without ever studying them since they are extensively used. As just like a recipe book doesn’t make one master chef, knowledge of OO design pattern does not necessarily make one an OOP expert so I would not go so far to say that someone who knows design patterns is an expert OOP though.

respond to the click of a mouse button, or keyboard, which will trigger the execution of an application or utility and redraw the screen appropriately. The mouse click event will result in one or more objects responding. Each object is otherwise independent. Each object is able to respond only to certain events. The advantage of the pattern is that the Subject and Observer are independent of each other, and the subject does not need to know anything about the handling of the notification by the observers (i.e., how update () works). This means that any type of broadcasting communication could be implemented in this way. Presentation

Part 2 P2 Design and build class diagrams using a UML tool Importance of UML UML diagrams may be used to illustrate a project before it begins or as documentation for a project after it has begun. However, the overarching objective of UML diagrams is to assist teams to envision how a project is or will operate, and they may be utilized in any industry, not just software engineering. The diagrams will allow teams to collaborate on visualizing how a