OODP 1 Full Note
OODP 1 Full Note
public class A {
Class එක public ෙවන්න ඔනි.
private static A a; Private static variable එකක් තිෙයන්ෙන ඔනි.
Private constructor එකක් තිෙයන්න ඔනි.
private A() {}
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 එකක් තිබිම
අනිවරය ෙනාෙව.
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.
} }
}
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 {
}
}
class Sniper implements Gun {
@Override
public void fire() {
}
}
class Man {
private Gun gun;
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();
}