搞定,吃饭

<?php
/*
The purpose of singleton pattern is to restrict instantiation of class to a single object. It
is implemented by creating a method within the class that creates a new instance of
that class if one does not exist. If an object instance already exists, the method simply
returns a reference to an existing object.
*/

class Logger {
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function logNotice($msg) {
        return 'logNotice: ' . $msg . '<br/>';
    }

    public function logWaring($msg) {
        return 'logWaring: ' . $msg . '<br/>';
    }

    public function logError($msg) {
        return 'logError: ' . $msg . '<br/>';
    }
}

echo Logger::getInstance()->logNotice('test-log-notice');
echo Logger::getInstance()->logWaring('test-log-waring');
echo Logger::getInstance()->logError('test-log-error');
?>

php单例型(singleton pattern)的更多相关文章

  1. 23种设计模式之单例(Singleton Pattern)

    单例 在软件系统中,经常有这样一些特殊的类,必须保证它们在系统中只存在一个实例(eg:应对一些特殊情况,比如数据库连接池(内置了资源)  全局唯一号码生成器),才能确保它们的逻辑正确性.以及良好的效率 ...

  2. Java设计模式—单例设计模式(Singleton Pattern)全然解析

    转载请注明出处:http://blog.csdn.net/dmk877/article/details/50311791 相信大家都知道设计模式,听的最多的也应该是单例设计模式,这种模式也是在开发中用 ...

  3. 单例(Singleton pattern)模式的七种写法

    转载请注明出处:http://www.cnblogs.com/smbk/ One: public class Singleton { private static Singleton instance ...

  4. 转载 单例(Singleton)模式)的误区

    在创建型模式中,单例(Singleton)模式和原型(Prototype)模式相对来说其用意更为简单明了.单例(Singleton)模式确保某类只有一个实例,且自行实例化并向整个系统提供这个实例:原型 ...

  5. 设计模式的征途—1.单例(Singleton)模式

    单例模式属于创建型模式的一种,创建型模式是一类最常用的设计模式,在软件开发中应用非常广泛.创建型模式将对象的创建和使用分离,在使用对象时无需关心对象的创建细节,从而降低系统的耦合度,让设计方案更易于修 ...

  6. 小菜学习设计模式(二)—单例(Singleton)模式

    前言 设计模式目录: 小菜学习设计模式(一)—模板方法(Template)模式 小菜学习设计模式(二)—单例(Singleton)模式 小菜学习设计模式(三)—工厂方法(Factory Method) ...

  7. 单例设计模式Singleton之懒加载模式(懒汉模式)【原】

    单例设计模式Singleton之懒加载模式(懒汉模式) SingletonLazy.java类 package kingtool; import kingtool.http.IPTool; publi ...

  8. 创建对象——单例(Singleton)模式

      单例(Singleton)模式:   保证一个类在系统里只能有一个对象被实例化.   如:缓存池.数据库连接池.线程池.一些应用服务实例等.   难点:在多线程环境中,保证实例的唯一性.     ...

  9. ThinkPHP5+Redis单例型购物车

    <?php /** * Redis + 单例型购物车 * param $basket 存储商品信息 * param $ins 存储实例化对象 */ namespace lib; use redi ...

随机推荐

  1. ros ap 的无线中继

    https://wiki.mikrotik.com/wiki/Manual:Interface/Wireless#Repeater Wireless repeater will allow to re ...

  2. CF461B Appleman and Tree

    CF461B Appleman and Tree 传送门 一道比较容易的树形DP. 考虑用\(dp[i][1]\)代表将\(i\)分配给\(i\)的子树内黑点的方案数,\(dp[i][0]\)代表将\ ...

  3. centos git编译

    1. 下载git源码 https://git-scm.com 2. 根据文档一步步操作 https://git-scm.com/book/en/v2/Getting-Started-Installin ...

  4. Notepad++使用NppFTP插件编辑linux上的文件

    [转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11351446.html   作者:jstarseven    码字挺辛苦的.....  一.N ...

  5. SpringBoot 应用篇之从 0 到 1 实现一个自定义 Bean 注册器

    191213-SpringBoot 应用篇之从 0 到 1 实现一个自定义 Bean 注册器 我们知道在 spring 中可以通过@Component,@Service, @Repository 装饰 ...

  6. Linux常用基础(一)

    1.命令解释器 shell---Unix操作系统 bash---Linux操作系统 本质:根据输入的命令,调用相应的执行程序. 2.Linux下的快捷键 (1)命令和路径补全 Tab键 (2)主键盘的 ...

  7. jupyter lab matplotlib 画图

    解决jupyter lab   plt.plot()  不画图的问题 import matplotlib.pyplot as plt %matplotlib inline #insert this c ...

  8. sqlalchemy 配合bootstrap-table实现后台分页

    创建公共的mysql连接函数 def db_session(): db_config='mysql+mysqldb://'+ mysqluser + ':' + mysqlpassword+'@'+m ...

  9. Java的常用API之Object类简介

    Object类 1.toString方法在我们直接使用输出语句输出对象的时候,其实通过该对象调用了其toString()方法. 2.equals方法方法摘要:类默认继承了Object类,所以可以使用O ...

  10. Android开发DDMS找不到Emulator Control的方法

    1.右键DDMS,点击reset. 2.window->show view->other->android->Emulator Control