لا ينبغي إجبار أي عميل على الاعتماد على طريقة لا يستخدمها
خذ بعين الاعتبار مثالًا لمساحة مكتبية حيث يتم تمثيل أجهزة الإخراج المختلفة باستخدام الكائنات
قبل مبدأ فصل الواجهة:
واجهة متعددة الوظائف
/** * @ImultiFunction interface has methods related to all output devices present in office space * for devices like Printer, Scanner, Fax machines, etc */ public interface IMultiFunction { public void print(); public void getPrintSpoolDetails(); public void scan(); public void scanPhoto(); public void fax(); public void internetFax(); }
يتم الآن تنفيذ الواجهة المشتركة المذكورة أعلاه لمختلف الأجهزة
فئة XeroxWorkCenter تتمتع بجميع الإمكانات
/** * * You must have seen Xerox work station device which has all the features in one like printing, scanning, xerox, * fax etc */ public class XeroxWorkCenter implements IMultiFunction { @Override public void print() { // real printing code } @Override public void getPrintSpoolDetails() { // real get print spool details code } @Override public void scan() { // read scanning code } @Override public void scanPhoto() { // real scan photo code } @Override public void fax() { // real fax code } @Override public void internetFax() { // real internet fax code } }
تتمتع فئة HpPrinterNScanner بإمكانيات الطباعة والمسح الضوئي
public class HpPrinterNScanner implements IMultiFunction { @Override public void print() { // real printing code } @Override public void getPrintSpoolDetails() { // real get print spool details code } @Override public void scan() { // read scanning code } @Override public void scanPhoto() { // real scan photo code } //Since HpPrinterNScanner has only printing and scanning abilities fax() and internetFax() will have empty body @Override public void fax() {} @Override public void internetFax() {} }
فئة CanonPrinter لديها القدرة على الطباعة فقط
public class CanonPrinter implements IMultiFunction { @Override public void print() { // real printing code } @Override public void getPrintSpoolDetails() { // real get print spool details code } //Since the CanonPrinter has only printing ability rest of the method will have an empty body @Override public void scan() {} @Override public void scanPhoto() {} @Override public void fax() {} @Override public void internetFax() {} }
تقنيات التعرف على مخالفات مزود خدمة الإنترنت
بعد مبدأ فصل الواجهة:
public interface IPrint { public void print(); public void getPrintSpoolDetails(); }
public interface IScan { public void scan(); public void scanPhoto(); }
public interface IFax { public void fax(); public void internetFax(); }
/** * * You must have seen the Xerox workstation device which has all the features in one like printing, scanning, xerox, fax, etc. */ public class XeroxWorkCenter implements IPrint,IScan,IFax { @Override public void print() { // real printing code } @Override public void getPrintSpoolDetails() { // real get print spool details code } @Override public void scan() { // read scanning code } @Override public void scanPhoto() { // real scan photo code ̰ } @Override public void fax() { // real fax code } @Override public void internetFax() { // real internet fax code } }
public class HpPrinterNScanner implements IPrint,IScan { @Override public void print() { // real printing code } @Override public void getPrintSpoolDetails() { // real get print spool details code } @Override public void scan() { // read scanning code } @Override public void scanPhoto() { // real scan photo code } }
public class CanonPrinter implements IPrint { @Override public void print() { // real printing code } @Override public void getPrintSpoolDetails() { // real get print spool details code } }
تتحمل كل واجهة مسؤولية واحدة وأصبحت أكثر نظافة الآن.
العلاقة التي تربط مزود خدمة الإنترنت بمبادئ SOLID الأخرى
مسؤولية فردية
بعد فصل الواجهات إلى واجهات مختلفة، أصبحت الآن جميع الواجهات مثل IPrint وIScan تتحمل مسؤولية واحدة
تبديل ليسكوف
نظرًا للفصل الآن، تتبع جميع الفئات (التي تنفذ الواجهات) استبدال Liskov، حيث يمكن استبدال جميع الأنواع الفرعية أو فئات التنفيذ بالمتغير المرجعي للواجهة
تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.
Copyright© 2022 湘ICP备2022001581号-3