In functional programming language, we can use functions and key params to get rid of conditional branches. That mean use functions with condition param instead of "if esle". See example 3.
As computeSphereArea({radius:25.55})
Example 1: OOP
// in OOP(use java for example(sourceCode from: http: //developer.51cto.com/art/200907/136506.htm)):
public abstract class Shape {
// ...
public abstract void computeArea();
public abstract void computeVolume();
public abstract double getArea();
public abstract double getVolume();
}
public class Circle extends CircleShape2 {
// ...
double volume = 0.0; //
public void computeArea() { //
area = Math.PI * radius * radius;
}
public double getArea() {
return area;
}
public void computeVolume() {} //
public double getVolume() {
return volume;
}
}
public class Sphere extends Circle {
// ...
public void computeArea() { //
super.computeArea(); //
area = 4 * area;
}
public void computeVolume() { //
super.computeArea(); //
volume = 4.0 / 3 * radius * area;
}
}
public class CircleShapeApp {
public static void main(String[] args) {
Circle circle = new Circle(12.98);
Sphere sphere = new Sphere(25.55);
Shape shape = circle; //
//
shape.computeArea();
shape.computeVolume();
System.out.println("circle area: " + shape.getArea());
System.out.println("circle volume: " + shape.getVolume());
//
shape = sphere;
shape.computeArea();
shape.computeVolume();
System.out.println("Sphere area: " + shape.getArea());
System.out.println("Sphere volume: " + shape.getVolume());
}
}
Example 2: functional like oop.
//in functional programming(use javascript for example):
function initShape(v) {
var shape = {};
v = v || {};
if (typeOf(v, 'object') === true) {
shape.volumne = v.volumne || 0.0;
shape.computeArea = v.computeArea || function() {};
shape.computeVolume = v.computeVolume || function() {};
shape.getArea = v.getArea || function() {};
shape.getVolume = v.getVolume || function() {};
}
return shape;
}
function initCircle(v) {
var circle = {};
v = v || {};
if (typeOf(v, 'object') === true) {
circle.volume = 0.0;
circle.radius = v.radius || 0.0;
circle.computeArea = v.computeArea || function() {
this.area = Math.PI * this.radius * this.radius;
};
circle.computeVolume = function() {};
circle.getArea = v.getArea || function() {
return this.area
};
circle.getVolume = v.getVolume || function() {
return this.volume
};
}
return initShape(circle);
}
function initSphere(v) {
var sphere = {}
v = v || {};
if (typeOf(v, 'object') === true) {
var circle = initCircle(v);
sphere = circle;
sphere.volume = v.volume;
sphere.computeArea = function() {
circle.computeArea();
this.area = 4 * circle.area;
}
sphere.computeVolume = function() {
circle.computeArea();
this.volume = 4.0 / 3 * this.radius * circle.area;
}
}
return initShape(sphere);
}
var circle = initCircle(12.98);
circle.computeArea();
circle.computeVolume();
console.log("circle area: " + circle.getArea());
console.log("circle volume: " + circle.getVolume());
var sphere = initShpere(25.55);
sphere.computeArea();
sphere.computeVolume();
console.log("sphere area: " + sphere.getArea());
console.log("sphere volume: " + sphere.getVolume());
// Although, this is not pure functional program example, but with functional interface, like initCircle() initSphere().You can create more functions like computeCircleArea() computeSphereArea() makes it more functional.
// PS:typeOf() is here:https://github.com/will-v-king/javascript-showMe
Example3:
Ok, let's make it more funcitonal:
/** in functional code shape became meaningless.
function initShape(v) {
var shape = {};
v = v || {};
if (typeOf(v, 'object') === true) {
shape = v.object || v.shape || shape;
shape.volumne = v.volumne || 0.0;
}
return shape;
}
function computeShapeArea(v){
}
function computeShapeVolume(v){
}
*/
function initCircle(v) {
var circle = {};
v = v || {};
if (typeOf(v, 'object') === true) {
circle = v.object || v.circle || circle;
circle.volume = 0.0;
circle.radius = v.radius || 0.0;
}
return initShape(circle);
}
function computeCircleArea(v){
var area;
v = v || {};
if(typeOf(v) === 'Object'){
var radius = v.radius || v.object.radius || v.circle.radius;
if(!typeOf(v,'undefined')){
area = Math.PI * radius * radius;
}
}
return area;
}
function computeCircleVolume(v){
return 0.0;
}
/**function initCircle and initSphere are not necessary. why? see the last line.*/
function initSphere(v) {
var sphere = {}
v = v || {};
if (typeOf(v, 'object') === true) {
var circle = initCircle(v);
sphere = circle;
sphere.volume = v.volume;
}
return initShape(sphere);
}
function computeSphereArea(v){
var area;
v = v || {};
if(typeOf(v) === 'Object'){
var radius = v.radius || v.object.radius || v.sphere.radius;
if(!typeOf(v,'undefined')){
area = 4 * computeCircleArea({radius:radius}); // **POINT** the same as :circle.computeArea(); this.area = 4 * circle.area;
}
}
return area;
}
function computeSphereVolume(v){
var volume;
v = v || {};
if(typeOf(v,'object') === ture){
radius = v.radius || typeOf(v.object, 'object') === true ? v.object.radius : typeOf(v.sphere, 'Object') === true ? v.sphere.radius : 0.0;
var circleArea = computeCircleArea({radius:radius});
if(typeOf(circleArea,'number')=== true){
volume = 4.0 / 3 * radius * computeCircleArea({radius:radius}); // **POINT** the same as: circle.computeArea(); this.volume = 4.0 / 3 * this.radius * circle.area;
}
}
return volume;
}
var circle = initCircle({radius:12.98});
console.log("circle area: " + computeCircleArea(circle) );
console.log("circle volume: " + computeCircleVolume(circle) );
var sphere = initShpere(25.55);
console.log("sphere area: " + computeSphereArea({radius:25.55}) );
console.log("sphere volume: " + computeSphereVolume({radius:25.55}) );
console.log("sphere object is unused.That means initSphere is also not necessary as initShape()");