0% found this document useful (0 votes)
11 views17 pages

OODP 1 Full Note

Uploaded by

DIDUL ADEESHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

OODP 1 Full Note

Uploaded by

DIDUL ADEESHA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

අපි ළඟ තියන class එකකට එක object එකයි, හැෙමාමට use

කරන්න පුලුවන් ෙවන්න ඔනි.

public class A {
Class එක public ෙවන්න ඔනි.
private static A a; Private static variable එකක් තිෙයන්ෙන ඔනි.
Private constructor එකක් තිෙයන්න ඔනි.
private A() {}

public static A getA() {


if (a == null) {
a = new A();
} Return type
return a;
}
} A.getA කියලා access කරන්න පුලුවන්.
class Test {
● Row 3 ක් ෙවන් කරන් හදගන්න ඔනි.
public static void main(String[] args) {
● First row එෙක class name,interface name, abstract class name ලියන්න ඔනි.
A a1 = A.getA(); ● Second row එෙක variables ලියන්න ඔනි.
A a2 = A.getA(); ● Third row එෙක method ලියන්න ඔනි.
● Underline කෙරාත් static Underline කෙර නැත්තාම් instances.
System.out.println("a1==a2"); ● (-) Mark private (+) Mark public
● Colon (:) එකක් දලා තමා data type එක ෙදන්ෙන.
}
● Final keyword එකට Underscore(-) ෙමම symbolඑෙකන් ෙපන්නවයි.
} ● Abstract class Italic type කරලා ෙපන්න ඔනි.
class A {
public void a1() {
System.out.println("A.a1");
} Complex code එකක් අනිත් අයට use කරන්න පුලුවන්
විදිහට simplified කරන එක
public void a2() {
System.out.println("A.a2");
}
}
class B {
public void b1() {
System.out.println("B.b1");
}
}
class C {
public void c1() {
System.out.println("C.c1");
}
}
class X {
private A a;
private B b;
private C c;

public X() {
a = new A();
b = new B(); Call ෙවන්න ඔනි පිලිවල ෙම method එක
c = new C(); ඇත.
}
public void x1(){
a.a1();
b.b1();
c.c1();
a.a2();
}
}
class Test {
public static void main(String[] args) {
X x = new X();
x.x1();
}
}
interface Strategy {
public boolean check(String text); Strategy Design Pattern
} Interchangeable: Makes algorithms interchangeable without changing the code that uses them.
class SahanStrategy implements Strategy {
public boolean check(String text) {
Flexible: Lets you switch algorithms at runtime based on the situation
return text.contains("j");
}
} ● Runtime එෙකදි කැමති Strategy එකක්
class KasunStrategy implements Strategy { call කරන්න පුලුවන්
public boolean check(String text) {
boolean b = false;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == 'j') {

}
}
return b;
}
}
class Context {
private Strategy strategy;
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public boolean useStrategy(String text) {
strategy.check(text);
}
}
class Test {
public static void main(String[] args) {
String text = "Hello Java";
Context context = new Context();
context.setStrategy(new SahanStrategy());
System.out.println("SahanStrategy result: " + context.useStrategy(text));
context.setStrategy(new KasunStrategy());
System.out.println("KasunStrategy result: " + context.useStrategy(text));
}
} Parameter
useStrategy
Interfaces එකක් තිබිම
අනිවරය ෙනාෙව.

● compatible නැති ෙදන්ෙනක් compatible කරන්ෙන adapter එෙකන්


