前言: 相信有的人接触过会员系统,有的甚至写过。本文章不是为了说明怎么建设一个会员系统。而是提出一种思路。想学怎么建会员系统的可以不看本文,而想知道怎么建得更专业点的没事可以看看。哈哈。 问题的提出: 你想使用一个统一的类来创建会员,里面有普通
前言:相信有的人接触过会员系统,有的甚至写过。本文章不是为了说明怎么建设一个会员系统。而是提出一种思路。想学怎么建会员系统的可以不看本文,而想知道怎么建得更专业点的没事可以看看。哈哈。
问题的提出:
你想使用一个统一的类来创建会员,里面有普通会员,管理员,超级管理员……突然哪天老板说要给他加个上帝,郁闷……重新再修改类吗?哦好吧,…………终于写好了。555,使用前还要考虑是不是老板,还要判断,烦……如果你一开始就按工厂模式写的话或许就好多了。
工厂模式的作用:
可能你看到这里还有点郁闷,不知道工厂模式有什么用。来吧。那我们先玩下游戏先。可能放松一下你突然就领悟到了。
开个魔兽先,哇好多种族啊,有人族(Human),兽族(ORC),暗夜精灵族(Nighy Elf),不死族(Undead)。选了个暗夜精灵族(Nighy Elf),给电脑选了个兽族(ORC),其实我不是很会玩精灵族,不过听名字挺好听的,就选来玩一下^_^。因为小精灵(Wisp)能建造建筑,还能自爆。所以根据这个我们下面先写个小精灵(Wisp)的类。
电脑:555……你不给我苦工(Peon),我怎么建东西啊……这摆明欺负人……不……欺负电脑
我:呃……这个……对了,高兴过头了,忘记了,我帮你建一个……,这不是复制代码然后修改一下就行了吗,多简单~~
不死族:我也要僧侣
人族:我要农民
不死族,人族:你太不公平了!
我:(怎么办,难道又要再复制一个类……算了想想看有没有什么好办法),如果弄得多了,那每次要建造我都要记住不同的名字,多复杂啊。建造精灵是 new Wisp(),建造苦工是new Peon(),建造农民是new Peasant,建造侍僧又是new Acolyte,那不是很麻烦。怎么办呢?而且为了便于管理。我想把每个兵种的类分成一个独立文件,方便以后修改。那每次还要载入不同兵种的文件。实在太麻烦了。
电脑:你不会工厂模式吗?郁闷……
我:哦!对哦。嘻嘻(*^_^*)
观众:=_=///这么一大堆才进入主题啊
我:我这不是为了大家了解一下区别嘛……大家少安毋躁。
把刚才那段小精灵的类放在class_Wisp.php里面
把刚才那段苦工的类放在class_Peon.php里面
那么工厂呢。别急,建个class_Factory.php的文件。
至此,只要你想添加兵种,直接以class_兵种名.php做为文件名。(虽然你也可以使用__autoload自动载入类文件,但是以后会讲工厂模式更深层的应用,这可不只是为了解决__autoload也能解决的问题)然后用兵种名做为类名。然后实例化的时候只要如下:
这样就能实现对创建对象的任务进行封装。当然,这是最简单的工厂模式。还没考虑到其他因素。请看下面。
我:我狂造小精灵,哈哈,原来有漏洞,无限造精灵。
电脑:你……你……我也无限造苦工……
我:你耍赖……我不玩了……
电脑:是你先耍赖的,怪谁,哈哈……派苦工去灭了你……
我:……怎么办呢?
想知道?下回分解。

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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