Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Dessouki98/design-patterns-in

ย 
ย 

Repository files navigation

Design Patterns

Design patterns

License Commits Stars Forks Last Commit Hits Contributions (Welcome)

ูู‡ุฑุณ ุงู„ุญุชูˆูŠุงุช:

ุชุนุฑูŠู Design patterns: ู‡ูŠ ุนุจุงุฑุฉ ุนู† ุญู„ูˆู„ ู„ู…ุดุงูƒู„ ู…ุชูƒุฑุฑุฉุŒ ูˆุชุดูƒู„ ู‚ูˆุงุนุฏ ุฅุฑุดุงุฏูŠุฉ ู„ูƒูŠููŠุฉ ุงู„ุชุนุงู…ู„ ู…ุน ุชู„ูƒ ุงู„ู…ุดุงูƒู„ ุงู„ู…ุญุฏุฏุฉ.

ู‡ูŠ ู„ูŠุณุช ุนุจุงุฑุฉ ุนู† Classes ูˆู„ุง Packages ูˆู„ุง ุฃูŠ ู…ูƒุชุจุงุช ุฃูˆ ุฃูƒูˆุงุฏ ุฌุงู‡ุฒุฉ ุจุญูŠุซ ูŠุชู… ุงุณุชุฎุฏุงู…ู‡ุง ู…ุจุงุดุฑุฉ ุจุฏุงุฎู„ ุงู„ูƒูˆุฏ ุงู„ุฎุงุต ุจูƒุŒ ููŠ ุงู„ู…ู‚ุงุจู„ ู…ุง ู‡ูŠ ุฅู„ุง ุฃุณุงู„ูŠุจ ู…ุชุจุนุฉ ู„ุญู„ ุงู„ู…ุดุงูƒู„ ุงู„ู…ุชูƒุฑุฑุฉ ุงู„ุชูŠ ูŠู…ูƒู† ุฃู† ุชูˆุงุฌู‡ู‡ุง ุฎู„ุงู„ ุนู…ู„ูŠุฉ ูƒุชุงุจุฉ ุงู„ูƒูˆุฏ.

ุชุนุฑูŠู Design patterns:

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.

ู†ู‚ุงุท ู…ู‡ู…ุฉ โš 

  • ู„ุง ุชุนุชุจุฑู‡ุง ูƒู€ ุญู„ูˆู„ ุณุญุฑูŠุฉ ู„ู„ู…ุดุงูƒู„ ุงู„ุชูŠ ู‚ุฏ ุชูˆุงุฌู‡ู‡ุงุŒ ูู‡ูŠ ู„ูŠุณุช ุนุจุงุฑุฉ ุนู† ู‚ูˆุงู„ุจ ุฌุงู‡ุฒุฉ ูŠุชู… ุงู„ุชุนุงู…ู„ ู…ุนู‡ุง ุฅู†ู…ุง ู‡ูŠ ุนุจุงุฑุฉ ุนู† ุญู„ูˆู„ ูŠุฌุจ ุนู„ูŠูƒ ุฃู† ุชูƒูŠูู‡ุง ุญุณุจ ุงู„ุจูŠุฆุฉ ูˆุงู„ู…ุดูƒู„ุฉ ุงู„ู„ุชุงู† ุชุฑูŠุฏ ุชุทุจูŠู‚ู‡ุง ุนู„ูŠู‡ู….
  • ุนู†ุฏู…ุง ูŠุชู… ุงุณุชุฎุฏุงู…ู‡ู… ุจุงู„ุดูƒู„ ุงู„ุตุญูŠุญ ูุฅู†ู‡ุง ุชุนุชุจุฑ ู…ู†ู‚ุฐุฉุŒ ุจุญูŠุซ ูŠุชู… ุญู„ ุงู„ู…ุดูƒู„ุฉ ุจุงู„ุดูƒู„ ุงู„ู…ู†ุงุณุจ ูˆุชุณุงู‡ู… ููŠ ุฅู†ุชุงุฌ ูƒูˆุฏ ู…ุฑุชุจ ูˆู…ู†ุณู‚ ุจุงู„ุดูƒู„ ุงู„ุตุญูŠุญุŒ ุจูŠู†ู…ุง ุงู„ุงุณุชุฎุฏุงู… ุงู„ุฎุงุทู‰ุก ู„ู‡ุง ูŠุคุฏูŠ ุฅู„ู‰ ููˆุถู‰.

