اذهب إلى المحتوى
  • 0

ما المقصود بمبدأ open-closed principle

Ahmed Yehia2

السؤال

Recommended Posts

  • 1

مبدأ open-closed أو (مفتوح للإمتداد - مغلق للتعديل) هو ثاني مبدأ من مبادي SOLID وهي المبادئ الخمسة الأساسية لتصميم نظام كائني التوجه بشكلٍ سليم, لنفهم المبدأ من خلال المثال التالي:

نفترض أن لدينا واجهة تُسمى Shape ويقوم بعمل implement لها أصناف مثل Square, Circle ولدينا صنف يُسمى AreaCalculator يحتوي على دالة تُسمى sum مسؤلة عن أخذ مجموعة من الأشكال وحساب مساحتها كما يتضح لنا في الشفرة التالية 

interface ShapeInterface
{
    public function area();
}

class Square implements ShapeInterface
{
    public int length;

    public function Square(int length)
    {
        this.length = length;
    }

    public function area()
    {
        return Math.pow(this.length, 2);
    }
}

class Circle implements ShapeInterface
{
    public int radius;

    public function Circle(int radius)
    {
        this.radius = radius;
    }
	
  	public function area()
    {
        return Math.pow(this.length, 2);
    }
   
}

class AreaCalculator
{
    protected ShapeInterface[] shapes;

    public function AreaCalculator()
    {
        this.shapes = [];
    }

    public function sum()
    {
        foreach (this.shapes : shape) {
            if (shape instanceof Square) {
                area+ = Math.pow(shape.length, 2);
            } elseif (shape instanceof Circle) {
                area+ = Math.pi * Math.pow(shape.radius, 2);
            }
        }

        return area;
    }
}

الأن لدينا مشكلة في تلك الشفرة, إن أردنا مثلًا إضافة شكل أخر مثل المثلث أو المستطيل أو أي شكل سنحتاج إلى الرجوع والتعديل مرة أخرى في الدالة sum وهذا سيؤدي إلى شفرة برمجية أكثر تعقيدًا وأكثر عرضة للأخطاء, لذا من الأفضل أن نجعل دالة المساحة موجودة في الأصناف المتوارثة من ShapeInterface وتقوم الدالة sum بنداء الدالة المسؤلة عن المساحة مباشرةً ليُصبح الشكل النهائي لشفرتنا كالتالي 

interface ShapeInterface
{
    public function area();
}

class Square implements ShapeInterface
{
    public int length;

    public function Square(int length)
    {
        this.length = length;
    }

    public function area()
    {
        return Math.pow(this.length, 2);
    }
}

class Circle implements ShapeInterface
{
    public int radius;

    public function Circle(int radius)
    {
        this.radius = radius;
    }
	
  	public function area()
    {
        return Math.pow(this.length, 2);
    }
   
}

class AreaCalculator
{
    protected ShapeInterface[] shapes;

    public function AreaCalculator()
    {
        this.shapes = [];
    }

    public function sum()
    {
        foreach (this.shapes : shape) {
            area+=shape.area();
        }

        return area;
    }
}

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...