- Design Patterns
- ููุฑุณ ุงูุญุชููุงุช:
- Creational Design patterns:
- Structural Patterns:
- Beahvioral Patterns:
- ู ุตุงุฏุฑ ู ููุฏุฉ ๐พ
ุชุนุฑูู Design patterns: ูู ุนุจุงุฑุฉ ุนู ุญููู ูู ุดุงูู ู ุชูุฑุฑุฉุ ูุชุดูู ููุงุนุฏ ุฅุฑุดุงุฏูุฉ ูููููุฉ ุงูุชุนุงู ู ู ุน ุชูู ุงูู ุดุงูู ุงูู ุญุฏุฏุฉ.
ูู ููุณุช ุนุจุงุฑุฉ ุนู
ClassesููุงPackagesููุง ุฃู ู ูุชุจุงุช ุฃู ุฃููุงุฏ ุฌุงูุฒุฉ ุจุญูุซ ูุชู ุงุณุชุฎุฏุงู ูุง ู ุจุงุดุฑุฉ ุจุฏุงุฎู ุงูููุฏ ุงูุฎุงุต ุจูุ ูู ุงูู ูุงุจู ู ุง ูู ุฅูุง ุฃุณุงููุจ ู ุชุจุนุฉ ูุญู ุงูู ุดุงูู ุงูู ุชูุฑุฑุฉ ุงูุชู ูู ูู ุฃู ุชูุงุฌููุง ุฎูุงู ุนู ููุฉ ูุชุงุจุฉ ุงูููุฏ.
In software engineering, a software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. It is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations.- ูุง ุชุนุชุจุฑูุง ูู ุญููู ุณุญุฑูุฉ ููู ุดุงูู ุงูุชู ูุฏ ุชูุงุฌููุงุ ููู ููุณุช ุนุจุงุฑุฉ ุนู ููุงูุจ ุฌุงูุฒุฉ ูุชู ุงูุชุนุงู ู ู ุนูุง ุฅูู ุง ูู ุนุจุงุฑุฉ ุนู ุญููู ูุฌุจ ุนููู ุฃู ุชููููุง ุญุณุจ ุงูุจูุฆุฉ ูุงูู ุดููุฉ ุงููุชุงู ุชุฑูุฏ ุชุทุจูููุง ุนูููู .
- ุนูุฏู ุง ูุชู ุงุณุชุฎุฏุงู ูู ุจุงูุดูู ุงูุตุญูุญ ูุฅููุง ุชุนุชุจุฑ ู ููุฐุฉุ ุจุญูุซ ูุชู ุญู ุงูู ุดููุฉ ุจุงูุดูู ุงูู ูุงุณุจ ูุชุณุงูู ูู ุฅูุชุงุฌ ููุฏ ู ุฑุชุจ ูู ูุณู ุจุงูุดูู ุงูุตุญูุญุ ุจููู ุง ุงูุงุณุชุฎุฏุงู ุงูุฎุงุทูุก ููุง ูุคุฏู ุฅูู ููุถู.
- ุชููุฑ ุญููู ุนุงู
ุฉุ
documentedุจุดูู ู ุฌุฑุฏ ูุบูุฑ ู ุฑุชุจุท ุจุชูุงุตูู ู ุดููุฉ ู ุนููุฉ. - ุชุณุงูู ุจุชุณุฑูุน ุนู ููุฉ ุงูุชุทููุฑุ ุจุญูุซ ุชููุฑ ูู ุงุฐุฌ ุชุทููุฑ ู ุฌุฑุจุฉ ู ุณุจูุงู ูู ุซุจุฉ ุงููุนุงููุฉ.
- ุชุนุฏ ุงู
design patternsูุชุงุฌ ูุฏุฑุงุณุงุช ูุฎุจุฑุงุช ู ุทูุฑูู ุณุงุจูููุ ููุนุฏ ุงุณุชุฎุฏุงู ูุง ุฅูุฌุงุจูุฉ ุจุญูุซ ุฃูู ุชุณุชููุฏ ู ู ุชูู ุงูุฎุจุฑุงุช ุงูุฎุงุตุฉ ุจูู . - ุชู ูุน ู ู ุงููููุน ูู ู ุดููุงุช ุงูุฏูููุฉ ุงูุชู ุชุคุฏู ุฅูู ู ุดุงูู ุถุฎู ุฉ.
- ููุชุฌ ู
ู ุฎูุงููุง ููุฏ ุณูู ุงููุฑุงุกุฉุ ุฎุตูุตุงู ูุฏุง ุฃููุฆู ุงูุฐูู ูู
ุชูููู ู
ุนุฑูุฉ ุจุงู
design patternsุงูู ุณุชุฎุฏู ุฉ.
ูุฑุฌุน ุงููุถู ุงูุฃูุจุฑ ูู ูุฐุง ุงูุงูุชุดุงู ูู
ุฌู
ูุนุฉ ุชุณู
ู ุจ The Gang of Four (GoF) ุญูุซ ูุงู
ูุง ุจุฅูุดุงุก ูุชุงุจ ุจุงุณู
: Design Patterns: Elements of Reusable Object-Oriented Softwareุ ูุดุฑุญ ูุฐุง ุงููุชุงุจ ุนู 23 design patterns ุจุงููุฅุถุงูุฉ ุฅูู ุฃู
ุซูุฉ ูุฃููุงุฏ ู
ูุชูุจุฉ ุจูุบุฉ c++.
ุชูุชู ุจุงูููููุฉ ุงูุชู ูุชู ู ู ุฎูุงููุง ุฅูุดุงุก ุงู Objects ู ุงู Classs.
ุชูุชู ุจุชูููู ุงู Objects ู ุงู Classes.
ุชูุชู ุจุงูุชูุงุนู ุจูู ุงู Objects ู ุงู Classes ุจุงูุฅุถุงูุฉ ุฅูู ุฃููุง ุชูุชู ุจุงูู ุณุคูุงููุงุช ุงูุฎุงุตุฉ ุจูู ู ููุง.
ุชูุถุญ ุงูุนูุงูุงุช ุจูู ุงู classes ู ุงู classes ุงููุฑุนูุฉุ ููุชู
ุชุนุฑูููุง ุจุดูู static.
ุชูุถุญ ุงูุนูุงูุงุช ุจูู ุงู Objects ุ ููุชู
ุชุนุฑูููุง ุจุดูู dynamic.
ุชููู ุจุชุฌุฑูุฏ ุนู ููุฉ ุฅูุดุงุก ุงู Objectsุ ู ู ุง ูุณุงุนุฏ ูู ุงูุญุตูู ุนูู ูุธุงู ู ุณุชูู ูู ููููุฉ ุงูุงูุดุงุกุ ุงูุชูููู ูุงูุนุฑุถ ูู Objects ุงูุฎุงุตุฉ ุจู.
- ุชุณุชุฎุฏู
ุงู Creational patterns ุงููุฑุงุซุฉ(
inhiretance) ููุชูุฑูู ุจูู ุงู classes ุงูุชู ูุชู ุฅูุดุงุฆูุง. - ูู ุงู Creational patterns ูุชู ุชูููู ุนู ููุฉ ุฅูุดุงุก ุงู Objects ุฅูู Class ุงุฎุฑ.
- Factory Method
- Abstract Factory
- Builder
- Prototype
- Singleton
In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system.ุชุฎูู ุจุฃููุง ูุฑูุฏ ุฅูุดุงุก ุจุฑูุงู ุฌ ูููู ุจุชูููุฏ ุฃุฑูุงู ุนุดูุงุฆูุฉุ ููู ูุง ุจุชุตู ูู Class ููููู ุจุชูู ุงููุธููุฉ:
public class Random {
private int seed = 0;
public int getRandInteger(int from, int to) {
// Pseudorandom-number-generator
this.seed++;
float a = seed * 15485863;
float v = (a * a * a % 2038074743) / 2038074743;
return (int) (v * (to - from) + from);
}
}ููู ุฌุฑุจูุง ุงูููุฏ ุงูุณุงุจู :
public static void main (String[] args)
{
Random rand1 = new Random();
System.out.println(rand1.getRandInteger(1, 5));
Random rand2 = new Random();
System.out.println(rand2.getRandInteger(8, 50));
Random rand3 = new Random();
System.out.println(rand3.getRandInteger(7, 9));
Random rand4 = new Random();
System.out.println(rand4.getRandInteger(-50, 50));
}ุณูููู ุงููุงุชุฌ:
2
19
7
-23ุนุธูู ุ ุฅุฐู ุงูููุฏ ูุนู ู ุจุดูู ุตุญูุญ! ุ ูููู ูู ุชู ุนููุง ููููุฉ ุณููุงุญุธ ุจุฅููุง ูุฌุฑุจ ุจุงุณุชุฎุฏุงู ูุชุฑุงุช ู ุฎุชููุฉ ูู ูู ู ุฑุฉุ ููุนุฏ ุงูุชุฌุฑุจุฉ ุจุงุณุชุฎุฏุงู ุฐุงุช ุงูููู :
public static void main (String[]args)
{
Random rand1 = new Random();
System.out.println(rand1.getRandInteger(1, 5));
Random rand2 = new Random();
System.out.println(rand2.getRandInteger(1, 5));
Random rand3 = new Random();
System.out.println(rand3.getRandInteger(1, 5));
}ูุตุจุญ ุงููุงุชุฌ:
2
2
2ุฌู
ูุน ุงููุชุงุฆุฌ ุฌุงุกุช ุจููุณ ุงููุชูุฌุฉ! ููุฐุง ูุฎุงูู ูุฏู ุงูุฐู ุตู
ู
ูู ุงู classุ ูู ุงููุงูุนุ ุงูููุฏ ุงูุณุงุจู ู
ุตู
ู
ุจุงุณุชุฎุฏุงู
ุฎูุงุฑุฒู
ูุฉ Pseudorandom-number-generator ูุงูุชู ุชุนุชู
ุฏ ุนูู ุงู seed ูุงูุชู ูุฌุจ ุฃู ูุชู
ุชุบููุฑ ููู
ุชูุง ููุญุตูู ุนูู ุงูุฑูู
ุงูุนุดูุงุฆู ูู ูู ู
ุฑุฉุ ูุฐูู ูุฌุจ ุฃู ูุชู
ุงูุชุนุงู
ู ู
ุน Object ูุงุญุฏ ููุท ู
ู ุงูููุงุณ Random ุฎูุงู ุงูููุฏ ุงูุฎุงุต ุจูุ ูุฐูู ูุงูุฃูุณุจ ูู ุงุณุชุฎุฏุงู
ุงู Singleton Design pattern ุจุญูุซ ูููุฑ ุฎุงุตูุฉ ุฃู ูุชู
ุฅูุดุงุก Object ูุญูุฏ ููุท ู
ู ุงู Class ุฎูุงู ุงูููุฏ ูุงู
ูุงู.
ูุฐููุ ูุจุนุฏ ุชุบูุฑ ูููููุฉ ุงูููุฏ ุจุงุณุชุฎุฏุงู Singleton Design Pattern:
ูู ูู ุชุญู ูู ุงูููุฏ ูุงู ูุงู ู ู ููุง
class Random {
private int seed = 0;
private static Random instance;
private Random() {
} // Empty private Constructor
public int getRandInteger(int from, int to) {
// Pseudorandom-number-generator
this.seed++;
float a = seed * 15485863;
float v = (a * a * a % 2038074743) / 2038074743;
return (int) (v * (to - from) + from);
}
public static Random getInstance() {
if (instance == null)
instance = new Random();
return instance;
}
}ูู ูููุง ุชูููุฐู ุจุงูุดูู ุงูุชุงูู:
public class Main {
public static void main(String[] args) {
Random rand = Random.getInstance();
System.out.println(rand.getRandInteger(1, 5));
System.out.println(rand.getRandInteger(8, 50));
System.out.println(rand.getRandInteger(7, 9));
System.out.println(rand.getRandInteger(-50, 50));
}
}ูุจูุฐุง ุงูุดูู ูุฏ ุญุตุฑูุง ุฃูู ุณูุชู ุชูููู Object ูุญูุฏ ู ู ุงู Random.
ูุชุฑูุจ ุฃู Class ู ุตู ู ุจ Singleton ู ู ุนุฏุฉ ุฃู ูุฑ ุฃุณุงุณูุฉ:
-
ูููู ุงู
Constructorุงูุฎุงุต ุจูprivateุ ุจุญูุซ ูู ูุน ุฃู ุนู ููุฉ ูุฅูุดุงุก Object ู ู ุฎุงุฑุฌ ุงู Class ููุณูุ ูุจูุฐุง ุงูุดูู ูุชุฃูุฏ ุฃู ุนู ููุฉ ุงูุฅูุดุงุก ุณุชุชู ุญุณุจ ุงููููุฏ ุจุฏุงุฎู ุงู Class ููุณู ููุท. -
ู ุชุบูุฑ ูุณุชุนู ู ูู
instanceููุชู ุชุฎุฒูู ุงู Object ุงููุญูุฏ ุจุญูุซ ูุชู ุฅุฑุฌุงุนู ุฐุงุชู ูู ุญุงูุฉ ู ุญุงููุฉ ุงูุฅูุดุงุก ู ุฑุฉ ุฃุฎุฑูุ ููููู ู ู ููุนPrivateูstatic. -
ุฏุงูุฉ
getInstance(ุฃู ุฃู ุจุฏูู ููุง) ููุชู ู ู ุฎูุงููุง ุงููุตูู ุฅูู ุงู instance ุงููุญูุฏุ ุจุญูุซ ูููู ููุน ุงูุจูุงูุงุช ุงูุชู ูุชู ุฅุฑุฌุงุนูุง ู ู ุงูุฏุงูุฉ ู ู ููุณ ููุน ุงู Class ุงูุฐู ูุญุชูููุงุ ููุฌุจ ุฃู ุชููู ู ู ููุนpublicูstatic.
Factory (EN) == ู ุตูุน (ุงูุนุฑุจูุฉ)
The factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory methodโeither specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classesโrather than by calling a constructor.ุชุฎูู ุฃู ููุงูู ู ุตูุน ูุตูุน ุงูููู ๐ุ ุจุญูุซ ูุฃุชู ุงูุฒุจูู ููุทูุจ ุงููุนูุฉ ุจุงููููุฉ ูุงูุดูู ุงููุฐุงู ูุฑูุฏูู ุงุ ูุจุนุฏ ุฃู ูุฌูุฒ ุทูุจู ูุณุชูู ู ู ู ุงูู ุตูุน.
ูู ูุฐุง ุงูุณููุงุฑููุ ูุง ูู ูู ุงูุฒุจูู ุฃู ููุฑุฉ ุนู ุงูููููุฉ ุงูุชู ุชู ุฅุนุฏุงุฏ ูุงูุจ ุงูุญููู ู ู ุฎูุงููุงุ ูู ู ุง ุจุงูุฃู ุฑ ุฃูู ูุงู ุจุฅุฑุณุงู ุทูุจู ููู ุตูุนุ ูุญุตู ุนูู ุงูุดูุก ุงูุฐู ุทูุจู.
ููุฐูู ุงูุฃู
ุฑ ุจุงููุณุจุฉ ูู Factory design pattern ุจุญูุซ ูุชู
ู
ู ุฎูุงููุง ุนุฒู ุงููุฉ ุฅูุดุงุก ุงู Object ุนู ุงู Client.
ูู ูุฐุง ุงููู
ุทุ ูุชู
ุฅูุดุงุก ุฏุงูุฉ ุชุณู
ู ุจุงูู
Factory Method
ุชููู ู
ุณุคููุฉ ุนู ุนู
ููุฉ ุงูุฅูุดุงุก ูููุงุฆู ุงูู
ุฑุงุฏุ ููุชู
ุจุฏุงุฎููุง ุชุญุฏูุฏ ุงูููุน ุงูู
ูุงุณุจ ูููุงุฆู ุงูุฐู ุณูุชู
ุฅูุดุงุฆู ุจูุงุก ุนูู ุงูุงุณููุจ ุงูุชู ุตู
ู
ุช ู
ู ุฎูุงููุ ูุฐุง ุงูุฃู
ุฑ ู
ููุฏ ุฌุฏุงูุ ุฎุตูุตุงู ูู ุญุงูุงุช ูููู ูููุง ุงุณููุจ ูุฑุงุซุฉุ ุจุญูุซ ูููู ูุฏู Super-class ููุฑุซ ู
ูู ุนุฏุฉ ู
ู ุงู Sub-Classes ูู ูุฐู ุงูุญุงูุฉ ุชููู ุงูุฏุงูุฉูู ู
ู ููุฑุฑ ุฃู ุงูุฃุจูุงุก ูู ุงูุฃูุณุจ ููุชู
ุฅูุดุงุก ุงููุงุฆู ู
ู ุฎูุงูู ุญุณุจ ุงูู
ุนุทูุงุช ุงูุชู ูุชู
ุชู
ุฑูุฑูุง ููุฏุงูุฉ.
Client:
ุงูู ูุตูุฏ ุจู ูู ุงูู ูุงู ุงูุฐู ุณูุชู ุงุณุชุฎุฏุงู ุงููุงุฆู ุฏุงุฎูู
ูุนูู ุณุจูู ุงูู ุซุงู ูุฏ ูููู ุงูู ูุตูุฏ ุจู ูู ุฏุงูุฉmain.
ูู ูู ุชุญู ูู ุงูููุฏ ูุงู ูุงู ู ู ููุง
ูู ุทุจููุง ุงูู ุซุงู ุงูุฐู ุชู ุฐูุฑู ุณุงุจูุงูุ ุจุดูู ุจุฑู ุฌูุ ุณูููู ุดููู ุจุงูุดูู ุงูุชุงูู:
public enum Shape {
CIRCULAR,
RECTANGULAR,
IRREGULAR
}
public enum Flavour {
CHOCOLATE,
VANILLA
}
public interface Cake {
public Shape getShape();
public Flavour getFlavour();
}ChocolateCake.java :
public class ChocolateCake implements Cake {
private Shape shape;
private Flavour flavour;
public ChocolateCake(Shape shape) {
this.shape = shape;
this.flavour = Flavour.CHOCOLATE;
}
@Override
public Shape getShape() {
return this.shape;
}
@Override
public Flavour getFlavour() {
return this.flavour;
}
}VanillaCake.java :
public class VanillaCake implements Cake {
private Shape shape;
private Flavour flavour;
public VanillaCake(Shape shape) {
this.shape = shape;
this.flavour = Flavour.VANILLA;
}
@Override
public Shape getShape() {
return this.shape;
}
@Override
public Flavour getFlavour() {
return this.flavour;
}
}ููู ูุฐุง ุงูู
ุซุงูุ ูููู ุงู Client ูุฏูุ ูู ุงู Main class :
public class Main {
public static void main(String[] args) {
Cake myChocolateCake = CakeFactory.makeCake(Flavour.CHOCOLATE, Shape.CIRCULAR);
Cake myVanillaCake = CakeFactory.makeCake(Flavour.VANILLA, Shape.RECTANGULAR);
System.out.println("The shape of the " +
myChocolateCake.getFlavour() +
" cake is " +
myChocolateCake.getShape());
System.out.println("The shape of the " +
myVanillaCake.getFlavour() +
" cake is " +
myVanillaCake.getShape());
}
}ูู
ุง ููุงุญุธุ ุชู
ูููุง ู
ู ุฎูุงู ุงุณุชุฏุนุงุก ุฏุงูุฉ ุงู factory (makeCake) ู
ู ุฅูุดุงุก ุงููุงุฆู ุงูู
ูุงุณุจ ู
ู ุฎูุงู ุชุญุฏูุฏ ุงููููุฉ ูุงูุดูู ููุฏุงูุฉ ููู ุงูุชู ุชูููุช ุจุนู
ููุฉ ุชุญุฏูุฏ ุงูููุน ุงูู
ูุงุณุจ.
ุชูุชู ุจุงููุฉ ุชุฌู ุน ุงู Classes ู ุงู Objects ูู ุชูููู ุฃูุจุฑ.
- ุงู
Structural Class paatternsุชุณุชุฎุฏู ู ุจุงุฏูุก ุงููุฑุงุซุฉ ููุชูููู ุงูinterfacesุฃู ุงูClasses. - ุงู
Structural Object Patternsุชุตู ุงููุฉ ุชุฌู ุน ุฃูุซุฑ ู ู ุฃูุจุฌูุช ููุญุตูู ุนูู ูุธุงุฆู ุฌุฏูุฏุฉ. - ุชุนุทู ุงู
Structural Object Patternsููุนุงู ู ู ุงูู ุฑููุฉ ูู ุนู ููุฉ ุงู Composition ููุฃูุจุฌูุชุ ูุฐูู ูุงุจุน ู ู ุงููุฏุฑุฉ ุนูู ุชุบููุฑ ุงู Composition ูู ุฃุซูุงุก ุงูุชูููุฐ (Dynamically).
In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.ููุตุฏ ุจูุง ูุณูุทุ ูุฃุชู ุจูู ุดูุฆูู ุบูุฑ ู ุชูุงูููู ููููู ุจุนู ููุฉ ุงูุฑุจุท ุจูููู ุงุ ููู ุงูุญูุงุฉ ุงููุงูุนูุฉ ุนูู ุณุจูู ุงูู ุซุงูุ ูู ูู ุช ุจุงูุณูุฑ ุฅูู ุฏููุฉ ุฃุฎุฑู ูุฏ ุชุฌุฏ ุจุฃู ุดุงุญู ูุงุชูู ูุง ูุชูุงูู ู ุน ุงููุงุจุณ ุงูู ุชูุงุฌุฏ ููุงูุ ูุณุชุญุชุงุฌ ุฅูู ูุณูุท (Adapter) ููู ููู ุฃู ุชููู ุจุงูุฑุจุท ุจูููู ุงุ ูุงููุฐู ุณูููู ุดููู ููุฐุง:
ูุจุงูู ุซู ูู ุนุงูู ุงู Software Engineering, ูุฏููุง ู ุง ูุณู ู ุจุงู Adapter Design pattern, ุจุญูุซ ูููู ุนูู ุฃูุดุงุก ููุงุณ ู ููุตู ูุนู ู ููุณูุท ุจูู ููุงุณูู ุบูุฑ ู ุชูุงูููู.
ูุฏ ูุชุจุงุฏุฑ ููุฐูู ุชุณุงุคูุ ุจุฃูู ูู ุงุฐุง ูุง ูู ูููุง ุชุนุฏูู ุฃุญุฏ ุงู classes ุงูุบูุฑ ู ุชูุงููุฉ ุจุญูุซ ุชุณุจุญ ู ุชูุงููุฉุ
ุงูุฅุฌุงุจุฉ ุชูู
ู ูู ู
ุจุฏุฃ ูุณู
ู ุจ Open-Closed Principal.
" Objects or entities should be open for extension but closed for modification "
ูุนุชู ุฏ ูุฐุง ุงูู ุจุฏุฃ ุนูู ุฃูู ูุฌุจ ุฃู ูููู ุงู Objects ู ุงู Entites ู ูุชูุญุฉ ููุชูููุฐ ูููู ู ุบููุฉ ููุชุนุฏููุ ุจุญูุซ ุฃูู ูุง ูุญู ูู ูู ููุฏุณ ุฃู ุชููู ุจุงูุชุนุฏูู ุนูู ุฃู ุดูุกุ ููุฏ ูููู ุงูููุฏ ุงูุฐู ุชุณุชุนู ูู ู ูุชูุจ ู ู ูุจู ุดุฎุต ุฃุฎุฑ ููุง ูุณู ุญ ูู ุจุงูุชุนุฏูู ุนูููุ ุฃู ูุฏ ูููู ู ูุฏู ูู ู ุซูุงู ุนู ุทุฑูู ู ูุชุจุฉ ุฃู ุฃู ู ุตุฏุฑ ุฎุงุฑุฌู ููุง ูู ููู ุงููุตู ุฅููู ูุชุนุฏูููุ ูููุง ูุชุถุญ ุฃูู ูุฉ ุงู Adapter ูู ุงู Design patterns.
ูููุชุฑุถ ุจุฃููุง ูุฑูุฏ ุฃู ูููู
ุจุชูุถูุญ ุนุง~ูุฉ ุงูุณููุฑูุงุช (Felidae / ุงููุทุทูุงุช) ุจุฑู
ุฌูุงูุ ูููููุง ูู ุงูุชุทุจูู ูุงุฌููุง ู
ุดููุฉุ ุจุญูุซ ุฌู
ูุน ุฃูุฑุงุฏ ูุฐู ุงูุนุงุฆูุฉ ู
ู ุงููุงุฆูุงุช (ุงูุฃุณูุฏุ ุงููู
ูุฑุ ... ุงูุฎ) ุชููู
ุจุนู
ููุฉ ุงูุฒุฆูุฑุ ูููู ูุจูู ููุง ุงุณุชุซูุงุก ููู ุงููุทุทุ ููู ูุง ุชููู
ุจุงูุฒุฆูุฑุ ุจู ุชููู
ุจุงูู
ูุงุกุ ููุง ูู
ูููุง ุฃู ูุฌุนู ุงููุทุท ุฃุจูุงุก ู
ุจุงุดุฑุฉ ูู Felidaeุ ุจู ูุญุชุงุฌ ุฅูู ูุณูุท ูููู
ุจุฑุจุท ุนู
ููุฉ ุงูู
ูุงุก ูุฏู ุงููุทุฉ ุจุนู
ููุฉ ุงูุฒุฆูุฑ ูุฏู ุงูููุน ุงูุฑุฆูุณ Felidae, ููุญุชุงุฌ ุฅูู ุชุตู
ูู
ููุงุณ ุฃุฎุฑ CarAdapter ููููู
ุจูุฐุง ุงูุบุฑุถุ ููู
ูููุง ุงุนุชุจุงุฑู ู
ู ุซู
ุงุจูุงู ู
ุจุงุดุฑุงู ููุณููุฑูุงุช.
ููู ุทุจููุง ุงูู ุซุงู ุงูุณุงุจู ุจุฑู ุฌูุงู:
ูู ูู ุชุญู ูู ุงูููุฏ ูุงู ูุงู ู ู ููุง
Felidae.java:
public interface Felidae{
public void roar();
}Lion.java:
public class Lion implements Felidae {
public void roar(){
System.out.println("The lion is Roaring!");
}
}Tiger.java:
public class Tiger implements Felidae{
public void roar(){
System.out.println("The tiger is Roaring!");
}
}Cat.java:
public class Cat {
public void meow(){
System.out.println("The cat is meowing!");
}
}CatAdapter.java:
public class CatAdapter implements Felidae {
private Cat cat;
public CatAdapter(Cat cat){
this.cat = cat;
}
public void roar(){
this.cat.meow();
}
}ูู ูููุง ุงูุฃู ุชุฌุฑูุจ ุงูููุฏ ูุชูููุฐู:
public class Main {
public static void main(String[] args) {
Felidae lion = new Lion();
Felidae tiger = new Tiger();
Felidae cat = new CatAdapter(new Cat());
lion.roar();
tiger.roar();
cat.roar();
}
}ููููู ุงููุงุชุฌ:
The lion is Roaring!
The tiger is Roaring!
The cat is meowing! The composite pattern is a partitioning design pattern. The composite pattern describes a group of objects that are treated the same way as a single instance of the same type of object. The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.ูููุชุฑุถ ุจุฃููุง ูุฑูุฏ ุจุฃู ูููู
ุจุชูุถูุญ ุชุฑููุจ ุงูุณูุงุฑุฉ ุจุงุณููุจ ุจุฑู
ุฌูุ ูุฏ ูููู ุชูููุฑูุง ุงูุฃููู ุฃู ูููู ูุฏููุง ูู ุดูุก ูููููุง ุนุจุงุฑุฉ ุนู Object ู
ููุตูุ ุจุญูุซ ูููู ูุฏููุง ุฃูุจุฌูุช ุฎุงุต ู
ู ููุน Engine ู ุฃูุจุฌูุช ุฃุฎุฑ ู
ู ููุน CarBody ูุฃูุจุฌูุช ุงุฎุฑ ู
ู ููุน ููุฐุงุ ูููู ูุฐุง ูุคุฏู ุฅูู ููุถูุ ูุนูู ุณุจูู ู
ู ุงูู
ุซุงูุ ูู ุฃุฑุฏูุง ู
ุซูุงู ุฃู ูููู
ุจุญุณุงุจ ุงูุชูููุฉ ุงูุฎุงุตุฉ ุจุงูู
ุญุฑู ูุชูููุฉ ูููุฉุ ุจุญูุซ ูุชู
ุญุณุงุจ ุชูููุฉ ุฌู
ูุน ุงูู
ูููุงุช ุงูุฏุงุฎููุฉ ููุ ูููู ุจูุฐุง ุงูุดูู ููุฐู ุงูููุถู ู
ู ุงูุตุนุจ ุงูููุงู
ุจู
ุซู ูุฐู ุงูุฃู
ูุฑุ ูููุง ุชูู
ู ููุฉ ุงู Composite Design pattern ุจุญูุซ ูู
ูููุง ุชุฎูู ุงูุณูุงุฑุฉ ุจุดูู ุฃูุถู ุจุงูุดูู ุงูุงุชู:
ูุจูุฐุง ุงูุดูู ูู
ูููู ุฃู ุฃููู
ุจุชูุณูู
ุงูุดูุก ุฅูู ู
ุฎุทุท ุดุฌุฑู ุจุญูุซ ูู
ูููู ู
ู ู
ุนุฑูุฉ ุงูู
ูููุงุช ููู ุฌุฒุก ุจุดูู ุชุชุงุจุนูุ ูู
ูููุงุช ุงูุณูุงุฑุฉ ู
ุซูุงุ ูู ูููู ุงูุณูุงุฑุฉุ ูุงูู
ุญุฑู ูู
ู ุซู
ู
ูู ุชูุณูู
ู
ูููุงุช ุงูู
ุญุฑู ู
ุซูุงูุ ุฅูู ุบุฑู ุงุญุชุฑุงู ู ุดู
ุนุงุงุช ุงูุงุญุชุฑุงู (ุจูุฌููุงุช)ุ ููุฐูู ูู
ูููุง ุชูุณูู
ูููู ุงูุณูุงุฑุฉ ุฅูู ุฃุจูุงุจู ุฅุทุงุฑุงุชุ ุจุญูุซ ูุญุชูู ูู Object ุนูู ูุงุฆู
ุฉ ู
ู ุงู Objects ุงูุชู ุชุนุฏ ู
ูููุงุช ููุฐุง ุงู Object ุงูุฑุฆูุณุ ููู
ูููู ู
ู ุฎูุงู ูุฐู ุงููููููุฉ ุงููุตูู ุฅูู ุชูุงุตูู ุฃู ู
ููู ุจุงูุฅุถุงูุฉ ูู
ูููุงุชู ุงููุฑุนูุฉ ุจูู ุณูููุฉ.
ูู ูู ุชุญู ูู ุงูููุฏ ูุงู ูุงู ู ู ููุง
CarElement.java:
public interface CarElement {
float getPrice();
void printDetails();
}CarPart.java:
public class CarPart implements CarElement{
String model;
float price;
public CarPart(String model, float price) {
this.model = model;
this.price = price;
}
String getModel() {
return this.model;
}
public float getPrice() {
return this.price;
}
public void printDetails() {
System.out.println("The part Model: (" + this.model + "), price (" + this.getPrice() + ")");
}
}CarComposite.java:
import java.util.ArrayList;
import java.util.List;
public class CarComposite implements CarElement {
String model;
List<CarElement> parts;
public CarComposite(String model) {
parts = new ArrayList<>();
this.model = model;
}
String getModel() {
return this.model;
}
void addPart(CarElement part) {
this.parts.add(part);
}
public float getPrice() {
float price = 0;
for (CarElement p : parts)
price += p.getPrice();
return price;
}
public void printDetails() {
System.out.println("The part Model: (" + this.model + "), price (" + this.getPrice() + ")");
if (!parts.isEmpty()) {
System.out.println("It has the following sub-parts: ");
for (CarElement p : parts)
p.printDetails();
}
System.out.println("================================");
}
}ููู ูููุง ุงูุชุทุจูู ุจุงูุดูู ุงูุงุชู:
package Java.Structural.Composite.Examples.Car;
public class Main {
public static void main(String[] args) {
CarComposite car = new CarComposite("Audi R8");
CarComposite engine = new CarComposite("5.2L V10 FSI");
CarComposite carBody = new CarComposite("Audi R8 Body");
CarPart wheel1 = new CarPart("Audi R8 Wheels", 2400);
CarPart wheel2 = new CarPart("Audi R8 Wheels", 2400);
CarPart wheel3 = new CarPart("Audi R8 Wheels", 2400);
CarPart wheel4 = new CarPart("Audi R8 Wheels", 2400);
carBody.addPart(wheel1);
carBody.addPart(wheel2);
carBody.addPart(wheel3);
carBody.addPart(wheel4);
CarPart door1 = new CarPart("Audi R8 Door", 5000);
CarPart door2 = new CarPart("Audi R8 Door", 5000);
carBody.addPart(door1);
carBody.addPart(door2);
CarPart piston1 = new CarPart("V10 Piston", 1500);
CarPart piston2 = new CarPart("V10 Piston", 1500);
CarPart piston3 = new CarPart("V10 Piston", 1500);
CarPart piston4 = new CarPart("V10 Piston", 1500);
CarPart piston5 = new CarPart("V10 Piston", 1500);
CarPart piston6 = new CarPart("V10 Piston", 1500);
engine.addPart(piston1);
engine.addPart(piston2);
engine.addPart(piston3);
engine.addPart(piston4);
engine.addPart(piston5);
engine.addPart(piston6);
CarPart sparkPlug1 = new CarPart("NGK Spark plug", 80);
CarPart sparkPlug2 = new CarPart("NGK Spark plug", 80);
CarPart sparkPlug3 = new CarPart("NGK Spark plug", 80);
CarPart sparkPlug4 = new CarPart("NGK Spark plug", 80);
CarPart sparkPlug5 = new CarPart("NGK Spark plug", 80);
CarPart sparkPlug6 = new CarPart("NGK Spark plug", 80);
engine.addPart(sparkPlug1);
engine.addPart(sparkPlug2);
engine.addPart(sparkPlug3);
engine.addPart(sparkPlug4);
engine.addPart(sparkPlug5);
engine.addPart(sparkPlug6);
car.addPart(carBody);
car.addPart(engine);
car.printDetails();
}
}ููููู ุงููุงุชุฌ:
The part Model: (Audi R8), price (29080.0)
It has the following sub-parts:
The part Model: (Audi R8 Body), price (19600.0)
It has the following sub-parts:
The part Model: (Audi R8 Wheels), price (2400.0)
The part Model: (Audi R8 Wheels), price (2400.0)
The part Model: (Audi R8 Wheels), price (2400.0)
The part Model: (Audi R8 Wheels), price (2400.0)
The part Model: (Audi R8 Door), price (5000.0)
The part Model: (Audi R8 Door), price (5000.0)
================================
The part Model: (5.2L V10 FSI), price (9480.0)
It has the following sub-parts:
The part Model: (V10 Piston), price (1500.0)
The part Model: (V10 Piston), price (1500.0)
The part Model: (V10 Piston), price (1500.0)
The part Model: (V10 Piston), price (1500.0)
The part Model: (V10 Piston), price (1500.0)
The part Model: (V10 Piston), price (1500.0)
The part Model: (NGK Spark plug), price (80.0)
The part Model: (NGK Spark plug), price (80.0)
The part Model: (NGK Spark plug), price (80.0)
The part Model: (NGK Spark plug), price (80.0)
The part Model: (NGK Spark plug), price (80.0)
The part Model: (NGK Spark plug), price (80.0)
================================
================================ุงุณุชุทุนูุง ู ู ุฎูุงู ุชุทุจูู ุงู Composite pattern ู ู ุงูุชุนุงู ู ู ุน ุฃู ุดุฌุฑุฉ ูุฑุนูุฉ (ุนูุตุฑ ูู ุฌู ูุนุฉ ุงูุนูุงุตุฑ ุงูู ูููุฉ ูู) ุจุดูู ู ููุตูุ ูุงูุฎุญุตูู ุนูู ุงูู ุนููู ุงุช ุงูุฎุงุตุฉ ุจูู ุจูู ุณูููุฉุ.
ุชูุชู ุจุงูุฎูุงุฑุฒู ูุงุช ูุจุชูุณูู ุงูู ุณุคูููุงุช ุจูู ุงู Objects.
- ุงู
Behavioural Class patternsุชุณุชุฎุฏู ู ุจุงุฏูุก ุงููุฑุงุซุฉ ูุชูุณูู ุงูู ูุงู ูุงูุณููู ุนูู ุงู classes ุงูู ูููุฉ ููุง. - ุงู
Behavioural Object patternsุชุณุฏุฎุฏู ุงู composition ุจุฏูุงู ู ู ุงููุฑุงุซุฉุ ููุถุญ ุจุนุถูุง ุนู ููู ูู ูู ูุฃูุซุฑ ู ู Object ุงูุชุนุงูู ูู ุฅูุฌุงุฒ ู ูู ุฉ ู ุนููุฉ ูุงูู ูู ูุฅุญุฏุงูุง ุฅูุฌุงุฒูุง ููุญุฏู.
Command (EN) == ุฃู ุฑ (ุงูุนุฑุจูุฉ)
The command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.ูู ุทูุจ ู ูู ุฃู ุชููู ุจุชุตู ูู ุจุฑูุงู ุฌ ุงูุฉ ุญุงุณุจุฉ ุจุณูุทุฉ ุชููู ุจุงูุนู ููุงุช ุงูุฃุณุงุณูุฉ (ุฌู ุนุ ุทุฑุญุ ุถุฑุจุ ูุณู ุฉ) ุณูููู ุจูุฐุง ุงูุดูู ุบุงูุจุงู:
public class Calculator {
int value = 0;
public void add(int amount) {
this.value += amount;
}
public void subtract(int amount) {
this.value -= amount;
}
public void multiply(int amount) {
this.value *= amount;
}
public void divide(int amount) {
this.value /= amount;
}
}ููุฐุง ุงูููุฏ ู ู ุชุงุฒ ููุนู ู ุจุงูุดูู ุงููุงุฒู ุ ููู ูููุง ุชูููุฐู:
public class Main{
public static void main(String[] args) {
Calculator c = new Calculator();
c.add(5);
c.multiply(2);
c.subtact(3);
System.out.println(c.value); // 7
}
}ูููู ูููุชุฑุถ ุจุฃููุง ูุฑูุฏ ุฃู ูุถูู ุฎุงุตูุฉ ุงูุชุฑุงุฌุน (Undo) ููุฐู ุงูุญุงุณุจุฉุ ุจูุฐุง ุงูููุฏ ุงูุญุงูู ุณูููู ุงูุฃู ุฑ ุตุนุจ.
ูุฐูู ูุตุจุญ ุงูุญู ุงูุฃู ุซู ูู ุฃู ูุนูุฏ ููููุฉ ุงูููุฏ ุจุงุณุชุฎุฏุงู Command Design pattern.
ู
ุง ุณููุนูู ูู ุฃู ูุณูุฎุฑุฌ ูู ุงูู
ูุงู
ุงูุชู ูููู
ุจูุง ุงู class ุงูุฎุงุต ุจูุงุ ููุตู ูู ู
ููุง ุนูู ุญุฏุฉ ูู Command ุฎุงุตุ ูุนูู ุณุจูู ุงูู
ุซูู ูู ู
ุซุงููุง ุงูุณุงุจูุ ูุฏููุง ุงู class ุงูู
ุณู
ู Calculator ูุญูู ุนูู ุฃุฑุจุนุฉ ู
ูุงู
ูููู
ุจูุง (add, subtract, divide, multiply) ู
ู
ุซูุฉ ุจุงูุฏูุงู ุงูุชู ูู
ุชูููุงุ ููููู
ุจุฅูุดุงุก class ุฌุฏูุฏ ููู ู
ููุง (ู
ู ููุน Command) ูุญูู ุนูู ุฏุงูุชูู ุฃุณุงุณูุชูู:
execute:
ุชููู ุจุชูููุฐ ุงูุนู ููุฉ ุงูุฃุณุงุณูุฉ ุงูู ุทููุจุฉ.
undo:
ุชููู ุจุงูุชุฑุงุฌุน ุนู ุงูุนู ููุฉ ุงูุฃุณุงุณูุฉุ ู ู ุฎูุงู ุชูููุฐ ุงูู ุนููุณ ููุนู ููุฉ ุงูุฃุณุงุณูุฉ.
ูุนูู ุณุจูู ุงูู
ุซุงูุ ูู ุญุงูุฉ ุงูุฃู
ุฑ addุ ุชููู ุฏุงูุฉ execute ุชููู
ุจุฅุถุงูุฉ ุงูุฑูู
ุฅูู ุงูููู
ุฉ ุงูุญุงููุฉุ ุจููู
ุง ุชููู ุฏุงูุฉ undo ุชููู
ุจุทุฑุญ ุงูุฑูู
ู
ู ุงูููู
ุฉ ุงูู
ูุฌูุฏุฉ (ู
ุนููุณ ุนู
ููุฉ ุงูุฌู
ุน ูู ุนู
ููุฉ ุงูุทุฑุญ).
ููุตุจุญ ูุฏููุง Add.java ุจุงูุดูู ุงูุชุงูู:
ูู ููู ุชุญู ูู ุงูููุฏ ูุงู ูุงู ู ู ููุง
public class Add implements Command {
private int value;
private Calculator calculator;
public Add(Calculator calc, int value){
this.calculator = calc;
this.value = value;
}
public void execute(){
this.calculator.value += this.value;
}
public void undo(){
this.calculator.value -= this.value;
}
}ุจุญูุซ ูููู Command.java :
public interface Command {
public void execute();
public void undo();
}ููุฐูู ุงูุฃู ุฑ ุจุงููุณุจุฉ ููู ู ู:
Subtract.java :
public class Subtract implements Command {
private int value;
private Calculator calculator;
public Subtract(Calculator calc, int value) {
this.calculator = calc;
this.value = value;
}
public void execute() {
this.calculator.value -= this.value;
}
public void undo() {
this.calculator.value += this.value;
}
}Multiply.java :
public class Multiply implements Command {
private int value;
private Calculator calculator;
public Multiply(Calculator calc, int value){
this.calculator = calc;
this.value = value;
}
public void execute(){
this.calculator.value *= this.value;
}
public void undo(){
this.calculator.value /= this.value;
}
}Divide.java :
public class Divide implements Command {
private int value;
private Calculator calculator;
public Divide(Calculator calc, int value){
this.calculator = calc;
this.value = value;
}
public void execute(){
this.calculator.value /= this.value;
}
public void undo(){
this.calculator.value *= this.value;
}
}ุงูุฃู ูุญุชุงุฌ ุฃู ูููู
ุจุชุนุฏูู ุงู class ุงูุฃุณุงุณู ุงูุฎุงุต ุจูุง (Calculator)ุ ููุญุชุงุฌ ุฃู ูููู
ุจุชุนุฏููู ุจุญูุซ ูููู
ุจุงุณุชูุจุงู ุงู Commands ููุฎุฒููุง ููููุฐูุง.
ุจุฏุงูุฉูุ ูุญุชุงุฌ ุฃู ูููู
ุจุชุฎุฒูู ุงูุฃูุงู
ุฑ ุงูุชู ูุชู
ุชูููุฐูุง ุญุชู ูุชู
ูู ู
ู ุชุทุจูู ุชูููุฉ ุงูุชุฑุงุฌุนุ ููุญุชุงุฌ ุฅูู ููุน ู
ูุงุณุจ ู
ู ุงู ููุงุฆู
ุ ู
ู ุงูู
ู
ูู ุฃู ูุฎุชุงุฑ Linked List ุนูู ุณุจูู ุงูู
ุซุงูุ ูููู ุงูุงุฎุชูุงุฑ ุงูุฃูุถู ุฃู ูููู Stackุ ูุฐูู ูุฃูู ุนูุฏู
ุง ูููู
ุจุงูุชุฑุงุฌุน ุณูุญุชุงุฌ ุฃู ูููู
ุจุงูุชุฑุงุฌุน ุนู ุงูุฃู
ุฑ ุงูุฃุฎูุฑ ุงูุฐู ุชู
ุชูููุฐูุ ูุฐูู ูุฌุนู ุงู Stack ุฎูุงุฑูุง ุงูุฃู
ุซู.
ูุณูููู
ุจุฅุถุงูุฉ:
private Stack<Command> commands = new Stack<>();ูุณูุญุชุงุฌ ุฃูุถุงู ุฅูู ุฏุงูุฉ ุชููู
ุจุชูููุฐ ุงูุฃู
ุฑ ุจุงููุณุจุฉ ูู Calculator:
public void execuetCommand(Command cmd) {
this.commands.add(cmd);
cmd.execute();
}ุชููู ูุฐู ุงูุฏุงูุฉ ุจุฅุฐุงูุฉ ุงูุฃู ุฑ ุงูุฐู ุชู ุชูููุฐู ูููุงุฆู ุฉ ูู ู ุซู ุชูููุฐู.
ูุจุงููุณุจุฉ ูุงููุฉ ุงูุชุฑุงุฌุน ูู ูููุง ุงูููุงู ุจูุง ุงูุงู ุนู ุทุฑูู ุฏุงูุฉ:
public void undoCommand() {
Command cmd = this.commands.pop();
if (cmd == null) return;
cmd.undo();
}ุชููู
ูุฐู ุงูุฏุงูุฉ ุจุฌูุจ ุงุฎุฑ ุฃู
ุฑ ุชู
ุชูููุฐู ูุฅุฒุงูุชู ู
ู ุงููุงุฆู
ุฉุ ูู
ู ุซู
ุงุชุณุฏุนุงุก ุฏุงูุฉ undo ุงูุชู ูู
ุชูููุง ููุชุฑุงุฌุน.
ููุตุจุญ Calculator.java ุจุงูุดูู ุงูุงุชู:
import java.util.Stack;
public class Calculator {
public int value = 0;
private Stack<Command> commands = new Stack<>();
public void execuetCommand(Command cmd) {
this.commands.add(cmd);
cmd.execute();
}
public void undoCommand() {
Command cmd = this.commands.pop();
if (cmd == null)
return;
cmd.undo();
}
}ููู ูููุง ุชูููุฐ ูุชุฌุฑุจุฉ ุงูููุฏ :
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
calc.execuetCommand(new Add(calc, 5));
calc.execuetCommand(new Multiply(calc, 3));
calc.execuetCommand(new Subtract(calc, 5));
System.out.println(calc.value);
calc.undoCommand();
System.out.println(calc.value);
calc.undoCommand();
System.out.println(calc.value);
calc.undoCommand();
System.out.println(calc.value);
}
}ููููู ุงููุงุชุฌ:
10
15
5
0Observer (EN) == ู ุฑุงูุจ (ุงูุนุฑุจูุฉ)
The observer pattern is a software design pattern in which an object, named the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.ุชุฎูู ุจุฃููุง ูุฑูุฏ ุชุตู
ูู
ุจุฑูุงู
ุฌ ูููู
ุจุนุฑุถ ุญุงูุฉ ุงูุทูุณ ูู ู
ูุทูุฉ ู
ุนููุฉุ ุจุญูุซ ุชุฃุชู ุงูู
ุนููู
ุงุช ู
ู ู
ุตุฏุฑ ู
ุงุ (ูู ุญุงูุฉ ุงูู
ุซุงู ูุฑุฑูุง ุจุฃู ุชููู ูุงุฏู
ุฉ ู
ู class ุจุฏุงุฎู ุงูู
ุดุฑูุน ุจุญูุซ ูููู ู
ุฑุชุจุท ู
ุซูุงู ุจู
ุญุทุฉ ููุฃุฑุตุงุฏ ุงูุฌููุฉ ุฃู ู
ุง ุดุงุจู)ุ ุจุญูุซ ูุชู
ุชุญุฏูุซ ุงูุจูุงูุงุช ุฏุงุฎู ุงูุชุทุจูู ุงูุฎุงุต ุจูุง ุชููุงุฆูุงู ูู ูู ู
ุฑุฉ ูุญุฏุซ ุชุบูุฑ ุนูู ุงูุทูุณุ ูููุฒู
ููุง ูู ูุฐู ุงูุญุงูุฉ ุฃู ูููู ููุงู ูุณููุฉ ููู
ุตุฏุฑ ุจุฃู ูููู
ุจุฅุนูุงู
ุงูุชุทุจูู ุจูุฌูุฏ ุชุญุฏูุซ ุฌุฏูุฏ ููู
ุนููู
ุงุช ุงูุฎุงุตุฉ ุจุงูุทูุณุ ููุฐู ุชูู
ู ุจูุธููุฉ ุงู Observer Design patternุ ุจุญูุซ ูููู ุงูุชุทุจูู ุงูุฎุงุต ุจู ู
ู ููุน Observer (ู
ุฑุงูุจ)ุ ููุชู
ุฑุจุทู ุจุงูู
ุฒูุฏ ุงูุฎุงุต ุจู
ุนููู
ุงุช ุงูุทูุณ ุนู ุทุฑูู ุฏุงูุฉ ูู
ุชูููุง (subscribe)ุ ูู
ุชูู ุงูู
ุฒูุฏ ูุงุฆู
ุฉ ู
ู ุงู Observers ูุชู
ุฅุถุงูุฉ ุงู Observer ููุง ุนูุฏู
ุง ูุชู
ุชู
ุฑูู ูุฏุงูุฉ subscribe, ุจุญูุซ ูููู
ุงูู
ุฒูุฏ ุจุฅุนูุงู
ุฌู
ูุน ุงู Observers ุงูู
ุดุชุฑููู ูุฏููุ ุจุญูุซ ูุญุชูู ูู Observer ู
ููู
ุนูู ุฏุงูุฉ update ุชุญูู ุงูุฃูุงู
ุฑ ุงูู
ุทููุจ ุชูููุฐูุง ูู ุญุงูุฉ ูุฌูุฏ ู
ุนููู
ุงุช ุฌุฏูุฏุฉุ ูู ุญุงูุฉ ุชุทุจูููุง ู
ุซูุงูุ ูู ู
ุง ุชููู
ุจู ูุฐู ุงูุฏุงูุฉ ูู ุฃู ูุชู
ุทุจุงุนุฉ ุงูุจูุงูุงุช ุงูุฌุฏูุฏุฉ ููู
ุณุชุฎุฏู
.
ูู ููู ุชุญู ูู ุงูููุฏ ูุงู ู ู ู ููุง
WeatherType.java :
public enum WeatherType {
NORMAL,
SUNNY,
CLOUDY,
RAINY,
STORMY
}WeatherObserver.java :
public interface WeatherObserver {
void update();
void setProvider(Weather provider);
}Weather.java :
import java.util.ArrayList;
import java.util.List;
public class Weather {
private WeatherType currentWeather = WeatherType.NORMAL;
public final List<WeatherObserver> observers;
public Weather() {
this.observers = new ArrayList<>();
}
public void subscribe(WeatherObserver observer) {
this.observers.add(observer);
observer.setProvider(this);
}
public void setWeather(WeatherType type) {
this.currentWeather = type;
for(WeatherObserver observer : this.observers){
observer.update();
}
}
public WeatherType getWeather() {
return this.currentWeather;
}
}Application.java :
public class Application implements WeatherObserver {
private Weather provider;
@Override
public void update() {
System.out.println("The Weather has changed to: " + this.provider.getWeather());
}
@Override
public void setProvider(Weather provider) {
this.provider = provider;
}
}Main.java :
public class Main {
public static void main(String[] args) {
Weather weatherProvider = new Weather();
Application myApplication = new Application();
weatherProvider.subscribe(myApplication);
weatherProvider.setWeather(WeatherType.CLOUDY);
weatherProvider.setWeather(WeatherType.RAINY);
weatherProvider.setWeather(WeatherType.STORMY);
weatherProvider.setWeather(WeatherType.SUNNY);
}
}ูุงุชุฌ ุงูุชูููุฐ: ๐ป
The Weather has changed to: CLOUDY
The Weather has changed to: RAINY
The Weather has changed to: STORMY
The Weather has changed to: SUNNY- design-patterns-for-humans ( Github repository )
- design_patterns_in_typescript ( Github repository )
- Java Design patterns ( Complete Guide )
- Refactoring Guru-Design patterns ( Complete Guide )
- Design Patterns: Elements of Reusable Object-Oriented Software ( Book )
- Head First Design Patterns: A Brain-Friendly Guide ( Book )
- remembering "The Gang of four..!" ( Cheat-sheet )
- Useful posters of the GoF patterns ( Cheat-sheet )
- Design-patterns-Relationships ( Cheat-sheet )