class Laptop { class Adapter implements SD {
private SD sd;
public void setSd(SD sd) {
private MicroSD microSD;
this.sd = sd;
} public Adapter(MicroSD microSD) {
public void viewFile(SD sd) { this.microSD = microSD;
this.sd.readSDCard(); }
} @Override
} public void readSDCard() {
interface SD { this.microSD.readMicroSD();

public abstract void readSDCard(); }


} }
class SonySD implements SD {
@Override
public void readSDCard() { class Test {
System.out.println("Reading Sony SD public static void main(String[] args) {
Card"); Laptop laptop = new Laptop();
}
} SonySD sonySD = new SonySD();
interface MicroSD { laptop.setSd(sonySD);
public abstract void readMicroSD(); laptop.viewFile();
}
class SamsungMicroSD implements MicroSD { SamsungMicroSD samsungMicroSD = new SamsungMicroSD();
@Override //laptop.setSd (samsungMicroSD);
public void readMicroSD() {
Adapter adapter = new Adapter(samsungMicroSD);
System.out.println("Reading Micro Sd
Card"); laptop.setSd(adapter);
} laptop.viewFile();
}
} } Adapter Is-A relationship sd
Adapter Has A relationship microsd
Factory Design Pattern
Object Creation: Encapsulates object creation to avoid exposing the instantiation logic to the client.
Decoupling: Decouples the client code from the specific classes it needs to instantiate. Directly class වලි object එකක් හදන එක තහනම් කරලා, factory
එකකින් logic එකක් ඇතිව object සැදිම.
interface Gun {
public abstract void fire();
}

class AK47 implements Gun {


@Override
public void fire() {
}
}

class Sniper implements Gun {


@Override
public void fire() {
}
}

class GunFactory {
public Gun makeGun(String name) {
if (name.equals("AK47")) {
return new AK47();
} else if (name.equals("Sniper")) {
return new Sniper();
} else {
return null;
}
}
}

class Test {
public static void main(String[] args) {
GunFactory gunFactory = new GunFactory();
Gun g1 = gunFactory.makeGun("AK47");
g1.fire();
Gun g2 = gunFactory.makeGun("Sniper");
g1.fire();
}
}
interface DataBase { Proxy Design Pattern
public abstract void search(String Query); Control Access: The proxy controls and manages access to the real object.
}
class RealDatabase implements DataBase {
Not Directly Access
@Override
public void search(String query) {
System.out.println("Database search" + query);
}

��
}
class ProxyDatabase implements DataBase {
Real Proxy
private RealDatabase realDatabase;
private String password; Object Object
public ProxyDatabase(String password) {
this.password = password;
this.realDatabase = new RealDatabase();
Client
} The real object can be accessed through the proxy object.
@Override
public void search(String query) {
if (authenticate()) {
this.realDatabase.search(query);
} else {
System.out.println("Access Denied!");
}
}
private boolean authenticate() {
if (this.password.equals("123")) {
return true;
} else {
return false;
}
}
}
class Test {
public static void main(String[] args) {
ProxyDatabase proxyDatabase = new ProxyDatabase("123");
ProxyDatabase.search("SELECT * FROM product");
}
}
Template Method Design Pattern
Algorithm Framework: Sets up the main steps of an algorithm, letting subclasses fill in the details.
Consistency: Keeps the core structure of the algorithm the same, preventing changes to key parts.
abstract class A {
public final void process() {
m();
n();
p();
}
private void m() {
System.out.println("m");
}
Child abstract class එක විතරයි ෙවනස්
public abstract void n(); කරන්න පුලුවන්.
private void p() {
System.out.println("p");
}
}

class X extends A {
@Override
public void n() {
System.out.println("X.n");
}
}

class Test {
public static void main(String[] args) {
A a = new X(); ● Algorithm එක design කරන class එක abstract class එකක් ෙවන්න
a.process(); ඔනි.
} ● Algorithm එක ලියන method එක final ෙවන්න ඔනි.
}
abstract class A {
class Orangejuice extends A {
@Override
public final void MakeFoodjuiceprocess() {
protected void foodSelect() {
System.out.println("Select Orange");
foodSelect();
}
if (canAddingridition()) {
Addingridition();
@Override
protected void Addingridition() {
}
System.out.println("Add Water Mint");
blend();
}
Serve();
}
}
protected abstract void foodSelect();
class Watermelon extends A {
@Override
protected abstract void Addingridition();
protected void foodSelect() {
System.out.println("Select Watermelon");
private void blend() {
}
System.out.println("blend");
@Override
}
protected void Addingridition() {
private void Serve() {
System.out.println("Add Water Mint");
System.out.println("Serve");
}
}
@Override
public boolean canAddingridition() {
public boolean canAddingridition() {
return true;
return false;
}
}
}
}
class Applejuice extends A {
@Override
class Test {
protected void foodSelect() {
public static void main(String[] args) {
System.out.println("Select Apple");
A a = new Applejuice();
}
a.MakeFoodjuiceprocess();
@Override
A a1 = new Orangejuice();
protected void Addingridition() {
a1.MakeFoodjuiceprocess();
System.out.println("Add Water Mint");
A a2 = new Watermelon();
}
a2.MakeFoodjuiceprocess();
}
}
}
Iterator Design Pattern
Sequential Access: Allows you to access elements one by one in a collection.
Separate Traversal Logic: Keeps the logic of how to traverse a collection separate from the collection itself.

● ඔනිම collection එකක් read කරන්න ෙපාදු model එක තමා


iterator එෙකන් කරන්ෙන.

● ෙම Pattern එක වැදගත් ෙවන්ෙන collection වලට විතරයි


Iterator Design Pattern
Sequential Access: Allows you to access elements one by one in a collection.
Separate Traversal Logic: Keeps the logic of how to traverse a collection separate from the collection itself.
import java.util.ArrayList;
class Test {
interface Iterator {
class ArrayListIterator implements Iterator public static void main(String[] args) {
public abstract boolean hasNext();
{
public abstract Object Next();
} // Array
private ArrayList list[]; String array1[] = {"Java", "PHP", "C#"};
private int index;
class ArrayIterator implements
Iterator { }
public ArrayListIterator(Object[] objects) //ArrayList
{ ArrayList<String> list1 = ArrayList < > ();
private Object array[];
this.list = list; list1.add("Java");
private int index;
} list1.add("PHP");
public ArrayIterator(Object[] array) list1.add("C#");
@Override
{
public boolean hasNext() { Iterable iterator1 = new
this.array = array;
return index < list.size(); ArrayIterator(array1);
}
} Iterable iterator2 = new
@Override ArrayListIterator(list1);
@Override
public boolean hasNext() {
public Object Next() { while(iterator1.hasNext()){
return index < Array.length;
return list.get[index++]; System.out.println(iterator1.next());
}
} }
@Override
} while(iterator2.hasNext()){
public Object Next() {
return Array[index++]; System.out.println(iterator2.next());
} }

} }
}
Observer Design Pattern
Defines a One-to-Many Dependency: When one object changes state, all its dependents (observers) are notified and
updated automatically
import java.util.ArrayList;
interface Observer {
interface Subject { public abstract void update(int x);
public void registerObserver(Observer observer); }
public void removeObserver(Observer observer);
public void notifyObserver(); class ConcreteObserver1 implements Observer {
}
class ConcreteSubject implements Subject { @Override
private int x; public void update(int x) {
private ArrayList<Observer> observerList; System.out.println("Observer 1;" + x);
public ConcreteSubject() { }
this.observerList = new ArrayList<>();
} }
@Override
public void registerObserver(Observer observer) { class ConcreteObserver2 implements Observer {
this.observerList.add(observer);
} @Override
@Override public void update(int x) {
public void removeObserver(Observer observer) { System.out.println("Observer 2;" + x);
this.observerList.remove(observer);
} }
@Override
public void notifyObserver() { }
for (Observer observer : observerList) {
observer.update(this.x); class Test {

} public static void main(String[] args) {


}
ConcreteSubject subject = new ConcreteSubject();
public void serX(int x) {
this.x = x; Observer observer1 = new ConcreteObserver1();
notifyObserver(); Observer observer2 = new ConcreteObserver2();
}
subject.registerObserver(observer1);
public int getX() { subject.registerObserver(observer2);
return x;
} }
} }
interface Gun { State Design Pattern
public abstract void fire();
}
Encapsulates State-Specific Behavior: Allows an object to alter its behavior when its internal state changes.
class AK47 implements Gun {
@Override
public void fire() {

}
}
class Sniper implements Gun {
@Override
public void fire() {

}
}
class Man {
private Gun gun;

public void setGun(Gun gun) {


this.gun = gun;
}

public void shoot() {


this.gun.fire();
}
}
class Test {
public static void main(String[] args) {
Man man = new Man();

Gun ak47 = new AK47();


Gun sniper = new Sniper();

man.setGun(ak47);
man.shoot();

man.setGun(sniper);
man.shoot();
}
}
Command Design Pattern
Decouples Sender and Receiver: Separates the object that invokes the operation from the one that knows how to perform it

interface Command {
public abstract void execute();
}

class BallaBuranawa implements Command {


private Balla balla;
public BallaBuranawa(Balla balla) {
this.balla = balla; class Invoker {
} private Command command;
public void setCommand(Command command) {
@Override this.command = command;
public void execute() { }
this.balla.buranava();
} public void invoke() {
} this.command.execute();
}
class BallaDuwanawa implements Command { }
private Balla balla;
public BallaDuwanawa(Balla balla) { public class Test {
this.balla = balla; public static void main(String[] args) {
} Balla receiver = new Balla();

@Override Command command1 = new BallaDuwanawa(receiver);


public void execute() { Command command2 = new BallaDuwanawa(receiver);
this.balla.duwanava();
} Invoker invoker = new Invoker();
}
invoker.setCommand(command1);
class Balla { invoker.invoke();
public void buranava() {
System.out.println("Balla Buranawa"); invoker.setCommand(command2);
} invoker.invoke();
public void duwanava() { }
System.out.println("Balla Duwanawa"); }
}
}

You might also like