Development of +MVC pattern in layered pattern development--Hanshunping employee database management

Source: Internet
Author: User
Tags extend

1. Layered mode

Before writing code in a layered design pattern, we were more involved in process-oriented and then cram, writing all the functions in one or two program code, which was only suitable for small personal projects. Because it is not conducive to reading and modification, only programming individuals are more familiar with the structure of the program. This is not conducive to the expansion of the program and collaborative development. So, we introduce a fixed pattern for programming, which makes all the code structures clear and easy to extend.

One of the patterns described here is the layering pattern. The program is divided into several levels: interface layer, business logic layer, data layer.

Interface layer: The main function is to realize the display of the interface. For example, to display the input box on the landing page, you need to put the form form in login.php. For example, to display employee information, you need emplist.php to display the information obtained from the database. However, whether the input data obtained in the login.php is legal and not relevant to the interface layer, the information interface layer displayed in the emplist.php does not have to be from where, it just completes the display. These data operations are given to the business logic layer, and all it takes is to accept the feedback from the business logic layer and make the corresponding display.

Business Logic Layer: The main function is to implement the logical processing, regardless of the interface layer. This layer mainly includes admin.class.php,adminservice.class.php,emp.class.php,empservice.class.php, etc., Admin, EMP represents two tables in the database.

Among them,Admin.class.php, Emp.class.php are classes, the member variables within the class are the properties of each table.

   AdminService.class.php, EmpService.class.php are classes, the main function is to provide function support for the operation of two tables;

In addition, the business logic layer includes the SqlHelper.class.php Helper class, which mainly provides MySQL database operation support. Other functions want to manipulate the database by invoking the helper class to manipulate the database, for example, to complete the add and revise operation.

Data layer: Refers to the MySQL database, the database is stored in the form of the admin and EMP to use. This is the lowest level of database resources.

For the employee information Management system, the three tiers are explained separately:

Interface layer: login.php emplist.php, etc.: login.php is responsible for displaying the form for user input, the legitimacy of input data to loginprocess.php to deal with, Loginprocess.php then calls the Checkadmin () function in AdminService.class.php to validate and return the result.

Emplist.php is responsible for displaying employee information, its data comes from empmanage.php, and this PHP calls EmpService.class.php to do the actual work.

Business Logic Layer: checkadmin () verifies that the information entered is legitimate by invoking the SQL helper class to manipulate the database.

SqlHelper.class.php Helper class provides a variety of basic operations on the database, such as: the constructor to achieve the connection, specify the database, set the encoding format, EXECUTE_DQL ($sql) Implementation of query instructions, return $RES;EXECUTE_DML ($sql) Implement additions and deletions, return $res;mysql_close_conn () to automatically close the connection defined in the helper class.

Data tier: is the lowest tier database.

2. Mvc mode

    • (Controller controllers)-responsible for forwarding requests and processing requests.
    • (views view)-graphical interface design for interface designers.
    • (model)-Programmer to write the functions that the program should have (implementation algorithm, etc.), database experts for data Management and database design (can achieve specific functions).
    • model is used to encapsulate data that is relevant to the business logic of an application and how the data is processed. "Model" has the right to direct data access , such as access to the database. "Model" does not depend on "view" and "Controller", that is, the model does not care how it will be displayed or how it is manipulated. However, changes in the data in the model are generally advertised through a refresh mechanism. To implement this mechanism, the views that are used to monitor this model must be registered in advance on this model, so that the view can understand the changes that have occurred on the data model. (Comparison: Viewer mode (software design mode))
    • views enable the purposeful display of data (in theory, this is not required). There is generally no logic on the program in the view. To implement the Refresh feature on the view, the view needs to access the data model it monitors, so it should be registered with the data it is monitoring beforehand.
    • The controller acts as an organizational function between different levels to control the flow of the application . It handles the event and responds. An "event" includes the user's behavior and changes on the data model.

3. Design mode

Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary. Design patterns are used in order to reuse code , make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in others in the system are multi-win; Design patterns make code production truly engineering ; Design patterns are the cornerstone of software engineering, like the structure of a building.

  

Design principle Editor Why should we advocate "Design pattern?" The root cause is for code reuse, which increases maintainability. So how do you implement code reuse? There are several principles of object-oriented: Open Closed PRINCIPLE,OCP, Richter substitution principle (Liskov Substitution principle,lsp), dependency reversal principle (Dependency inversion PRINCIPLE,DIP), the interface isolation principle (Interface segregation principle,isp), the synthesis/aggregation multiplexing principle (composite/aggregate reuse Principle,carp), Minimum knowledge principle (Principle of Least Knowledge,plk, also called Dimitri Law). The opening and closing principle has the idealistic color, it is the object-oriented design ultimate goal. The other several, can be regarded as the opening and closing principle of the implementation method. The design pattern is the realization of these principles, so as to achieve code reuse, increase maintainability. Opening and closing principleThis principle was proposed by Bertrand Meyer. The original text is: "Software entities should is open for extension,but closed for modification". This means that the module should be open for expansion and closed for modifications. The module should be extended as far as possible without modifying the original ("original", referring to the original code) code. So how do you extend it? We look at the Factory mode "factory pattern": If Zhongguancun has a selling pirated discs and pornography of the boy, we give him a "CD-ROM sales management software." We should design a "disc" interface first. [pre]______________|<>|CD ||_____________||+ sell () || ||_____________| [/pre]and the pirate disk and the pornography are its subclasses. The kid manages these discs through "discfactory". The code is:
1 2 3 5 6 7 8 9 10 11 publicclassDiscFactory{publicstatic光盘getDisc(Stringname){ //return(光盘)Class.forName(name).getInstance(); return(光盘)Class.forName(name).newInstance(); } }
Some people want to buy pirated disk, how to achieve it?
1 2 3 4 5 6 Publicclass Boy { publicstaticvoidmain ( String[]args) { disc D=discfactory.getdisc ( "Pirate disk" d. Sell (); } }
If one day, this boy conscience found, began to sell genuine software. It doesn't matter, we just have to create a "disc" subclass "Genuine software" on it, no need to modify the original structure and code. What do you think? Open for expansion, closed for modification--"opening and closing Principle". Factory mode is to expand the specific product, some projects may need more extensibility, to the "factory" also expand, it becomes "abstract factory model." principle of substitution on the Richter scalethe principle of substitution of the Richter scale was proposed by Barbara Liskov. If the parent class is called, then a subclass can be fully run. For example:
1 2 光盘d=new盗版盘();d.卖();
to change the "pirated disk" class to "pornography" class, no problem, can be run completely. The Java compiler checks to see if the program conforms to the Richter substitution principle. Remember a principle of Java inheritance? The subclass override method cannot access permissions less than the parent class's corresponding method. such as "CD" in the method of "sell" access is "public", then "piracy disk" and "pornography" in the "sell" method can not be protected or private, compilation cannot pass. Why would you do that? You want to: if the "sell" method of "pirated disk" is private. Then the following code cannot be executed:
1 2 光盘d=new盗版盘();d.卖();
It can be said that the Richter substitution principle is a basis for inheritance reuse. Dependency Reversal Principleabstractions should not be dependent on detail, and detail should be dependent on abstraction. Programming for the interface, not for implementation. pass parameters, or in a composite aggregation relationship, refer to the class with the highest level possible. mainly in the construction of objects, you can dynamically create a variety of concrete objects, of course, if some concrete classes are more stable, you do not have to get an abstract class to do its parent class, so there is superfluous feelingInterface Isolation principleExamples of custom services, each interface should be a role, not a lot, do not do things, should do the work. synthesis/polymerization multiplexing principlesThe synthesis/aggregation multiplexing principle (composite/aggregate reuse principle,carp) is often called the synthetic multiplexing principle. The synthesis/aggregation reuse principle is to use some existing objects inside a new object to make it part of the new object, and the new object is to reuse the existing functionality by delegating to those objects. It is designed to use composition/aggregation as much as possible and not to use inheritance. that means less inheritance, more use of synthetic relationships to achieve. I have written this program: there are several classes to deal with the database, wrote a database operation of the class, and then other databases dealing with the classes inherit this. As a result, I modified a method of the database operations class, and each class needed to be changed. "Reaching"! Object oriented is to limit the fluctuation to a small extent. in Java, you should try to program for interface rather than implementing classes. This way, replacing a subclass does not affect the code that invokes it. To make each class as little as possible with others, "Don't Talk to strangers." In this way, the fire of the city gate is not implicating. Scalability and maintainability can be improved. Understanding these principles, and then looking at design patterns, is just how to achieve these principles on specific issues. Zhang Mowgli Learn Taijiquan, forget all the moves, down the "Xuan Chou", the so-called "heart without Strokes." Design mode is a trick, if the first learning through various modes, and forget all the mode and arbitrary, is oo (object-oriented, object-oriented) the highest state. Oh, funny, funny! Minimum Knowledge principlealso called the Dimitri Law. Don't talk to strangers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.