ู„ู…ุงุฐุง ุฃุณุชุฎุฏู… ุงู„ Design patterns ุŸ ๐Ÿค”

  • ุชูˆูุฑ ุญู„ูˆู„ ุนุงู…ุฉุŒ documented ุจุดูƒู„ ู…ุฌุฑุฏ ูˆุบูŠุฑ ู…ุฑุชุจุท ุจุชูุงุตูŠู„ ู…ุดูƒู„ุฉ ู…ุนูŠู†ุฉ.
  • ุชุณุงู‡ู… ุจุชุณุฑูŠุน ุนู…ู„ูŠุฉ ุงู„ุชุทูˆูŠุฑุŒ ุจุญูŠุซ ุชูˆูุฑ ู†ู…ุงุฐุฌ ุชุทูˆูŠุฑ ู…ุฌุฑุจุฉ ู…ุณุจู‚ุงู‹ ูˆู…ุซุจุฉ ุงู„ูุนุงู„ูŠุฉ.
  • ุชุนุฏ ุงู„ design patterns ู†ุชุงุฌ ู„ุฏุฑุงุณุงุช ูˆุฎุจุฑุงุช ู…ุทูˆุฑูŠู† ุณุงุจู‚ูŠู†ุŒ ููŠุนุฏ ุงุณุชุฎุฏุงู…ู‡ุง ุฅูŠุฌุงุจูŠุฉ ุจุญูŠุซ ุฃู†ูƒ ุชุณุชููŠุฏ ู…ู† ุชู„ูƒ ุงู„ุฎุจุฑุงุช ุงู„ุฎุงุตุฉ ุจู‡ู….
  • ุชู…ู†ุน ู…ู† ุงู„ูˆู‚ูˆุน ููŠ ู…ุดูƒู„ุงุช ุงู„ุฏู‚ูŠู‚ุฉ ุงู„ุชูŠ ุชุคุฏูŠ ุฅู„ู‰ ู…ุดุงูƒู„ ุถุฎู…ุฉ.
  • ูŠู†ุชุฌ ู…ู† ุฎู„ุงู„ู‡ุง ูƒูˆุฏ ุณู‡ู„ ุงู„ู‚ุฑุงุกุฉุŒ ุฎุตูˆุตุงู‹ ู„ุฏุง ุฃูˆู„ุฆูƒ ุงู„ุฐูŠู† ูŠู…ุชู„ูƒูˆู† ู…ุนุฑูุฉ ุจุงู„ design patterns ุงู„ู…ุณุชุฎุฏู…ุฉ.

ูƒูŠู ุชู… ุงู„ุชูˆุตู„ ุฅู„ูŠู‡ู…ุŒ ูˆู…ู† ู‡ู… ุงู„ุฐูŠู† ุชูˆุตู„ูˆุง ุฅู„ูŠู‡ู… ุŸ ๐Ÿ‘จโ€๐Ÿซ

ูŠุฑุฌุน ุงู„ูุถู„ ุงู„ุฃูƒุจุฑ ููŠ ู‡ุฐุง ุงู„ุงูƒุชุดุงู ู„ู…ุฌู…ูˆุนุฉ ุชุณู…ู‰ ุจ The Gang of Four (GoF) ุญูŠุซ ู‚ุงู…ูˆุง ุจุฅู†ุดุงุก ูƒุชุงุจ ุจุงุณู…: Design Patterns: Elements of Reusable Object-Oriented SoftwareุŒ ูŠุดุฑุญ ู‡ุฐุง ุงู„ูƒุชุงุจ ุนู† 23 design patterns ุจุงู„ู„ุฅุถุงูุฉ ุฅู„ู‰ ุฃู…ุซู„ุฉ ูˆุฃูƒูˆุงุฏ ู…ูƒุชูˆุจุฉ ุจู„ุบุฉ c++.

