在java跟php中,单例模式的区别
在java和php中,单例模式的区别
在java和php中都会用到很多的单例模式,那么它们有什么区别呢?昨天晚上我相互对比了一下,就做了个小小的总结,然后写了个很小的java单例和php单例
一、java 单例
//完成对用户登录的简单验证操作class User{ private String name=null;//用户名 private String passwd=null;//密码 private static User ins = new User("这里可以传入常量,如“chunge”,此时构造函数要有对应形参。但是不支持在静态初始化函数中对它进行外部传递赋值");//单例实例对象 //初始化用户名密码的代码块 { this.name="chunge"; this.passwd="111111"; } //私有构造函数 private User(String n){ this.name=n; } //获取单例对象 public static User getIns(){ return ins; } //判断用户信息是否正确 public void checkUserInfo(String name , String passwd ){ if(name==this.name && passwd==this.passwd){ System.out.println("欢迎您" + this.name); System.exit(1); } //用户密码不匹配 System.out.println("非法登录"); System.exit(1); }};public class oop{ public static void main(String args[]){ User U = User.getIns();//实例化对象 U.checkUserInfo("chunge","111111"); }};
<?php /**网站全局配置类**/ class Config{ private static $ins=""; private $config_file="";//配置文件 public $WEB_CONFIG=array(); //关闭初始化函数 private function __construct(){ //引入全局配置文件 require_once ROOT."config/global_config.php"; $this->config_file=ROOT."config/global_config.php"; //赋值 $this->WEB_CONFIG=$WEB_CONFIG; } //禁用克隆函数 private function __clone(){ return false; } //单例模式 public static function getIns(){ if(!(self::$ins instanceof self)){ //如果不存在 self::$ins=new self(); } return self::$ins; } ////动态输出配置信息 public function configGet($key){ if(array_key_exists($key,$this->WEB_CONFIG)){ return $this->WEB_CONFIG[$key]; }else{ echo "该配置信息不存在"; } } //动态添加配置信息传入键即可 public function configSet($key,$value){ //真正存入 $str=file_get_contents($this->config_file); //var_dump($str); $tmp=$this->configGet($key); $str=str_replace($tmp,$value,$str); file_put_contents($this->config_file,$str); //file_put_contents("ROOT1.php",$str); } ////动态输出配置信息 public function __get($key){ if(array_key_exists($key,$this->WEB_CONFIG)){ return $this->WEB_CONFIG[$key]; }else{ echo "该配置信息不存在"; } } //动态添加配置信息 public function __set($key,$value){ //只是暂时的配置,未真正存入 $this->WEB_CONFIG[$key]=$value; } }?>
总结:
/** 笔记:类名字User 单例变量ins 在java和php中,单例模式的区别 1.类可以进行单例模式,和php一样 2.java的单例模式变量声明: private/public static 类名字 对象名字 = new 类名字(); 例如:private static User ins = new User();//已经进行了new操作 php的单例模式变量声明: public/private static 对象名字=null;//只是声明,未进行new操作 例如:public static $ins = null; 3.java获得该单例对象方法 public static 类名字 方法名字(){ return 刚刚定义的对象名字 } 例如: public static User getIns(){ return ins;//注意这里无法用this.但在php中,可以用self::获取 } php中获取该单例对象方法 public function getIns(){ if(self::$ins==null || (!self::$ins instanceof self)){ self::$ins=new self(); } return self::$ins; } 4.在java中,单例模式如果初始化类中某些变量,因为其getIns()方法是静态的,如果变量不为静态的,则无法实例化该变量。即使构造函数需要传参,个人觉得可以直接用代码块 进行初始值的设置 在php中,单例模式可以对起进行初始值的赋值,如 public function getIns($can1,$can2.......){ if(self::$ins==null || (!self::$ins instanceof self)){ self::$ins=new self($can1,$can2........);//.......代表不限个数 } return self::$ins; } 这是只需要类的构造函数传入对应个数的参数即可。**/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

While installing a new version of an application, Windows may display this error message "An error occurred while parsing C:\\Windows\Microsoft.Net\Framework\v2.0.50727\Config\machine.configParser returned error 0xC00CE556". This problem also occurs when your system boots. No matter what situation you encounter this problem, .NETFramework is the real culprit behind the scenes. There are some very simple fixes you can use to stop this error code from appearing again. Fix 1 – Replace corrupted files You can easily replace corrupted ma from the original directory

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

With the continuous development of the PHP language, ThinkPHP, which is widely used in the PHP back-end framework, is also constantly improving. As business scenarios become increasingly complex, the demand for configuration management in ThinkPHP is also increasing. In this context, ThinkPHP provides rich configuration management functions. Today we will introduce how to implement configuration management through ThinkPHPConfig. 1. Introduction to ThinkPHPConfig ThinkPHPConfig is Thin

Before introducing the usage of self in Python, let’s first introduce the classes and instances in Python. We know that the most important concepts of object-oriented are classes and instances. Classes are abstract templates, such as abstract things like students. , can be represented by a Student class. Instances are specific "objects" created based on classes. Each object inherits the same methods from the class, but its data may be different. 1. Take the Student class as an example. In Python, the class is defined as follows: classStudent(object):pass(Object) indicates which class the class inherits from. The Object class is all

1. Introduction to CodeIgniter CodeIgniter is a lightweight and comprehensive PHP development framework designed to provide web developers with fast and powerful tools to build web applications. It is an open source framework that uses the MVC architecture pattern to achieve rapid development and basic functions, while supporting a variety of databases. 2. Introduction to the Config library The Config library is a component in the CodeIgniter framework and is used to configure and manage code. The Config library contains many

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!
