التغليف (برمجة الحاسوب)

في أنظمة البرمجيات، يشير التغليف إلى تجميع البيانات مع الآليات أو الأساليب التي تعمل عليها. وقد يشير أيضًا إلى تقييد الوصول المباشر إلى بعض تلك البيانات، مثل مكونات الكائن. [ 1 ] وباختصار، يمنع التغليف الشيفرة الخارجية من الخوض في تفاصيل عمل الكائن الداخلية.

يُمكّن التغليف المطورين من تقديم واجهة متسقة مستقلة عن تفاصيل التنفيذ الداخلية. على سبيل المثال، يُمكن استخدام التغليف لإخفاء قيم أو حالة كائن بيانات مُهيكل داخل فئة . هذا يمنع العملاء من الوصول المباشر إلى هذه المعلومات بطريقة قد تكشف تفاصيل التنفيذ المخفية أو تُخلّ بثبات الحالة الذي تحافظ عليه الدوال.

يشجع التغليف المبرمجين على وضع جميع التعليمات البرمجية المتعلقة بمجموعة بيانات معينة في نفس الفئة، مما يُسهّل فهمها من قِبل المبرمجين الآخرين. التغليف أسلوب يُشجع على فصل المكونات .

تدعم جميع أنظمة البرمجة كائنية التوجه (OOP) التغليف، [ 2 ] [ 3 ] ولكن التغليف ليس حكرًا على البرمجة كائنية التوجه. كما توفر تطبيقات أنواع البيانات المجردة والوحدات والمكتبات التغليف أيضًا . وقد فسّر منظرو لغات البرمجة هذا التشابه من خلال الأنواع الوجودية . [ 4 ]

معنى

في لغات البرمجة الموجهة للكائنات ، والمجالات الأخرى ذات الصلة، يشير التغليف إلى أحد مفهومين مرتبطين ولكن متميزين، وأحيانًا إلى الجمع بينهما: [ 5 ] [ 6 ]

  • آلية لغوية لتقييد الوصول المباشر إلى بعض مكونات الكائن . [ 7 ] [ 8 ]
  • بنية لغوية تُسهّل تجميع البيانات مع الطرق (أو الوظائف الأخرى) التي تعمل على تلك البيانات. [ 1 ] [ 9 ]

يستخدم بعض الباحثين والأكاديميين في لغات البرمجة المعنى الأول وحده أو بالاشتراك مع المعنى الثاني كسمة مميزة للبرمجة الموجهة للكائنات ، بينما تعتبر بعض لغات البرمجة التي توفر الإغلاقات المعجمية التغليف سمة من سمات اللغة المتعامدة مع التوجه الكائني.

The second definition reflects that in many object-oriented languages, and other related fields, the components are not hidden automatically and this can be overridden. Thus, information hiding is defined as a separate notion by those who prefer the second definition.

The features of encapsulation are supported using classes in most object-oriented languages, although other alternatives also exist.

Encapsulation may also refer to containing a repetitive or complex process in a single unit to be invoked. Object-oriented programming facilitates this at both the method and class levels. This definition is also applicable to procedural programming.[10]

Encapsulation and inheritance

The authors of Design Patterns discuss the tension between inheritance and encapsulation at length and state that in their experience, designers overuse inheritance. They claim that inheritance often breaks encapsulation, given that inheritance exposes a subclass to the details of its parent's implementation.[11] As described by the yo-yo problem, overuse of inheritance and therefore encapsulation, can become too complicated and hard to debug.

Information hiding

Under the definition that encapsulation "can be used to hide data members and member functions", the internal representation of an object is generally hidden outside of the object's definition. Typically, only the object's own methods can directly inspect or manipulate its fields. Hiding the internals of the object protects its integrity by preventing users from setting the internal data of the component into an invalid or inconsistent state. A supposed benefit of encapsulation is that it can reduce system complexity, and thus increase robustness, by allowing the developer to limit the interdependencies between software components.

