Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the seven principles of the web design pattern

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what are the seven principles of web design pattern". In daily operation, I believe many people have doubts about what the seven principles of web design pattern are. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the seven principles of web design pattern?" Next, please follow the editor to study!

Seven principles of design patterns:

1: principle of single responsibility

For a class, that is, a class should be responsible for only one responsibility, such as class An is responsible for two different responsibilities: responsibility 1, responsibility 2.

When the requirement of responsibility 1 changes and changes A, it may cause an error in the execution of responsibility 2, so it is necessary to decompose the granularity of class An into A1 and A2.

2: interface isolation principle

The client should not rely on interfaces it does not need, that is, the dependence of one class on another should be based on the smallest interface uplink.

Split the interface

Package com.yuandatou;public class Segregation1 {public static void main (String [] args) {An a = new A (); B b = new B (); a.depend1 (b); / / Class A depends on class B a.depend2 (b); a.depend3 (b);} interface InterFace1 {void operation1 ();} interface InterFace2 {void operation2 () Void operation3 ();} interface InterFace3 {void operation4 (); void operation5 ();} static class B implements InterFace1,InterFace2 {@ Override public void operation1 () {System.out.println ("B implements operation1");} @ Override public void operation2 () {System.out.println ("B implements operation2") } @ Override public void operation3 () {System.out.println ("B implements operation3");}} static class D implements InterFace1,InterFace3 {@ Override public void operation1 () {System.out.println ("D implements operation1") } @ Override public void operation4 () {System.out.println ("D implements operation4");} @ Override public void operation5 () {System.out.println ("D implements operation5");}} static class A {public void depend1 (InterFace1 I) {i.operation1 () } public void depend2 (InterFace2 I) {i.operation2 ();} public void depend3 (InterFace2 I) {i.operation3 ();}} static class C {public void depend1 (InterFace1 I) {i.operation1 ();} public void depend2 (InterFace3 I) {i.operation4 () } public void depend3 (InterFace3 I) {i.operation5 ();}

3: rely on the principle of reversal

1: high-level modules should not rely on lower-level modules, both should rely on their abstraction

2: abstraction should not rely on details, details should rely on abstractions

3: the central idea of dependency inversion (inversion) is interface-oriented programming.

4: the principle of relying on inversion is based on the design idea that it is equivalent to the variability of details and that abstract things are much more stable.

An abstraction-based architecture is much more stable than a detail-based architecture. In java, abstraction refers to an interface or abstract class

The details are the specific implementation classes

5: the purpose of using interfaces or abstract classes is to develop specifications without involving any specific operations, leaving the task of showing details to their implementation classes to complete

Package com.yuandatou;public class Yilaidaozhuan {public static void main (String [] args) {Person person = new Person (); person.receive (new Email ()); person.receive (new Weixin ());} interface Ireceiver {public String getInfo ();} static class Email implements Ireceiver {@ Override public String getInfo () {return "received email message" }} static class Weixin implements Ireceiver {@ Override public String getInfo () {return "received weixin message";}} static class Person {public void receive (Ireceiver I) {System.out.println (i.getInfo ());}

4: Richter substitution principle

5: open and close principle ocp

1: the opening and closing principle (Open closed principle) is the most basic and important design principle in programming

2: a software entity such as classes, modules, and functions should be open to extensions (to the provider) and closed to modifications (to the user).

Build the framework with abstraction and extend the details with implementation.

3: when the software needs to change, try to achieve the change by extending the behavior of the software entity, rather than by modifying the existing code.

4: other principles are followed in programming, and the purpose of using design patterns is to follow the opening and closing principle.

Package com.yuandatou;public class Ocp {public static void main (String [] args) {GraphicEditor graphicEditor = new GraphicEditor (); graphicEditor.drawShape (new A ()); graphicEditor.drawShape (new B ()); graphicEditor.drawShape (new C ());} abstract class Draw {abstract void draw ();} class GraphicEditor {public void drawShape (Draw draw) {draw.draw () }} class An extends Draw {@ Override void draw () {System.out.println ("drawing an A figure");}} class B extends Draw {@ Override void draw () {System.out.println ("drawing a B figure");}} class C extends Draw {@ Override void draw () {System.out.println ("drawing a C figure");}}

6: Demeter's rule

7: the principle of synthetic reuse

Singleton mode

Simple factory model

1: the simple factory model is a creative model, which is a kind of factory model. The simple factory pattern is for a factory object to decide which instance of a product class to create. The simple factory model is the simplest and most practical model in the factory model family.

2: simple factory pattern: define a class that creates an object that encapsulates the behavior of the instantiated object

3: in software development, when we use a large number of objects to create some kind, class or batch of objects, we use the factory pattern.

Factory method mode:

Factory method pattern: defines an abstract method to create an object, and the subclass determines the class to instantiate. The factory method pattern defers the instantiation of objects to subclasses.

Personal understanding: just encapsulate a few simple factory models again

Abstract factory pattern

1: abstract factory pattern: defines a cluster of objects that interface uses to create or have dependencies without specifying specific classes

2: the abstract factory pattern can integrate the simple factory pattern and the factory method pattern.

3: at the design level, the abstract factory pattern is an improvement (or further abstraction) of the simple factory pattern.

4: abstract the factory into two layers, the AbsFactory (abstract factory) and the factory subclass of the concrete implementation. The programmer can use the corresponding factory subclass according to the type of object created. This turns a single simple factory into a factory cluster, which is more conducive to code maintenance and extension.

The prototype pattern (Prototype Pattern) is used to create duplicate objects while ensuring performance. It is a creative design pattern that provides the best way to create objects.

This pattern implements a prototype interface that is used to create a clone of the current object. This mode is used when the cost of creating an object directly is high. For example, an object needs to be created after a costly database operation. We can cache the object, return its clone on the next request, and update the database when needed to reduce database calls.

Introduction to shallow copy:

1: for member variables whose type is the basic data type, the shallow copy passes the value directly, that is, a copy of the attribute value is copied to the new object.

2: for a member variable whose data type is a reference data type, for example, a member variable is an array, an object of a class, etc., then a shallow copy will pass the reference, that is, just copy the reference value (memory address) of the member variable to the new object. Because in fact, the member variable of both objects points to the same instance. In this case, modify the member variable in one object to the value of the member variable that affects another object,

3: before we cloned the sheep, it is a shallow copy.

4: the shallow copy is implemented using the default clone () method

Basic introduction to Deep copy

1: copy the member variable values of all basic data types of the object

2: request storage space for all member variables of the reference data type, and copy the amount object referenced by each reference data type member variable until all objects that are reachable by that object. That is, to make a deep copy of an object, you need to copy the entire object.

Deep copy implementation 1: re-clone method to achieve deep copy

Deep copy implementation 2: deep copy through object serialization

The use of prototype pattern in spring

Agent mode

Personal understanding is that a class implements an interface, but I want to control the class to implement the interface through the proxy way. The static proxy method is to create a proxy factory, also to implement the interface, and then use an instance of the proxy factory to make the proxied class implement the interface.

The disadvantage of static proxy is that both the proxy and the proxy factory have to implement this interface, and it is too troublesome for a proxied object to correspond to a proxy factory, so dynamic proxies are used.

Cglib Agent

Both static proxy and JDK collocation pattern require that the target object is to implement an interface, but sometimes the target object is just a single object and does not implement any interface, so we can use the target object subclass to implement the proxy, which is the Cglib proxy.

Cglib agent, also known as subclass agent, builds a subclass object in memory to extend the function of the target object. Some books also attribute Cglib agents to dynamic proxies.

Cglib is a powerful high-performance code generation package that can extend JAVA classes and implement java interfaces at run time. It is widely used by many AOP frameworks, such as Spring AOP, to intercept implementation methods.

How to choose the proxy mode in AOP programming:

The target object needs to implement the interface and use the JDK proxy

The target object does not need to implement the interface, using the Cglib proxy

The bottom layer of the Cglib package is to convert bytecode and generate new classes by using the bytecode processing framework ASM

Observer mode

Observer mode

The Observer pattern (Observer Pattern) is used when there is an one-to-many relationship between objects. For example, when an object is modified, objects that depend on it are automatically notified. The observer model belongs to the behavioral model.

Introduction

Intent: defines an one-to-many dependency between objects. When the state of an object changes, all objects that depend on it are notified and updated automatically.

The main solution: the problem of notifying other objects of a change in the state of one object, and taking into account easy to use and low coupling to ensure a high degree of cooperation.

When to use: when the state of an object (target object) changes, all dependent objects (observer objects) will be notified for broadcast notification.

How to solve it: using object-oriented technology, you can weaken this dependency.

Key code: there is an ArrayList in the abstract class for observers.

Application examples: 1. During the auction, the auctioneer observes the highest bid and then notifies other bidders to bid. 2. In the Journey to the West, Wukong asked the Bodhisattva to surrender the Red Bodhisattva. The Bodhisattva sprinkled water to attract an old tortoise. The tortoise was the observer. He observed the Bodhisattva's act of sprinkling water.

Advantages: 1. The observer and the observed are abstract and coupled. 2. Establish a set of trigger mechanism.

Disadvantages: 1. If an observed object has many direct and indirect observers, it will take a lot of time to notify all the observers. 2. If there is a circular dependency between the observer and the observation target, the observation target will trigger a circular call between them, which may cause the system to crash. 3. The observer model has no corresponding mechanism to let the observer know how the observed object has changed, but only knows that the observed object has changed.

Use the scene:

An abstract model has two aspects, one of which depends on the other. Encapsulate these aspects in separate objects so that they can be changed and reused independently.

The change of one object will lead to the change of one or more other objects, but we don't know how many objects will change, which can reduce the coupling between objects.

An object must notify other objects without knowing who those objects are.

You need to create a trigger chain in the system, the behavior of An object will affect B object, and the behavior of B object will affect C object. You can use observer mode to create a chained trigger mechanism

Note: 1. There are already support classes for the Observer pattern in JAVA. 2. Avoid circular references. 3. If executed sequentially, an observer error will cause the system to jam, usually in an asynchronous way.

The observer mode uses three classes, Subject, Observer, and Client. The Subject object has methods to bind the observer to and unbind the observer from the Client object. We create Subject classes, Observer abstract classes, and entity classes that extend the abstract class Observer.

ObserverPatternDemo, our demo class uses Subject and entity class objects to demonstrate the observer pattern.

At this point, the study of "what are the seven principles of web design patterns" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report