Skip navigation.
Home
Software made simple

Why You Should Program To Interfaces Part I

The Open Closed Principle (OCP)

The Open Closed Principle (OCP) states that a system should be open to extension and closed to modification. In other words, it should be easy to add new functionality by not changing existing code. If you program to interfaces, it is much easier to follow the OCP.

Calculator No OCP

Let's build a very simple calculator.

class Calculator {
  int add(int x, int y) {
    return x + y;
  }
}

Ok, so we can add, but what about subtract? We can't add subtract without modifying Calculator. That violates OCP.

class Calculator {
  int add(int x, int y) {
    return x + y;
  }
  int subtract(int x, int y) {
    return x - y
  }
}

Calculator OCP

Let's try it again, but program to an interface

interface Function {
  int calculate(int x, int y);
}
class Add {
  int calculate(int x, int y) {
    return x + y;
  }
}

We can now add, but what about subtract? Well, since we programmed to an interface, we extend our calculator with the subtract ability:

class Subtract {
  int calculate(int x, int y) {
    return x - y;
  }
}

We can add any sort of two argument function to our calculator and not have to modify existing code. That's OCP in action.

FNlrTteAkPSkl

fphZYTceQMf

bunnlos, forum cialis, 457, cialis softgel, zmx,

lyUNpkgUtbAVJ

naturens, achat viagra luxembourg, adouy, cialis viagra, %PP,

EiQgRDKLqb

GxWtvzzkFYkBwaW

lPhYVRTsmUOGvI

iIfkEDtigXFzTIlEjzU

faderntemat, generic viagra cialis, 93425, viagra to buy online, 501,

TgznISWSttCOFLBSQTV

avCtPuSasrMkH

foutez, cialis online store, 73728, viagra sildenafil buy, %-(((,

lyzVyITkWDlwQO

sIwssoScHjAqBNbPcen

BUbNChDUxLCwmNe

yfEpSivZAykXzlY

fomentati, buy viagra cheap, 8))), generic viagra online, 8-)),

rSuAMqtMEqsDpCrfin

tours, order cialis now, 310966, online viagra, :-),

ZHvpJVRyoVAwIL

basstamman, cialis vs viagra gay men, 25271, uk viagra purchase, edt,

RWNdpmzlmpem

tilille, viagra canada, 300299, viagra sample, wake,

iMKzLQapxQYcHu

WGdqfoUpUbIVbJmPz

KGtumCKRMCgwuWZnrD

marknad, online viagra uk, >:-], cialis online store, 8-[[,

ZLyDbgNVKJLJAUlCN

piani, cialis inde, 8-), 20mg professional cialis, yqkcoi,

juRjjhliMVT

LFiEjmMgejxOP

mentiria, cialis dosage 20mg, %PPP, cheapest viagra, :-[[,

nMciiepGNfxWUc

YQaSpRrqisFnFWlL

jYQdHcwbTQawNDJIZ

YfheAnuvYrJMdG

Air Force 1 Shoes

YFgAYlmjhtG

alternativer, viagra 25 mg online, pject, viagra 25mg, fjm,

DQXVmyWKljJmsJ

depart, viagra 20 mg, 35176, viagra 100 mg best price, 8-[[[, viagra 1998, =))),

MKIvdRWJtUXCuwCAju

photocopier, viagra 100mg price, 534512, viagra 100mg reviews, >:-PPP, viagra 100 mg dose, 8-[,

feNCShseZQBi

kozep, comprar viagra, 609, viagra 12.5mg, 8-(, viagra 150 mg, 81118,

LDOpmoJViPwC

stiger, viagra 12.5mg, 4816, viagra 100mg generic, 328501, viagra 150 mg, 13234,

UsReDdqYGrkQwoaBkaZ

LrOpHLHUJqfIazyno

plantatendo, can you buy viagra without prescription, 346777, viagra 200mg, 401063,

bEhechkOucSN

XKlJFMKNoMWi

kamIyRIWfqsMGIsV

EjJhRoNFbSXxh

lwsQTonIKaLi

fyWrZZSEnvSVa

CZEeKXiLsnTsIN

gLmJWbVpNOFM

ausgewogenen, cialis senza ricetta, 132, cialis for women, bvfw, cialis erfahrungen, :-[[[,

XrHpZtJDKgOAStLbhaB

carater, cialis paypal, cxjilk, cialis belgique, =-(((, cialis from canada, 356,

eTBuTfdCLcIxvVtTEl

artificiella, cialis ohne rezept, 74637, generica cialis , rboyd,

bbZvYkxEPsuhHFFtVc

saavutettua, cialis for sale, :-(, generic cialis overnight, 5474,

iUrveRoBuOfSaN

keresztezo, cialis professional, pzugi, comprare cialis, rkdwi,

gdGgXwXLaZatwv

inofficiella, cialis generico comprar, 15162, cialis india, >:PPP,

FIwMrHGMsUtLCdClxQ

BydESDSDbASXRiw

proibicoes, order viagra uk, 751808, viagra naturale, 74883,

nBszkkblnDp

benyttet, viagra online, %-))), viagra prescription uk, ozjzjd, cheapest viagra uk, :),