Principle of design patterns

zhaozj2021-02-16  66

In recent years, everyone has begun to pay attention to design patterns. So, why should we use the design mode? So much

Why do you want to design this? To be honest, I haven't figured it out before. That is to see a "Design"

Pattern ", the heart is a bit married. So I bought the design model of the" Four Help ", and the results seem to be understood:

It seems to be understood, I will forget it for a while. It may be that I have a relatively "dull" :)) recently, I have some feelings. "

"Share", share with you, but also hope to advise!

Why advocate "Design Pattern"? The root cause is for code multiplexing, increasing maintainability. How much

Can you realize the code multiplexing? The OO community has several principles: "Open-Closed" principle (Open Closed Principal)

, The principle of replacement of Rivien, the synthesis reuse principle. The design pattern is to achieve these principles, thus achieving code multiplexing, increasing

The purpose of adding maintainability.

First, "open-closed" principle

This principle is proposed by "Bertrand Meyer". The original article is: "Software Entities SHOULD BE OPEN

For Extension, But Closed for Modification. "That is to say that the module should be open, and the revised customs

close. The module should be expanded as much as possible without modifying the original ("original", referring to the original code) code. So how to expand

? We look at the factory model "factory pattern": Suppose Zhongguancun has a boy who sells the pirated plate and the mask, we give

He designed a "CD Sales Management Software". We should first design a "CD" interface. Figure:

______________

| <> |

| CD |

| _____________ |

| Sell () |

| | |

| _____________ |

The pirated plates and the masks are their subclasses. The boy manages these discs via "DiscFactory". The code is:

Public class discfactory {

Public Static CD GetDisc (String Name) {

Return (CD) Class.Forname (Name) .GetInstance ();

}

}

Someone wants to buy theft, how to achieve it?

Public class kid {

Public static void main (String [] args) {

CD D = DiscFactory.getdisc ("Pirated Disk");

CD. Sell ();

}

}

If one day, this kid conscience found and started selling genuine software. It doesn't matter, we only have to create a "light"

The subclass of the disk "" genuine software "can be. No need to modify the original structure and code. How? Extension development, modification

shut down. "Open - Closed Principle"

The factory model is to expand the specific product, and some projects may need more scalability, and they must also have this "factory"

During expansion, it became "abstract factory model".

Second, the principle of replacement

The replacement principle is proposed by "Barbara Liskov". If the call is the parent class, then the replacement is complete

Can run. such as:

CD D = New pirated disk ();

D. Sell ();

Now you have to change the "pirated disk" class to the "cap" class, no problem, you can run it. The Java Compiler will check if the program meets the principle of replacement. Remember a principle of Java inherited? Access permission for subclass OVERLOAD methods cannot

Less than the access rights of the parent class corresponding method. For example, the method "sell" in "CD" is "public", then "piracy"

The "sell" method in the disk "and" Mozzare "cannot be package or private, compile can't pass. Why do you want this?

You think: If the "pirated" method is private. Then the following code cannot be executed:

CD D = New pirated disk ();

D. Sell ();

It can be said that the replacement principle is a foundation for inheritance multiplexing.

Third, the synthetic reuse

That is to say less inheritance, multi-use synthetic relationship. I used to write the process: there are several classes to be with the database

After dealing, write a class of database operations, and then inherit this inheritance of the class that is dealing with the database. The result is later

I have modified a method of database operation classes, and all classes need to be changed. "Making the whole body"! Object-oriented is

To limit the fluctuations to a small range.

In Java, you should try to program the interfab, not the class. In this way, the replacement subclass does not affect the call.

The code of the method. To contact others as little as possible, "Don't talk to strangers." In this way, the city gate is fire,

It is not until you have a pool. Scalability and maintenance can improve

Understand these principles, look at the design model, just how to achieve these principles on specific issues. Zhang Wuji

Tai Chi, forgot all the moves, knocked down "Xuan Po No. 2", the so-called "no move in the heart". Design mode can be said, if

First, learn a variety of models, and forget all the models and you want to achieve "no mode in your heart", you can say the highest realm of OO.

. Oh, funny, funny!

转载请注明原文地址:https://www.9cbs.com/read-21286.html

New Post(0)