ุฃู‚ุณุงู… ุงู„ design patterns :

ุญุณุจ ุงู„ุบุงูŠุฉ:

Creational:

ุชู‡ุชู… ุจุงู„ูƒูŠููŠุฉ ุงู„ุชูŠ ูŠุชู… ู…ู† ุฎู„ุงู„ู‡ุง ุฅู†ุดุงุก ุงู„ Objects ูˆ ุงู„ Classs.

Structural:

ุชู‡ุชู… ุจุชูƒูˆูŠู† ุงู„ Objects ูˆ ุงู„ Classes.

Beahavioral:

ุชู‡ุชู… ุจุงู„ุชูุงุนู„ ุจูŠู† ุงู„ Objects ูˆ ุงู„ Classes ุจุงู„ุฅุถุงูุฉ ุฅู„ู‰ ุฃู†ู‡ุง ุชู‡ุชู… ุจุงู„ู…ุณุคูˆุงู„ูŠุงุช ุงู„ุฎุงุตุฉ ุจูƒู„ ู…ู†ู‡ุง.

ุญุณุจ ุงู„ู…ุฌุงู„:

ู…ุฌุงู„ ุงู„ Class:

ุชูˆุถุญ ุงู„ุนู„ุงู‚ุงุช ุจูŠู† ุงู„ classes ูˆ ุงู„ classes ุงู„ูุฑุนูŠุฉุŒ ูˆูŠุชู… ุชุนุฑูŠูู‡ุง ุจุดูƒู„ static.

ู…ุฌุงู„ ุงู„ Object:

ุชูˆุถุญ ุงู„ุนู„ุงู‚ุงุช ุจูŠู† ุงู„ Objects ุŒ ูˆูŠุชู… ุชุนุฑูŠูู‡ุง ุจุดูƒู„ dynamic.

Creational Design patterns:

ุชู‚ูˆู… ุจุชุฌุฑูŠุฏ ุนู…ู„ูŠุฉ ุฅู†ุดุงุก ุงู„ ObjectsุŒ ู…ู…ุง ูŠุณุงุนุฏ ููŠ ุงู„ุญุตูˆู„ ุนู„ู‰ ู†ุธุงู… ู…ุณุชู‚ู„ ููŠ ูƒูŠููŠุฉ ุงู„ุงู†ุดุงุกุŒ ุงู„ุชูƒูˆูŠู† ูˆุงู„ุนุฑุถ ู„ู„ Objects ุงู„ุฎุงุตุฉ ุจู‡.

ู†ู‚ุงุท ู…ู‡ู…ุฉ: ๐Ÿงฉ

  • ุชุณุชุฎุฏู… ุงู„ Creational patterns ุงู„ูˆุฑุงุซุฉ(inhiretance) ู„ู„ุชูุฑูŠู‚ ุจูŠู† ุงู„ classes ุงู„ุชูŠ ูŠุชู… ุฅู†ุดุงุฆู‡ุง.
  • ููŠ ุงู„ Creational patterns ูŠุชู… ุชูˆูƒูŠู„ ุนู…ู„ูŠุฉ ุฅู†ุดุงุก ุงู„ Objects ุฅู„ู‰ Class ุงุฎุฑ.

ุชู‚ุณูŠู… Creational Design Patterns ุญุณุจ ุงู„ scope:

Class Scope:

  • Factory Method

Object Scope:

  • Abstract Factory
  • Builder
  • Prototype
  • Singleton

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.

ูƒูŠู ูŠุชู… ุชูƒูˆูŠู† ูƒู„ุงุณ ุจ Singleton Design pattern ? ๐Ÿ—

