Skip to content

Instantly share code, notes, and snippets.

@jpartogi
Last active March 15, 2017 04:53
Show Gist options
  • Save jpartogi/dd2b531a4ca03c00946c to your computer and use it in GitHub Desktop.
Save jpartogi/dd2b531a4ca03c00946c to your computer and use it in GitHub Desktop.
Bad OO Design in Java
/**
* @BankAccount.java
* BankAccount is a domain object (POJO) for bank account holder.
**/
public class BankAccount {
private String accountNumber;
private double accountBalance;
private static final double interestRate = 5;
public BankAccount(String accountNumber){
this.accountNumber = accountNumber;
}
// Do interest calculation business logic
public double calculateInterest(){
return (interestRate * accountBalance)/100;
}
// Save money into current account business logic
public double save(double amount){
return accountBalance + amount;
}
// Withdraw money from current account business logic
public double withdraw(double amount){
return accountBalance - amount;
}
public String getAccountNumber() {
return accountNumber;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public double getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
}
}
public class InterestCalculator {
}
/**
* @Shape.java
**/
public class Shape {
static final int CIRCLE = 1;
static final int RECTANGLE = 2;
int TYPE = 0;
private double radius;
private double width;
private double length;
public double area() {
switch(TYPE){
case CIRCLE:
return Math.PI * radius;
case RECTANGLE:
return width * length;
default:
return 0;
}
}
public void createCircle(double radius) {
this.radius = radius;
this.TYPE = CIRCLE;
}
public void createRectangle(double width, double length){
this.width = width;
this.length = length;
this.TYPE = RECTANGLE;
}
}
/**
* @Shape.java
**/
public interface Shape {
public double area();
}
/**
* @Rectangle.java
**/
public class Rectangle implements Shape {
private double width;
private double length;
private Rectangle(){}
public Rectangle(double width, double length){
this.width = width;
this.length = length;
}
public double area(){
return this.width * this.length;
}
}
/**
* @Square.java
**/
public class Square extends Rectangle {
private double edge;
public Square(double edge) {
super(edge, edge);
this.edge = edge;
}
}
public interface Vehicle {
void fly() throws Exception;
void drive() throws Exception;
}
public class Car implements Vehicle {
@Override
public void fly() throws Exception {
throw new Exception("Uh oh cannot fly");
}
@Override
public void drive() throws Exception {
// implement drive here
}
}
public class Plane implements Vehicle {
@Override
public void fly() throws Exception {
// implement fly here
}
@Override
public void drive() throws Exception {
throw new Exception("not good for driving");
}
}
/**
* @IOutputter.java
**/
public interface IOutputter {
void print(String output);
}
/**
* @ConsoleOutputter.java
**/
public class ConsoleOutputter implements IOutputter {
public void print(String output) {
System.out.println(output);
}
}
/**
* @MockOutputter.java
**/
public class MockOutputter implements IOutputter {
public void print(String output){
// do nothing
}
}
/**
* @CommandInvoker.java
**/
public class CommandInvoker {
public void invoke(ConsoleOutputter outputter) {
outputter.print("Hello World");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment