Home php教程 php手册 PHP+Java的开发经验:不要太面向对象

PHP+Java的开发经验:不要太面向对象

Jun 13, 2016 am 11:03 AM
How many object develop Now of experience language For

说起面向对象,现在很多语言多少都有一些。Java是传统的面向对象语言,PHP也有一些面向对象,但不是很好。完全的面向对象在具体的项目中(本文是Web开发项目),有时候其实并不是最好的选择。本文作者最终选择了PHP+Java的一个模式,并分享了一些自己的经验。

我较早接触了C++(高中),也较早接受了面向对象思想。面向对象思想更接近人的思考方式,其封装、继承等特性也常常能够简化一些工作,最重要的是思路看起来清晰多了。我对面向对象的思想深信不疑,直到有一天,我在WEB项目中陷入困惑。

我以前的工作也都是WEB开发相关的,通常项目里都是接口、实现,service层,DAO层这个样子。久而久之,就习惯了这种模式。后来,我开始自己做网站(自己运营),也沿用这种模式,花了一阵子时间把东西弄出来,可以跑了,问题也随之而来了。大家都知道,类似门户网站这样的东西,尤其是成长期的网站,可能会经常面对一些变更、扩展。它不像企业项目或是以稳定模式运营的网站,可以一套写好的程序一直用下去。可是JAVA的东西改动起来有点麻烦。

第一:项目里用了很多接口,业务变更有不少时候还要动接口。也许有人会说,这是因为需求没做好。是的,可以这么认为,但有个前提:需求根本没法一步到位,否则网站也别跑了,等需求分析做好,花都谢了。回忆一下这个经典的流程:要增加一个特性(页面部分暂不讨论),先增加或修改一个service接口;然后增加或修改其实现;接着视需要可能还要再增加或修改一个DAO层接口,对应的要增加或修改其实现;最后,我们真正要改的,往往只是一个SQL语句。

这一系列流程太过繁琐了。门户网站基本是展示信息的,它的业务逻辑,说到底基本上是SQL语句体现出来的。你想想看,网站上显示什么东西,怎么排序,怎么聚合,这些不都对应着相应的SQL语句么?如果你非要把DAO层写成基本的增删改,然后在service层大作文章去实现本来对应着一个SQL语句的业务罗辑,这有什么意思呢?纯粹为了分层而分层?为了面向对象而面向对象?更不用说那一堆接口,平白增加工作量。我当然不会否认接口在编程思想中的意义,只是传统的JAVA WEB编程中的那一堆接口,是否真正是一个合理应用呢?我看很多情况下不是。我后来用PHP重写我的项目中的一大部分功能,只用了几天的时间,没有分层,没有接口。这样带来的工作效率的提升,真是惬意!

第二:JAVA WEB项目的发布通常需要重启服务,造成WEB运行中断。不少人在讨论热部署,我不知道热部暑最终能达到怎样一个水平,但我想信无法达到像PHP那样随时修改文件随时生效。为了不中断服务,我通常选择做个集群,轮流发布。这样虽然仍旧有可能产生一些问题,但比中断应用好多了。可是集群会带来发布上的麻烦,集群本身也未必是我真正需要的。

随之而来的还有一些小问题,比如如果我项目中包含一些存储大量文件的文件夹,在发布的时候又要特别处理,这样很不爽。即使做软链接,发布时也免不了要做额外工作。这些问题,当然我想信会有更好的解决办法,我个人目前仍在探索中。

面对上述这些问题,我最终不再坚守面向对象。我把项目改成了前PHP后JAVA的形式。PHP做前端显得灵活多了,整个改版,PHP的逻辑部分没花多少时间,时间都用在了页面设计上;JAVA的后端又能保证稳定高效,易于安全设计。由此,我最终发出了“不要太面象对象”这一感叹。需求决定一切,跟着别人的思想走,跟入邪教没区别。

问题也还并没有结束,对JAVA后端部分,我还在探索一个基于插件式的可以热加载、缷截插件的CMS后端系统。呵,不能因为上面的原因把面象对象一棒子打死。

不过不管怎样,看了作者的描述,倒是不妨试试PHP+Java的组合:看看放弃一些面向对象能够带来些什么好处吧。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

Understanding VSCode: What is this tool used for? Understanding VSCode: What is this tool used for? Mar 25, 2024 pm 03:06 PM

"Understanding VSCode: What is this tool used for?" 》As a programmer, whether you are a beginner or an experienced developer, you cannot do without the use of code editing tools. Among many editing tools, Visual Studio Code (VSCode for short) is very popular among developers as an open source, lightweight, and powerful code editor. So, what exactly is VSCode used for? This article will delve into the functions and uses of VSCode and provide specific code examples to help readers

Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Exploring the boundaries of agents: AgentQuest, a modular benchmark framework for comprehensively measuring and improving the performance of large language model agents Apr 11, 2024 pm 08:52 PM

Based on the continuous optimization of large models, LLM agents - these powerful algorithmic entities have shown the potential to solve complex multi-step reasoning tasks. From natural language processing to deep learning, LLM agents are gradually becoming the focus of research and industry. They can not only understand and generate human language, but also formulate strategies, perform tasks in diverse environments, and even use API calls and coding to Build solutions. In this context, the introduction of the AgentQuest framework is a milestone. It not only provides a modular benchmarking platform for the evaluation and advancement of LLM agents, but also provides researchers with a Powerful tools to track and improve the performance of these agents at a more granular level

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values ​​are passed and object references are passed.

Is PHP front-end or back-end in web development? Is PHP front-end or back-end in web development? Mar 24, 2024 pm 02:18 PM

PHP belongs to the backend in web development. PHP is a server-side scripting language, mainly used to process server-side logic and generate dynamic web content. Compared with front-end technology, PHP is more used for back-end operations such as interacting with databases, processing user requests, and generating page content. Next, specific code examples will be used to illustrate the application of PHP in back-end development. First, let's look at a simple PHP code example for connecting to a database and querying data:

See all articles