ูŠุชุฑูƒุจ ุฃูŠ Class ู…ุตู…ู… ุจ Singleton ู…ู† ุนุฏุฉ ุฃู…ูˆุฑ ุฃุณุงุณูŠุฉ:

  • ูŠูƒูˆู† ุงู„ Constructor ุงู„ุฎุงุต ุจู‡ privateุŒ ุจุญูŠุซ ูŠู…ู†ุน ุฃูŠ ุนู…ู„ูŠุฉ ู„ุฅู†ุดุงุก Object ู…ู† ุฎุงุฑุฌ ุงู„ Class ู†ูุณู‡ุŒ ูุจู‡ุฐุง ุงู„ุดูƒู„ ู†ุชุฃูƒุฏ ุฃู† ุนู…ู„ูŠุฉ ุงู„ุฅู†ุดุงุก ุณุชุชู… ุญุณุจ ุงู„ู‚ูŠูˆุฏ ุจุฏุงุฎู„ ุงู„ Class ู†ูุณู‡ ูู‚ุท.

  • ู…ุชุบูŠุฑ ูŠุณุชุนู…ู„ ูƒู€ instance ู„ูŠุชู… ุชุฎุฒูŠู† ุงู„ Object ุงู„ูˆุญูŠุฏ ุจุญูŠุซ ูŠุชู… ุฅุฑุฌุงุนู‡ ุฐุงุชู‡ ููŠ ุญุงู„ุฉ ู…ุญุงูˆู„ุฉ ุงู„ุฅู†ุดุงุก ู…ุฑุฉ ุฃุฎุฑู‰ุŒ ูˆูŠูƒูˆู† ู…ู† ู†ูˆุน Privateูˆ static.

  • ุฏุงู„ุฉ getInstance (ุฃูˆ ุฃูŠ ุจุฏูŠู„ ู„ู‡ุง) ู„ูŠุชู… ู…ู† ุฎู„ุงู„ู‡ุง ุงู„ูˆุตูˆู„ ุฅู„ู‰ ุงู„ instance ุงู„ูˆุญูŠุฏุŒ ุจุญูŠุซ ูŠูƒูˆู† ู†ูˆุน ุงู„ุจูŠุงู†ุงุช ุงู„ุชูŠ ูŠุชู… ุฅุฑุฌุงุนู‡ุง ู…ู† ุงู„ุฏุงู„ุฉ ู…ู† ู†ูุณ ู†ูˆุน ุงู„ Class ุงู„ุฐูŠ ูŠุญุชูˆูŠู‡ุงุŒ ูˆูŠุฌุจ ุฃู† ุชูƒูˆู† ู…ู† ู†ูˆุน public ูˆ static.

Simple Factory Design Pattern ๐Ÿญ

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) ู…ู† ุฅู†ุดุงุก ุงู„ูƒุงุฆู† ุงู„ู…ู†ุงุณุจ ู…ู† ุฎู„ุงู„ ุชุญุฏูŠุฏ ุงู„ู†ูƒู‡ุฉ ูˆุงู„ุดูƒู„ ู„ู„ุฏุงู„ุฉ ูˆู‡ูŠ ุงู„ุชูŠ ุชูƒูู„ุช ุจุนู…ู„ูŠุฉ ุชุญุฏูŠุฏ ุงู„ู†ูˆุน ุงู„ู…ู†ุงุณุจ.

ุฃู…ุซู„ุฉ ๐ŸŽˆ

Structural Patterns:

ุชู‡ุชู… ุจุงู„ูŠุฉ ุชุฌู…ุน ุงู„ Classes ูˆ ุงู„ Objects ููŠ ุชูƒูˆูŠู† ุฃูƒุจุฑ.

ู†ู‚ุงุท ู…ู‡ู…ุฉ: โš 

  • ุงู„ Structural Class paatterns ุชุณุชุฎุฏู… ู…ุจุงุฏู‰ุก ุงู„ูˆุฑุงุซุฉ ููŠุชูƒูˆูŠู† ุงู„ interfaces ุฃูˆ ุงู„ Classes.
  • ุงู„ Structural Object Patterns ุชุตู ุงู„ูŠุฉ ุชุฌู…ุน ุฃูƒุซุฑ ู…ู† ุฃูˆุจุฌูƒุช ู„ู„ุญุตูˆู„ ุนู„ู‰ ูˆุธุงุฆู ุฌุฏูŠุฏุฉ.
  • ุชุนุทูŠ ุงู„ Structural Object Patterns ู†ูˆุนุงู‹ ู…ู† ุงู„ู…ุฑูˆู†ุฉ ููŠ ุนู…ู„ูŠุฉ ุงู„ Composition ู„ู„ุฃูˆุจุฌูƒุชุŒ ูˆุฐู„ูƒ ู†ุงุจุน ู…ู† ุงู„ู‚ุฏุฑุฉ ุนู„ู‰ ุชุบูŠูŠุฑ ุงู„ Composition ููŠ ุฃุซู†ุงุก ุงู„ุชู†ููŠุฐ (Dynamically).

