Chia sẻ PHP - Design Patterns - Singleton & Registry

Chia sẻ PHP - Design Patterns - Singleton & Registry

Hôm nay mình chia sẻ tới mọi người một khía cạnh khác trên blog cá nhân của mình - code PHP. Mình giới thiệu tới mọi người 2 mẫu design patterns rất phổ biến và đơn giản, đó là mẫu Singleton và mẫu Registry.

Singleton là mẫu thiết kế rất phổ biến trong OOP. Với singleton, sẽ hạn chế việc tạo nhiều thực thể trên cùng  một lớp đối tượng, mà thay vào đó nó sẽ cho phép chúng ta sử dụng chung một thực thể, hay nói một cách khác là dùng một shared instance. Việc này sẽ có lợi điểm cho bộ nhớ của hệ thống, không phải cứ phát sinh  đối tượng tùm lum ra.

Singleton:

<?php
class Singleton
{
    private static $_instance;
    protected function __contruct(){}
    public static function getInstance()
    {
        if (!isset (self::$_instance)) 
            self::$_instance = new self;
        return self::$_instance;
    {
}

Registry: Đây là một mẫu thiết kế cũng đơn giản và cũng rất phổ biến. Mẫu thiết kế này cung cấp một cơ chế để lưu trữ các giá trị toàn cục (global) một cách dễ quản lý hơn.

<?php
class Registry
{
    private static $_instance;
    private $_storage;
    protected function __construct(){}
    public static function getInstance()
    {
        if (!isset (self::$_instance)) 
            self::$_instance = new self;
        return self::$_instance;
    }
    // Xét giá trị thông qua magic call
    public function __set($name, $value)
    {
        $this->_storage[$name] = $value; 
    }
    // Lấy giá trị đã lưu thông qua magic call
    public function __get($name)
    {
        if (isset ($this->_storage[$name])) 
            return $this->_storage[$name];
        throw new Exception ('Không tìm thấy giá trị'); 
    }
    protected function __clone(){}
}
Tags: 
Bạn thấy bài viết này như thế nào?: 
Average: 9.3 (3 votes)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Bình luận (0)

 

Add Comment

Filtered HTML

  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Các thẻ HTML được chấp nhận: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Tự động ngắt dòng và đoạn văn.

Plain text

  • No HTML tags allowed.
  • Các địa chỉ web và email sẽ tự động được chuyển sang dạng liên kết.
  • Tự động ngắt dòng và đoạn văn.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Setup Xdebug, Acquia Dev Desktop, and Netbeans in Windows 7

Cấu hình Xdebug, Acquia Dev Desktop, và Netbeans trên Windows 7

I usually use linux, but for various reasons I've needed to use windows lately and so I figured I would setup Xdebug, Acquia Dev Desktop, and Netbeans in Windows.

Thông tin Acquia certification cho Drupal

Thông tin Acquia certification cho Drupal

This is something I've been hoping to see in the community. While there have been other experiments around certification, we as a community have lacked a way to ensure professional standards across Drupal.

Giới thiệu 4 tools cho dân lập trình Drupal development năm 2015

Giới thiệu 4 tools cho dân lập trình Drupal development năm 2015

Developing Drupal sites can be quite a challenge and adventure. And this comes from those who call themselves Drupal developers

Công ty diệt chuột T&C

 

Diet con trung