Факэдэ загвар
Appearance
Факэдэ (Фасад) загвар нь Програм хангамжийн инженерчлэл сэдэвт хамаарагдах зохиомжийн загвар-уудын нэг бөгөөд ихэвчлэн Объект хандалтат технологи-той цуг ашиглагддаг.
Факэдэ нь обьект бөгөөд class library шиг томоохон кодтой хэсэг рүү хандах замыг хөнгөвчилж өгдөг. Факэдэ загварыг ашигласнаар дараахи давуу талуудтай:
- Факэдэ загварыг ихэнхи кодын ажлуудад ашиглахад тохиромжтой учраас software library-г ашиглах, ойлгох, тест хийхэд хялбар болдог;
- library ашиглаж байгаа кодыг уншихад илүү хялбар болгоно;
- Ихэнхи программын код факэдэ загварыг ашиглачих учраас library доторхи ажлууд ба гадаад код 2-ын хоорондын хамаарал багасна. Ингэснээр системийг илүү уян хатан хөгжүүлэх боломжтой болно.
- муу загварчлагдсан API-нуудын цуглуулгыг зөв зохион байгуулагдсан ганц API болгоно.
Бүтэц
[засварлах | кодоор засварлах]- Facade (Загвар:Pron-en)
- The facade class abstracts Packages 1, 2, and 3 from the rest of the application.
- Clients
- The objects using the Facade Pattern to access resources from the Packages.
- Packages
Жишээнүүд
[засварлах | кодоор засварлах]Java
[засварлах | кодоор засварлах]This is an abstract example of how a client ("you") interacts with a facade (the "computer") to a complex system (internal computer parts, like CPU and HardDrive).
/* Complex parts */
class CPU {
public void freeze() { ... }
public void jump(long position) { ... }
public void execute() { ... }
}
class Memory {
public void load(long position, byte[] data) {
...
}
}
class HardDrive {
public byte[] read(long lba, int size) {
...
}
}
/* Facade */
class Computer {
private CPU cpu=null;
private Memory memory=null;
private HardDrive hardDrive=null;
public Computer() {
this.cpu=new CPU();
this.memory=new Memory();
this.hardDrive=new HardDrive();
}
public void startComputer() {
cpu.freeze();
memory.load(BOOT_ADDRESS, hardDrive.read(BOOT_SECTOR, SECTOR_SIZE));
cpu.jump(BOOT_ADDRESS);
cpu.execute();
}
}
/* Client */
class You {
public static void main(String[] args) {
Computer facade = new Computer();
facade.startComputer();
}
}
C#
[засварлах | кодоор засварлах]using System;
namespace Facade
{
public class CPU
{
public void Freeze() { }
public void Jump(long addr) { }
public void Execute() { }
}
public class Memory
{
public void Load(long position, byte[] data) { }
}
public class HardDrive
{
public byte[] Read(long lba, int size) { return null; }
}
public class Computer
{
CPU _cpu = new CPU();
Memory _memory = new Memory();
HardDrive _hardDrive = new HardDrive();
public void StartComputer()
{
_cpu.Freeze();
_memory.Load(0x22, hardDrive.Read(0x66, 0x99));
_cpu.Jump(0x44);
_cpu.Execute();
}
}
public class SomeClass
{
public static void Main(string[] args)
{
Computer facade = new Computer();
facade.StartComputer();
}
}
}
Python
[засварлах | кодоор засварлах]# Complex parts
class CPU:
def freeze(self): pass
def jump(self, position): pass
def execute(self): pass
class Memory:
def load(self, position, data): pass
class HardDrive:
def read(self, lba, size): pass
# Facade
class Computer:
def __init__(self):
self.cpu = CPU()
self.memory = Memory()
self.hard_drive = HardDrive()
def start_computer(self):
self.cpu.freeze()
self.memory.load(0, self.hard_drive.read(0, 1024))
self.cpu.jump(10)
self.cpu.execute()
# Client
if __name__ == '__main__':
facade = Computer()
facade.start_computer()
PHP
[засварлах | кодоор засварлах]/* Complex parts */
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 ) { /* ... */ }
}
/* Facade */
class Computer
{
protected $cpu = null;
protected $memory = null;
protected $hardDrive = null;
public function __construct()
{
$this->cpu = new CPU();
$this->memory = new Memory();
$this->hardDrive = new HardDrive();
}
public function startComputer()
{
$this->cpu->freeze();
$this->memory->load( BOOT_ADDRESS, $this->hardDrive->read( BOOT_SECTOR, SECTOR_SIZE ) );
$this->cpu->jump( BOOT_ADDRESS );
$this->cpu->execute();
}
}
/* Client */
$facade = new Computer();
$facade->startComputer();
Javascript
[засварлах | кодоор засварлах]Uses Firebug.
/* Complex parts */
var CPU = function () {};
CPU.prototype = {
freeze: function () {
console.log('CPU: freeze');
},
jump: function (position) {
console.log('CPU: jump to ' + position);
},
execute: function () {
console.log('CPU: execute');
}
};
var Memory = function () {};
Memory.prototype = {
load: function (position, data) {
console.log('Memory: load "' + data + '" at ' + position);
}
};
var HardDrive = function () {};
HardDrive.prototype = {
read: function (lba, size) {
console.log('HardDrive: read sector ' + lba + '(' + size + ' bytes)');
return 'hdd data';
}
};
/* Facade */
var Computer = function () {
var cpu, memory, hardDrive;
cpu = new CPU();
memory = new Memory();
hardDrive = new HardDrive();
var constant = function (name) {
var constants = {
BOOT_ADDRESS: 0,
BOOT_SECTOR: 0,
SECTOR_SIZE: 512
};
return constants[name];
};
this.startComputer = function () {
cpu.freeze();
memory.load(constant('BOOT_ADDRESS'), hardDrive.read(constant('BOOT_SECTOR'), constant('SECTOR_SIZE')));
cpu.jump(constant('BOOT_ADDRESS'));
cpu.execute();
}
};
/* Client */
var facade = new Computer();
facade.startComputer();