Adapter Design pattern ๐Ÿ”Œ

ุชุนุฑูŠู:

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?

ูŠู‚ุตุฏ ุจู‡ุง ูˆุณูŠุทุŒ ูŠุฃุชูŠ ุจูŠู† ุดูŠุฆูŠู† ุบูŠุฑ ู…ุชูˆุงูู‚ูŠู† ู„ูŠู‚ูˆู… ุจุนู…ู„ูŠุฉ ุงู„ุฑุจุท ุจูŠู†ู‡ู…ุงุŒ ูููŠ ุงู„ุญูŠุงุฉ ุงู„ูˆุงู‚ุนูŠุฉ ุนู„ู‰ ุณุจูŠู„ ุงู„ู…ุซุงู„ุŒ ู„ูˆ ู‚ู…ุช ุจุงู„ุณูุฑ ุฅู„ู‰ ุฏูˆู„ุฉ ุฃุฎุฑู‰ ู‚ุฏ ุชุฌุฏ ุจุฃู† ุดุงุญู† ู‡ุงุชููƒ ู„ุง ูŠุชูˆุงูู‚ ู…ุน ุงู„ู‚ุงุจุณ ุงู„ู…ุชูˆุงุฌุฏ ู‡ู†ุงูƒุŒ ูุณุชุญุชุงุฌ ุฅู„ู‰ ูˆุณูŠุท (Adapter) ู„ูŠู…ูƒู†ูƒ ุฃู† ุชู‚ูˆู… ุจุงู„ุฑุจุท ุจูŠู†ู‡ู…ุงุŒ ูˆุงู„ู„ุฐูŠ ุณูŠูƒูˆู† ุดูƒู„ู‡ ู‡ูƒุฐุง:

Adapter example

ูˆุจุงู„ู…ุซู„ ููŠ ุนุงู„ู… ุงู„ Software Engineering, ู„ุฏูŠู†ุง ู…ุง ูŠุณู…ู‰ ุจุงู„ Adapter Design pattern, ุจุญูŠุซ ูŠู‚ูˆู… ุนู„ู‰ ุฃู†ุดุงุก ูƒู„ุงุณ ู…ู†ูุตู„ ูŠุนู…ู„ ูƒูˆุณูŠุท ุจูŠู† ูƒู„ุงุณูŠู† ุบูŠุฑ ู…ุชูˆุงูู‚ูŠู†.

Open-Closed Principal

ู‚ุฏ ูŠุชุจุงุฏุฑ ู„ู„ุฐู‡ู† ุชุณุงุคู„ุŒ ุจุฃู†ู‡ ู„ู…ุงุฐุง ู„ุง ูŠู…ูƒู†ู†ุง ุชุนุฏูŠู„ ุฃุญุฏ ุงู„ 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!

Composite Design pattern ๐Ÿงฑ

ุชุนุฑูŠู:

 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 ุจุญูŠุซ ูŠู…ูƒู†ู†ุง ุชุฎูŠู„ ุงู„ุณูŠุงุฑุฉ ุจุดูƒู„ ุฃูุถู„ ุจุงู„ุดูƒู„ ุงู„ุงุชูŠ:

Car composition illustration

