What is a design pattern

Source: Internet
Author: User

Design patterns are a set of repeated use, most people know, through the classification of purpose, 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 more win, design patterns so that the code is really engineering, design pattern is the cornerstone of software engineering, like a block of bricks and stones in the building.

Gof's "design pattern" is the first time to promote the design model to the theoretical height, and standardize it, this book proposed 23 basic design patterns, since then, in the reusable object-oriented software development process, a large number of new design patterns continue to appear.

I. Design patterns and Frameworks

Now, reusable object-oriented software systems are now generally divided into three categories: the Application Toolbox and framework (frameworks), we usually develop the specific software is the application, Java API belongs to the Toolbox, and the framework is a class of specific software reusable design of a set of mutually cooperative classes. EJB (Enterprisejavabeans) is a framework that Java applies to enterprise computing.

The framework usually defines the design parameters of the whole structure class and object relationship of the application system, so that the specific application can focus on the specific details of the application itself. The framework mainly records the common design decision in the software application, the framework emphasizes the design reuse, so the design pattern must be used in the frame design.

In addition, design patterns help to understand the framework structure, the mature framework usually uses a variety of design patterns, if you are familiar with these design patterns, no doubt, you will quickly grasp the structure of the framework, our general developers if suddenly contact EJBJ2EE and other frameworks, will find it particularly difficult to learn, difficult to grasp, Then instead of mastering the design pattern, it gives you a sharp weapon to dissect the EJB or the Java EE system.

Ii. Principles of Design patterns

In recent years, everyone has begun to pay attention to design patterns. So, why do we use design patterns? Why are so many design patterns so designed? To tell you the truth, I didn't get it before. is to see everyone a "design pattern", the heart is a bit hollow. So bought the "gang" design mode, the result is indefinitely: Look at the time seems to understand, after a while forget. May be I compare "dull" bar:) Recently, a little sentiment. "Solo music is not as popular as music", share with you, also hope to advise!

Why advocate "design pattern"? The root cause is code reuse, which increases maintainability. So how do you implement code reuse? OO industry has predecessors of several principles: "Open-closed" principle (open Closed Principal), the Richter substitution principle, synthetic reuse principle. The design pattern is the implementation of these principles, so as to achieve code reuse, increase maintainability.

1, "open-closed" principle

This principle is 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 modification. The module should be extended as far as possible without modifying the original ("original", referring to the original code). So how to expand it? We look at the factory model "factory pattern": If Zhongguancun has a sale of pirated discs and pornography of the kid, we give him a "CD sales management software." We should design a "CD" interface first. As shown in figure: ______________

|<
>|
| 光盘    |
|_____________|
|+卖()    |
|       |
|_____________|

and pirated discs and pornography are their subclasses. The kid manages these CDs through "Discfactory". The code is:

public class DiscFactory{
   public static 光盘 getDisc(java/lang/String.java.html" target="_blank">String name){
    return (光盘)java/lang/Class.java.html" target="_blank">Class.forName(name).getInstance();
   }
}

Some people want to buy pirated discs, how to achieve it?

public class 小子{
   public static void main(java/lang/String.java.html" target="_blank">String[] args){
     光盘 d=DiscFactory.getDisc("盗版盘");
     光盘.卖();
   }
}

If one day, this kid conscience found, began to sell genuine software. It doesn't matter, we just have to create a "CD" Subclass of "genuine software" on it. You do not need to modify the original structure and code. What do you think? To the expansion development, to the modification closes. "Open-closed principle"

The factory model is to expand the specific product, some projects may need more extensibility, to the "factory" also expand, it becomes an "abstract factory model."

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.