Some languages like Smalltalk and Ruby only allow access via object methods, but most others (e.g., C++, C#, Delphi or Java[12]) offer the programmer some control over what is hidden, typically via keywords like public and private.[8] ISO C++ standard refers to protected, private and public as "access specifiers" and that they do not "hide any information". Information hiding is accomplished by furnishing a compiled version of the source code that is interfaced via a header file.

في أغلب الأحيان، توجد طريقة لتجاوز هذا النوع من الحماية ، عادةً عبر واجهة برمجة تطبيقات الانعكاس (Ruby، Java، C#، إلخ)، وأحيانًا عبر آليات مثل تغيير أسماء المتغيرات ( Python )، أو استخدام كلمات مفتاحية خاصة كما في C++. أما الأنظمة التي توفر أمانًا قائمًا على إمكانياتfriend الكائنات (متوافقة مع نموذج إمكانيات الكائنات ) فهي استثناء، وتضمن تغليفًا قويًا.

أمثلة

تقييد حقول البيانات

توفر لغات مثل C ++ و C# و Java و PHP و Swift و Delphi طرقًا لتقييد الوصول إلى حقول البيانات .

فيما يلي مثال بلغة C# يوضح كيفية تقييد الوصول إلى حقل بيانات باستخدام privateكلمة مفتاحية:

class Program { public class Account { private decimal _accountBalance = 500.00m ;public decimal CheckBalance () { return _accountBalance ; } }static void Main () { Account myAccount = new (); decimal myBalance = myAccount . CheckBalance ();/* يمكن لهذه الدالة الرئيسية التحقق من الرصيد عبر الدالة العامة  * "CheckBalance" التي توفرها فئة "Account"  * ولكن لا يمكنها التلاعب بقيمة "accountBalance" */ } }

فيما يلي مثال بلغة جافا :

public class Employee { private BigDecimal salary = new BigDecimal ( 50000.00 ); public BigDecimal getSalary () { return this . salary ; }public static void main () { Employee e = new Employee (); BigDecimal sal = e . getSalary (); } }

Encapsulation is also possible in non-object-oriented languages. In C, for example, a structure can be declared in the public API via the header file for a set of functions that operate on an item of data containing data members that are not accessible to clients of the API with the extern keyword.[13]

// Header file "api.h"#pragma oncetypedefstructEntityEntity;// Opaque structure with hidden members// API functions that operate on 'Entity' objectsEntity*openEntity(intid);intprocessEntity(Entity*e);voidcloseEntity(Entity*e);

Clients call the API functions to allocate, operate on, and deallocate objects of an opaque data type. The contents of this type are known and accessible only to the implementation of the API functions; clients cannot directly access its contents. The source code for these functions defines the actual contents of the structure:

// Implementation file "api.c"#include"api.h"typedefstructEntity{intent_id;// ID numbercharent_name[20];// Name...andothermembers...}Entity;// API function implementationsEntity*openEntity(intid){// ... }intprocessEntity(Entity*e){// ... }voidcloseEntity(Entity*e){// ... }

Name mangling

Below is an example of Python, which does not support variable access restrictions. However, the convention is that a variable whose name is prefixed by an underscore should be considered private.[14]

classCar:def__init__(self)->None:self._maxspeed=200def drive ( self ) -> None : print ( f "السرعة القصوى هي { self . _maxspeed } ." )redcar : Car = Car () redcar . drive () # سيتم طباعة "السرعة القصوى هي 200."redcar._maxspeed = 10 redcar.drive () # سيطبع هذا 'السرعة القصوى هي 10 ' .

انظر أيضاً

الاقتباسات

  1. 1 2 روجرز، ويليام بول (18 مايو 2001). "التغليف ليس إخفاءً للمعلومات" . جافا وورلد . تم الاسترجاع في 20 يوليو 2020 .
  2. "ما هي البرمجة الكائنية التوجه (OOP)؟" . هندسة التطبيقات . تم الاسترجاع في 2024-03-02 .
  3. "التغليف في البرمجة الكائنية التوجه (OOP)" . www.enjoyalgorithms.com . تم الاطلاع عليه بتاريخ 2024-03-02 .
  4. بيرس 2002 ، § 24.2 تجريد البيانات باستخدام الوجوديات
  5. سكوت، مايكل لي (2006). براغماتية لغات البرمجة ( الطبعة الثانية). مورغان كوفمان. ص 481. ISBN   978-0-12-633951-2تُمكّن آليات التغليف المبرمج من تجميع البيانات والإجراءات الفرعية التي تعمل عليها معًا في مكان واحد، وإخفاء التفاصيل غير ذات الصلة عن مستخدمي التجريد.
  6. ديل، نيل ب.؛ ويمز، تشيب (2007). البرمجة وحل المشكلات باستخدام جافا ( الطبعة الثانية). جونز وبارتليت. ص 396. ISBN   978-0-7637-3402-2.
  7. ميتشل، جون سي. (2003). مفاهيم في لغات البرمجة . مطبعة جامعة كامبريدج. ص 522. ISBN  978-0-521-78098-8.
  8. 1 2 بيرس، بنجامين (2002). الأنواع ولغات البرمجة . مطبعة معهد ماساتشوستس للتكنولوجيا. ص 266. ISBN  978-0-262-16209-8.
  9. كونولي، توماس م.؛ بيج، كارولين إي. (2005). "الفصل 25: مقدمة إلى أنظمة إدارة قواعد البيانات الكائنية § مفاهيم البرمجة الكائنية". أنظمة قواعد البيانات: منهج عملي للتصميم والتنفيذ والإدارة ( الطبعة الرابعة). بيرسون للتعليم. ص 814. ISBN   978-0-321-21025-8.
  10. McDonough, James E. (2017). "Encapsulation". Object-Oriented Design with ABAP: A Practical Approach. Apress. doi:10.1007/978-1-4842-2838-8. ISBN 978-1-4842-2837-1 via O'Reilly.
  11. Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns. Addison-Wesley. ISBN 978-0-201-63361-0.
  12. 12Bloch 2018, pp. 73–77, Chapter §4 Item 15 Minimize the accessibility of classes and members.
  13. King, K. N. (2008). C Programming: A Modern Approach (2nd ed.). W. W. Norton & Company. p. 464. ISBN 978-0393979503.
  14. Bader, Dan. "The Meaning of Underscores in Python". Improve Your Python Skills. Retrieved 1 November 2019.

References

  • Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991.