ูุจู‡ุฐุง ุงู„ุดูƒู„ ูŠู…ูƒู†ู†ูŠ ุฃู† ุฃู‚ูˆู… ุจุชู‚ุณูŠู… ุงู„ุดูŠุก ุฅู„ู‰ ู…ุฎุทุท ุดุฌุฑูŠ ุจุญูŠุซ ูŠู…ูƒู†ู†ูŠ ู…ู† ู…ุนุฑูุฉ ุงู„ู…ูƒูˆู†ุงุช ู„ูƒู„ ุฌุฒุก ุจุดูƒู„ ุชุชุงุจุนูŠุŒ ูู…ูƒูˆู†ุงุช ุงู„ุณูŠุงุฑุฉ ู…ุซู„ุงุŒ ู‡ูŠ ู‡ูŠูƒู„ ุงู„ุณูŠุงุฑุฉุŒ ูˆุงู„ู…ุญุฑูƒ ูˆู…ู† ุซู… ู…ูƒู† ุชู‚ุณูŠู… ู…ูƒูˆู†ุงุช ุงู„ู…ุญุฑูƒ ู…ุซู„ุงู‹ุŒ ุฅู„ู‰ ุบุฑู ุงุญุชุฑุงู‚ ูˆ ุดู…ุนุงุงุช ุงู„ุงุญุชุฑุงู‚ (ุจูˆุฌูŠู‡ุงุช)ุŒ ูˆูƒุฐู„ูƒ ูŠู…ูƒู†ู†ุง ุชู‚ุณูŠู… ู‡ูŠูƒู„ ุงู„ุณูŠุงุฑุฉ ุฅู„ู‰ ุฃุจูˆุงุจูˆ ุฅุทุงุฑุงุชุŒ ุจุญูŠุซ ูŠุญุชูˆูŠ ูƒู„ 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 ู…ู† ุงู„ุชุนุงู…ู„ ู…ุน ุฃูŠ ุดุฌุฑุฉ ูุฑุนูŠุฉ (ุนู†ุตุฑ ูˆู…ุฌู…ูˆุนุฉ ุงู„ุนู†ุงุตุฑ ุงู„ู…ูƒูˆู†ุฉ ู„ู‡) ุจุดูƒู„ ู…ู†ูุตู„ุŒ ูˆุงู„ุฎุญุตูˆู„ ุนู„ู‰ ุงู„ู…ุนู„ูˆู…ุงุช ุงู„ุฎุงุตุฉ ุจู‡ู… ุจูƒู„ ุณู‡ูˆู„ุฉุŒ.

Beahvioral Patterns:

ุชู‡ุชู… ุจุงู„ุฎูˆุงุฑุฒู…ูŠุงุช ูˆุจุชู‚ุณูŠู… ุงู„ู…ุณุคูˆู„ูŠุงุช ุจูŠู† ุงู„ Objects.

ู†ู‚ุงุท ู…ู‡ู…ุฉ: โš 

  • ุงู„ Behavioural Class patterns ุชุณุชุฎุฏู… ู…ุจุงุฏู‰ุก ุงู„ูˆุฑุงุซุฉ ู„ุชู‚ุณูŠู… ุงู„ู…ู‡ุงู… ูˆุงู„ุณู„ูˆูƒ ุนู„ู‰ ุงู„ classes ุงู„ู…ูƒูˆู†ุฉ ู„ู‡ุง.
  • ุงู„ Behavioural Object patterns ุชุณุฏุฎุฏู… ุงู„ composition ุจุฏู„ุงู‹ ู…ู† ุงู„ูˆุฑุงุซุฉุŒ ูŠูˆุถุญ ุจุนุถู‡ุง ุนู† ูƒูŠู ูŠู…ูƒู† ู„ุฃูƒุซุฑ ู…ู† Object ุงู„ุชุนุงูˆู† ููŠ ุฅู†ุฌุงุฒ ู…ู‡ู…ุฉ ู…ุนูŠู†ุฉ ู„ุงูŠู…ูƒู† ู„ุฅุญุฏุงู‡ุง ุฅู†ุฌุงุฒู‡ุง ู„ูˆุญุฏู‡.

Command Design Pattern ๐Ÿ–‡

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
0

Observer Design Pattern ๐Ÿ”ญ

Observer (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

ู…ุตุงุฏุฑ ู…ููŠุฏุฉ ๐Ÿ’พ

About

Design patterns ๐ŸŽˆ

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 83.6%
  • TypeScript 8.5%
  • Python 7.9%