Sunteți pe pagina 1din 23

Lesson 5:

Writing Extensible Code


through Polymorphism
Principle of Least Action
© 2008 Maharishi University of Management, Fairfield, Iowa

All rights reserved. No part of this slide presentation may be reproduced or


utilized in any form or by any means, electronic or mechanical, including
photocopying or recording, or by any information storage and retrieval
system, without permission in writing from Maharishi University of
Management.
Lesson 5
Program for change

•Binding, early & late


•Polymorphism
•Open-closed principle
•Strategy Design Pattern
Wholeness
Wholeness Statement
Statement

The more abstraction you use in


your class design, the better your
program supports change. All
change is supported by the abstract
field of all the Laws of Nature.
Binding

Binding is the connecting a method body to a method call.

Static methods have early binding – the linkage is made


at compile-time.

Late binding, or dynamic binding, occurs at run-time.

The JVM method-call mechanism finds the correct method body


and invokes it at run-time.
Polymorphism

•Polymorphism = many forms


•Objects of a particular type can take many different forms
•Implemented with dynamic binding (late binding)
Up-casting
A
An object of type A, can be bound to
an object of type B or C
B C

class Student {…}


class Undergraduate extends Student {…}
class Graduate extends Student {…}
Student

Student st1,st2, st3;


Graduate st4; Undergraduate
Graduate
st1 = new Student(); // okay
st2 = new Undergraduate(); // okay
st3 = new Graduate(); // okay
st2 = new Graduate(); // okay
st4 = new Student(); // compilation error
(Q: why is this not allowed?)
Polymorphism example
<<Abstract>>
Account
Bank current_amount : double
accountnr : String
account slist : Vector
deposit()
addAccount ()
n withdraw()
delAccount()
setAcountnr()
addInterest_all_account s()
getAccountnr()
getAmount()
<<Abstract>> addInterest()

Checkingsaccount Savingsac count


int eres t_rate : double interest_rate : double

addInterest() addInterest()
public abstract class Account{
private long current_amount; public class Checkingsaccount
private String accountnr; extends Account {
private double interest_rate = 0.01;
public void deposit(long amount){
current_amount+=amount; public void addInterest(){
} deposit(getAmount()*interest_rate/2);
public void withdraw(long amount){ }
current_amount-=amount; }
}
public void setAccountnr(String anr){ public class Savingsaccount
accountnr=anr; extends Account {
} private double interest_rate = 0.0425;
public String getAccountnr(){
return accountnr; public void addInterest(){
} deposit(getAmount()*interest_rate);
public double getAmount(){ }
return current_amount; }
}
public abstract void addInterest();
}
public class Bank{
private Vector accountslist;

public void addInterest_all_accounts(){


Account account;
for (int x=0; x<accountslist.size(); x++){
account=(Account)accountslist.elementAt(x);
account.addInterest(); // POLYMORPHISM HERE
}
}
public void addAccount(String type, String accountnr){
Account account;
if (type.equals("checking")){
account = new Checkingsaccount();
}
else {
account = new Savingsaccount();
}
account.setAccountnr(accountnr);
accountslist.addElement(account);
}
public void delAccount(String accountnr){
Account account;
for (int x=0; x<accountslist.size(); x++){
account=(Account)accountslist.elementAt(x);
if (account.getAccountnr().equals(accountnr)){
accountslist.removeElement(account);
break;
}
}
}
}
public class application{
static public void main(String args[]){
Bank mybank = new Bank();
mybank.addAccount("checking", "1");
mybank.addAccount("checking", "2");
mybank.addAccount("savings", "3");

mybank.addInterest_all_accounts();
<<Abstract>>
} Account
} current_amount : double
Bank
accountnr : String
account slist : Vector
deposit()
addAccount ()
n withdraw()
delAccount()
setAcountnr()
addInterest_all_account s()
getAccountnr()
getAmount()
<<Abstract>> addInterest()

Checkingsaccount Savingsac count


int eres t_rate : double interest_rate : double

addInterest() addInterest()
Main
Main Points
Points
1. With polymorphism, objects of a
particular type can take many different
forms, giving us great power and
flexibility. The Unified Field is the source
of all different forms in the relative
creation.
Why do we want polymorphism?

It allows us to extend our program with new features without


changing existing (already tested)code.
<<Abst ract>>
Account
Bank current_amount : double
accountnr : St ring
account slist : Vect or
deposit()
addAccount ()
n withdraw()
delAc count()
setAcount nr()
addInterest_all_accounts()
getAccountnr()
getAmount()
<<Abst ract>> addInterest()

Checkingsaccount Savingsaccount Businessaccount


interest_rate : double interest_rate : double interest_rate : double

addInterest() addInterest() addInterest()


Open-Closed principle

•Software design should be open for extension, but closed for


modification.

•All systems change during their life cycle, but when a single
change results in a cascade of changes, the program becomes fragile
and unpredictable. When requirements change, you implement these
changes by adding new code, not by changing old code that already
works.

•See bank example: new accounts (credit card account, business


account) can be added without changing the other classes.

•Software modules can never be 100% closed for modification.


Programmer has to decide what aspects should be closed.
Example of applying the Open-closed principle: strategy pattern
If you have an object that uses different algorithms to compute the
same variable, then:
•Different algorithms may be applicable at different times.
•The same algorithm may be applicable in different objects.
•The algorithm may use data that the object doesn't need to know
about.
public class Account{
private double checkings_interest= 0.023;
private double savings_interest= 0.0415; Can not be extended
public void addInterest(){ Can not be reused
if (type.equals("Checkings")){
current_amount = current_amount* checkings_interest;
}
else {
current_amount = current_amount* savings_interest;
}
}
}
Account if (type.equals("Checkings")){
current_amount : double current_amount = current_amount* checkings_interest;
accountnr : String }
checkings_interest : double else {
savings-interest : double current_amount = current_amount* savings_interest;
}
deposit()
withdraw()
setAcountnr()
getAccountnr()
getAmount()
addInterest()

<<Abstract>>

Encapsulate the different Account


current_amount : double
algorithms by defining objects for accountnr : String

them (strategy objects). Objects


which use the algorithm hold a deposit()
withdraw()

reference to the strategy object. setAcountnr()


getAccountnr()
getAmount()
Use different algorithms by <<Abstract>> addInterest()

creating accounts with different


type of account objects. Checkingsaccount Savingsaccount
interest_rate : double interest_rate : double

addInterest() addInterest()
General technique:
•Abstract out the changing from the unchanging
•Put the unchanging in an abstract base class
•Encapsulate the changing aspect and place it in the subclass

Unchanging
+
Changing

unchanging

Changing 1 Changing 2
Main
Main Points
Points
2. The general technique used to apply the
strategy pattern is to abstract out the changing
from the unchanging, put the unchanging in an
abstract base class and encapsulate the
changing aspect in the subclass. Living a
successful life in the always changing relative
is possible through transcending to the most
abstract field, the never changing Unified Field
of all the Laws of Nature.
CONNECTING THE PARTS
OF KNOWLEDGE WITH THE
WHOLENESS OF
KNOWLEDGE
1. When requirements change, you should
implement these changes by adding new
code, not by changing old code that already
works.
2. The concept of variation-oriented
design underlies many OO principles.
One separates changing from non-
changing aspects of a class, and
delegates the responsibility for the
changing aspects to a class that can be
extended through polymorphism.
3. Transcendental Consciousness is the non-
changing field of all possibilities.

4. Wholeness moving within itself : in Unity


Consciousness one perceives the nonchanging
basis of Pure Consciousness in all change.

S-ar putea să vă placă și