اذا لم تجد ما تبحث عنه يمكنك استخدام كلمات أكثر دقة.
نمط واجهة المبنى (بالإنجليزية: facade pattern) (يلفظ (فساد) façade أيضاً) هو نمط تصميم برامج شائع الاستخدام في البرمجة كائنية التوجه. تشبه الواجهة في العمارة، الواجهة هي كائن يعمل كواجهة أمامية تخفي كودًا أساسيًا أو بنيويًا أكثر تعقيدًا. يمكن للواجهة:
غالبًا ما يستخدم المطورون نمط تصميم الواجهة عندما يكون النظام معقدًا للغاية أو يصعب فهمه لأن النظام يحتوي على العديد من الأصناف المترابطة أو لأن الكود المصدري غير متوفر. يخفي هذا النمط تعقيدات النظام الأكبر ويوفر واجهة أبسط للعميل. وعادةً ما تتضمن صنف مغلّف واحد يحتوي على مجموعة من الأعضاء التي يطلبها العميل. يدخل هؤلاء الأعضاء إلى النظام نيابة عن عميل الواجهة ويخفي تفاصيل التنفيذ.
نمط تصميم الواجهة هو واحد من ثلاثة وعشرون نمطًا من أنماط تصميم GoF المعروفة التي تصف كيفية حل مشاكل التصميم المتكررة لتصميم برنامج مرن وقابل لإعادة الاستخدام، أي الكائنات التي يسهل تنفيذها، وتغييرها، اختبار وإعادة استخدامها.
ما هي المشاكل التي يمكن أن يحلها نمط تصميم الواجهة؟
يشير العملاء الذين يصلون إلى نظام فرعي معقد مباشرة (يعتمد على) العديد من الكائنات المختلفة التي لها واجهات مختلفة (اقتران ضيق)، مما يجعل العملاء صعب التنفيذ والتغيير والاختبار وإعادة الاستخدام.
ما الحل الذي يصفه نمط تصميم الواجهة؟
تعريف كائن واجهة Facade من أجل ذلك.
يتيح هذا العمل من خلال كائن واجهة Facade لتقليل التبعيات على نظام فرعي. انظر أيضًا صنف UML ومخطط التسلسل أدناه.
يتم استخدام الواجهة عند الرغبة في استخدام واجهة أسهل أو أبسط لكائن أساسي. بدلاً من ذلك، يمكن استخدام محول عندما يجب أن يلتزم الغلاف بواجهة معينة ويجب أن يدعم السلوك متعدد الأشكال. يجعل الديكور من الممكن إضافة أو تغيير سلوك واجهة في وقت التشغيل.
عادة ما يتم استخدام نمط الواجهة عندما
في الرسم التخطيطي لصنف UML أعلاه، لا يصل صنف Client إلى أصناف النظام الفرعي مباشرة. بدلاً من ذلك، يعمل العميل Client من خلال صنف الواجهة Facade التي تطبق واجهة بسيطة من حيث (عن طريق التفويض إلى) أصناف النظام الفرعي ( Class1 و Class2 و Class3 ). يعتمد Client فقط على واجهة Facade البسيطة وهو مستقل عن النظام الفرعي المعقد. يعرض الرسم التخطيطي للتسلسل تفاعلات وقت التشغيل: يعمل كائن عميل Client خلال كائن واجهة Facade الذي يفوض الطلب إلى مثيلات الأصناف التالية Class1 و Class2 و Class3 التي تقوم بتنفيذ الطلب.
هذا مثال تجريدي لكيفية تفاعل العميل ("you") مع واجهة ("computer") لنظام معقد (أجزاء الكمبيوتر الداخلية، مثل وحدة المعالجة المركزية والأجهزة).
#include <memory> class CPU { public: void Freeze(); void Jump(long position); void Execute(); }; class HardDrive { public: char* Read(long lba, int size); }; class Memory { public: void Load(long position, char* data); }; // Facade: class ComputerFacade { public: void Start() { cpu_->Freeze(); memory_->Load(kBootAddress, hard_drive_->Read(kBootSector, kSectorSize)); cpu_->Jump(kBootAddress); cpu_->Execute(); } private: std::unique_ptr<CPU> cpu_ = std::make_unique<CPU>(); std::unique_ptr<Memory> memory_ = std::make_unique<Memory>(); std::unique_ptr<HardDrive> hard_drive_ = std::make_unique<HardDrive>(); }; int main() { ComputerFacade computer; computer.Start(); }
namespace DesignPattern.Facade { class SubsystemA { public string OperationA1() { return "Subsystem A, Method A1 "; } public string OperationA2() { return "Subsystem A, Method A2 "; } } class SubsystemB { public string OperationB1() { return "Subsystem B, Method B1 "; } public string OperationB2() { return "Subsystem B, Method B2 "; } } class SubsystemC { public string OperationC1() { return "Subsystem C, Method C1 "; } public string OperationC2() { return "Subsystem C, Method C2 "; } } public class Facade { private readonly SubsystemA a = new SubsystemA(); private readonly SubsystemB b = new SubsystemB(); private readonly SubsystemC c = new SubsystemC(); public void Operation1() { Console.WriteLine("Operation 1 " + a.OperationA1() + b.OperationB1() + c.OperationC1()); } public void Operation2() { Console.WriteLine("Operation 2 " + a.OperationA2() + b.OperationB2() + c.OperationC2()); } } }
namespace DesignPattern.Facade.Sample { // The 'Subsystem ClassA' class class CarModel { public void SetModel() { Console.WriteLine(" CarModel - SetModel"); } } /// <summary> /// The 'Subsystem ClassB' class /// </summary> class CarEngine { public void SetEngine() { Console.WriteLine(" CarEngine - SetEngine"); } } // The 'Subsystem ClassC' class class CarBody { public void SetBody() { Console.WriteLine(" CarBody - SetBody"); } } // The 'Subsystem ClassD' class class CarAccessories { public void SetAccessories() { Console.WriteLine(" CarAccessories - SetAccessories"); } } // The 'Facade' class public class CarFacade { private readonly CarAccessories accessories; private readonly CarBody body; private readonly CarEngine engine; private readonly CarModel model; public CarFacade() { accessories = new CarAccessories(); body = new CarBody(); engine = new CarEngine(); model = new CarModel(); } public void CreateCompleteCar() { Console.WriteLine("******** Creating a Car **********"); model.SetModel(); engine.SetEngine(); body.SetBody(); accessories.SetAccessories(); Console.WriteLine("******** Car creation is completed. **********"); } } // Facade pattern demo class Program { static void Main(string[] args) { var facade = new CarFacade(); facade.CreateCompleteCar(); Console.ReadKey(); } } }
/* Comp class CPU { public void freeze() { ... } public void jump(long position) { ... } public void execute() { ... } } class HardDrive { public byte[] read(long lba, int size) { ... } } class Memory { public void load(long position, byte[] data) { ... } } /* Facade */ class ComputerFacade { private final CPU processor; private final Memory ram; private final HardDrive hd; public ComputerFacade() { this.processor = new CPU(); this.ram = new Memory(); this.hd = new HardDrive(); } public void start() { processor.freeze(); ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE)); processor.jump(BOOT_ADDRESS); processor.execute(); } } /* Client */ class You { public static void main(String[] args) { ComputerFacade computer = new ComputerFacade(); computer.start(); } }
# Complex Parts class CPU def freeze; end def jump(position); end def execute; end end class Memory def load(position, data); end end class HardDrive def read(lba, size); end end # Facade class ComputerFacade def initialize @processor = CPU.new @ram = Memory.new @hd = HardDrive.new end def start @processor.freeze @ram.load(BOOT_ADDRESS, @hd.read(BOOT_SECTOR, SECTOR_SIZE)) @processor.jump(BOOT_ADDRESS) @processor.execute end end # Client computer_facade = ComputerFacade.new computer_facade.start
/** * The complicated, underlying logic. */ class CPU { public function freeze() {/* ... */} public function jump($position) {/* ... */} public function execute() {/* ... */} } class Memory { public function load($position, $data) {/* ... */} } class HardDrive { public function read($lba, $size) {/* ... */} } /** * The facade that users would be interacting with. */ class ComputerFacade { protected CPU $cpu; protected Memory $memory; protected HardDrive $hd; public function __construct() { $this->cpu = new CPU; $this->memory = new Memory; $this->hd = new HardDrive; } public function start() : void { $this->cpu->freeze(); $this->memory->load(BOOT_ADDRESS, $this->hd->read(BOOT_SECTOR, SECTOR_SIZE)); $this->cpu->jump(BOOT_ADDRESS); $this->cpu->execute(); } } /** * How a user could start the computer. */ $computer = new ComputerFacade; $computer->start();
""" Facade pattern example. """ # Complex computer parts class CPU(object): """ Simple CPU representation. """ def freeze(self) -> None: print("Freezing processor.") def jump(self, position) -> None: print("Jumping to:", position) def execute(self) -> None: print("Executing.") class Memory(object): """ Simple memory representation. """ def load(self, position, data): print("Loading from {0} data: '{1}'.".format(position, data)) class SolidStateDrive(object): """ Simple solid state drive representation. """ def read(self, lba, size): return "Some data from sector {0} with size {1}".format(lba, size) class ComputerFacade(object): """ Represents a facade for various computer parts. """ def __init__(self) -> None: self.cpu = CPU() self.memory = Memory() self.ssd = SolidStateDrive() def start(self) -> None: self.cpu.freeze() self.memory.load("0x00", self.ssd.read("100", "1024")) self.cpu.jump("0x00") self.cpu.execute() computer_facade = ComputerFacade() computer_facade.start()
Output:
Freezing processor. Loading from 0x00 data: 'Some data from sector 100 with size 1024'. Jumping to: 0x00 Executing.
""" Facade pattern example. """ class CPU { freeze() { "$this freezing" | Out-Host } jump($position) { "$this jump to $position" | Out-Host } execute() { "$this execute" | Out-Host } } class Memory { load($position, $data) { "$this load $position $data" | Out-Host } } class HardDrive { [object] read($lba, $size) { "$this read $lba $size" | Out-Host return "(some data)" } } # Façade class Computer { hidden [CPU]$cpu hidden [Memory]$memory hidden [HardDrive]$hardDrive Computer() { $this.cpu = New-Object CPU $this.memory = New-Object Memory $this.hardDrive = New-Object HardDrive } StartComputer() { $this.cpu.freeze() $this.memory.load(0, $this.hardDrive.read(0, 1024)) $this.cpu.jump(10) $this.cpu.execute() } } $façade = [Computer]::new() $façade.StartComputer()
Output:
CPU freezing HardDrive read 0 1024 Memory load 0 (some data) CPU jump to 10 CPU execute