Module 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Module 3

Integrative Coding
At the end of this module you are expected to:
• Defined the importance of using design patterns and list the motivation for using
each of the following design patterns.
• Described what a programming interface is and why it is important to
programming and give an example of where the use of a programming interface
simplified the development of a system.
• Defined the concept of inheritance and describe how it can be applied to
encourage code reuse.
• Designed an abstract class and use inheritance to create a class that extends the
abstract class.

Introduction

Design patterns

• A lower level framework for structuring an application than architectures


(sometimes called micro-architecture)
• Reusable collaborations that solve sub problems within an application

Why Design Patterns?

• Design patterns support object-oriented reuse at a high level of abstraction


• Design patterns provide a “framework” that guides and constraints object-
oriented implementation
The group help classify how the patterns are used

1. Creational patterns: used to help make a system independent of how its objects
are created, composed and represented.
2. Structural patterns: concerned with how classes and objects are organized and
composed to build larger structures.
3. Behavioral patterns: used to deal with assignment of responsibilities to objects
and communication between objects.

Creational Patterns

• Abstract Factory – create instances of other objects


Eg: creating GUI components for different GUI toolkits
• Factory Method – common interface for creating subclasses
• Singleton – create only one instance of a class
Structural Patterns

• Decorator – add more responsibilities to an object dynamically


Eg: adding scrolling to a text view
• Façade – higher level unified interface to a set of objects in a subsystem
• Proxy – interface layer between objects

Behavioral Patterns

• Iterator – a means to access all the elements of objects sequentially


• Momento – capture and save the current state of an object
• Observer – when any numbers of objects (the observers) need to be notified
automatically

INTERFACES

Application Programming Interfaces

• Are sets of requirements that govern how one application can talk to another
• Applications to share data and take actions on one another’s behalf without
requiring developers to share all of their software’s code
• Define exactly how a program will interact with the rest of the software world –
saving time and resources
Examples:
System-level APIs. Cut and paste LibreOffice document into an Excel
spreadsheet
Facebook APIs. Facebook users sign into many apps and websites using
their Facebook ID
Web APIs. Games let players chat, post high scores and invite friends to
play via Facebook, right there in the middle of the game.
Why And When To Use Interfaces?

1) To achieve security - hide certain details and only show the important details of an
object (interface).

2) Java does not support "multiple inheritance" (a class can only inherit from one
superclass). However, it can be achieved with interfaces, because the class
can implement multiple interfaces. Note: To implement multiple interfaces, separate
them with a comma (see example below).

Multiple Interfaces

To implement multiple interfaces, separate them with a comma:


Example

interface FirstInterface {

public void myMethod(); // interface method

interface SecondInterface {

public void myOtherMethod(); // interface method

class DemoClass implements FirstInterface, SecondInterface {

public void myMethod() {

System.out.println("Some text..");

public void myOtherMethod() {

System.out.println("Some other text...");


}

class MyMainClass {

public static void main(String[] args) {

DemoClass myObj = new DemoClass();

myObj.myMethod();

myObj.myOtherMethod();

INHERITANCE

• Derive a new class based on an existing class, with modifications or extensions


• A subclass inherits all the variables and methods from its super classes including
its immediate parent as well as all the ancestors
• Avoid duplication and reduce redundancy

Types of Inheritance

Simple, Multilevel, Multiple, Hierarchical and Hybrid

Inheritance and Abstract Class

• Abstract Method: a method with only signature (i.e.,the method name, the list of
arguments and the return type) without implementation (i.e., the method’s body)
• Use the keyword abstract to declare and abstract method
Abstract Class

• A class containing one or more abstract method is called abstract class


• Must be declared with a class -modifier abstract
• Provides a template for further development

Notes:

• An abstract method cannot be declared final, as final method cannot be


overridden
• An abstract method must be overridden in a descendent before it can be used.
• An abstract method cannot be private (which generate a compilation error,
because private method is not visible to the subclass and thus cannot be
overridden)

Example for Inheritance


Example for Abstract Class and Inheritance

Abstract Class and Inheritance in JAVA


Interfaces and inheritance in polymorphism
We are focusing on the relationship between polymorphism and inheritance. The main
thing to keep in mind is that polymorphism requires inheritance or interface
implementation. You can see this in the example below, featuring Duke and Juggy:

public abstract class JavaMascot {

public abstract void executeAction();

public class Duke extends JavaMascot {

@Override

public void executeAction() {

System.out.println("Punch!");

public class Juggy extends JavaMascot {

@Override
public void executeAction() {

System.out.println("Fly!");

public class JavaMascotTest {

public static void main(String... args) {

JavaMascot dukeMascot = new Duke();

JavaMascot juggyMascot = new Juggy();

dukeMascot.executeAction();

juggyMascot.executeAction();

The output from this code will be:

Punch!

Fly!

Because of their specific implementations, both Duke and Juggy’s actions will be
executed.

Another example:
class Animal {

public void animalSound() {

System.out.println("The animal makes a sound");

class Pig extends Animal {

public void animalSound() {

System.out.println("The pig says: wee wee");

class Dog extends Animal {

public void animalSound() {

System.out.println("The dog says: bow wow");

class MyMainClass {

public static void main(String[] args) {

Animal myAnimal = new Animal(); // Create a Animal object

Animal myPig = new Pig(); // Create a Pig object

Animal myDog = new Dog(); // Create a Dog object


myAnimal.animalSound();

myPig.animalSound();

myDog.animalSound();

Interfaces

Another way to achieve abstraction in Java, is with interfaces.

An interface is a completely "abstract class" that is used to group related methods with
empty bodies:

Example

// interface

interface Animal {

public void animalSound(); // interface method (does not have a body)

public void run(); // interface method (does not have a body)

}
To access the interface methods, the interface must be "implemented" (kinda like
inherited) by another class with the implements keyword (instead of extends). The body of
the interface method is provided by the "implement" class:

Example

// Interface

interface Animal {

public void animalSound(); // interface method (does not have a body)

public void sleep(); // interface method (does not have a body)

// Pig "implements" the Animal interface

class Pig implements Animal {

public void animalSound() {

// The body of animalSound() is provided here

System.out.println("The pig says: wee wee");

public void sleep() {

// The body of sleep() is provided here

System.out.println("Zzz");

}
class MyMainClass {

public static void main(String[] args) {

Pig myPig = new Pig(); // Create a Pig object

myPig.animalSound();

myPig.sleep();

You might also like