Advanced
Programming
Class GCS0604
Name : Nguyễn Minh Đăng Khoa
Nguyễn Thái Bình
Huỳnh Thanh Đức
Introduce
Object-Oriented Programming
Abstract Class and Interface
The Unified Modeling Language
(UML)
Design and Patterns
Object-Oriented
Programming
Programming languages are based on two
fundamental concepts, data and ways to
manipulate data.
Object-oriented programming provides the
following features:
Abstraction
• Abstraction is the feature of extracting only the required information from
objects. For example, consider a television as an object. It has a manual stating
how to use the television. However, this manual does not show all the technical
details of the television, thus, giving only an abstraction to the user.
• Details of what a class contains need not be visible to other classes and objects
Encapsulation that use it. Instead, only specific information can be made visible and the others
can be hidden. This is achieved through encapsulation, also called data hiding.
Both abstraction and encapsulation are complementary to each other.
• Inheritance is the process of creating a new class based on the attributes and
methods of an existing class. The existing class is called the base class whereas the
Inheritance new class created is called the derived class. This is a very important concept of
object-oriented programming as it helps to reuse the inherited attributes and
methods.
• Polymorphism is the ability to behave differently in different situations. It is
Polymorphism basically seen in programs where you have multiple methods declared with the
same name but with different parameters and different behavior.
Classes and Objects
C# programs are composed of classes
that represent the entities of the
program which also include code to
instantiate the classes as objects.
When the program runs, objects are
created for the classes and they may
interact with each other to provide the
functionalities of the program
Class and Object
I have the following
example, the car is the
class, in that vehicle it
has attributes such as
red, 1 bridge 2 bridges,
and actions like
running, backing, and
winning are viewed as
a method.
Class
A class is an entity that determines how an object will
behave and what the object will contain. In other
words, it is a blueprint or a set of instruction to build a
specific type of object.
Object
An object is nothing but a self-contained component
which consists of methods and properties to make a
particular type of data useful. Object determines the
behavior of the class. When you send a message to an
object, you are asking the object to invoke or execute
one of its methods.
Abstract Class
C# allows designing a class specifically to be used as a
base class by declaring it an abstract class.
Such class can be referred to as an incomplete base
class, as it cannot be instantiated, but it can only be
implemented or derived.
An abstract class is declared using the abstract
keyword which may or may not contain one or more of
the following:
normal data member(s)
normal method(s)
abstract method(s)
Definition
An abstract class can implement methods that are
similar for all the subclasses.
Contents
Interface
An interface contains only abstract members that
cannot implement any method.
An interface cannot be instantiated but can only be
inherited by classes or other interfaces.
An interface is declared using the keyword interface.
In C#, by default, all members declared in an interface
have public as the access modifier.
Example
Interface: syntax
interface <InterfaceName>
{
//interface members
}
Example
interface IAnimal
{
void AnimalType();
}
Implementing an
Interface
An interface is implemented by a class in a
way similar to inheriting a class.
When implementing an interface in a class,
implement all the abstract methods declared
in the interface. If all the methods are not
implemented, the class cannot be compiled.
The methods implemented in the class should
be declared with the same name and
signature as defined in the interface.
The implementation of an
interface
Implementing an
Interface:
syntax
class <ClassName> : <InterfaceName>
{
//Implement the interface methods.
//Define class members.
}
Example
interface IAnimal {
void Habitat();
}
class Dog : IAnimal {
public void Habitat() {
Console.WriteLine("Can be housed with human beings");
}
static void Main(string[] args) {
Dog objDog = new Dog();
Console.WriteLine(objDog.GetType().Name);
objDog.Habitat();
}
The Unified Modeling
Language (UML)
The Unified Modeling Language (UML) is a
standard modeling language (language for
drawing diagrams) that allows developers to
express software designs using a visual notation
UML covers a huge range of design areas
Class Structure
State
User Interactions
Object Interactions
Few people probably know all of UML…
UML
UML is a standard that is maintained by the
Object Management Group (OMG)
Before UML, there were multiple competing
design methodologies:
Rumbaugh's OMT
Booch’s Method
Etc.
UML was created to unite the methodologies
UML Diagram
UML Class Diagrams
A UML class diagram captures the classes in
an application
UML class diagrams show inheritance
hierarchies
UML class diagrams show relationships
between classes
Containment
Inheritance
UML class diagrams do not show state,
sequencing of events, etc…
UML Classes
public class Car {
private string model;
Class
public string Name
manufacturer; //…poor OO
public void start() {…} Attributes
public void turn(int degrees)
{…}
}
Design patterns
A design pattern is a time-tested solution to
a common software problem
Patterns enable a common design vocabulary,
improving communication, easing
documentation
Patterns capture design expertise and allow
that expertise to be transferred
Pattern: Singleton
a class that has only one instance
Singleton pattern
singleton: an object that is the only object of
its type
ensures that a class has at most one instance
provides a global access point to that instance
takes responsibility of managing that instance
away from the programmer (illegal to construct
more instances)
provide accessor method that allows users to
see the (one and only) instance
possibly the most known / popular design
pattern! (this should tell you something)
Singleton diagram
Singleton Example