In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail the "what is the use of the adapter pattern of the web design pattern", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the use of the adapter pattern of the web design pattern" can help you solve your doubts.
Intention
Convert the interface of one class to another interface that the customer wants. The Adapter pattern allows classes that cannot work together because of interface incompatibility.
Adapters are such a good name that readers can consider power plug converters, and we'll repeat this analogy over and over again.
motivation
Sometimes, a toolbox class designed for reuse cannot be reused simply because its interface does not match that required by professional applications.
It is generally not difficult to provide a class library or function library that can be reused, but it is difficult to predict how it will be used in the future, that is, to prepare an interface for the future. In other words, when we use existing class libraries, interfaces generally don't exactly match our needs.
For example, there is a drawing editor that allows users to draw and arrange basic elements (lines, polygons, text, etc.) to generate pictures and charts. The key abstraction of this drawing editor is the graphical object. Graphic objects have an editable shape and can draw themselves. The interface to a graphical object is defined by an abstract class called Shape. The drawing editor defines a subclass of Shape for each graphic object: the LineShape class corresponds to a straight line, the PolygonShape class corresponds to a polygon, and so on.
Suppose you want to develop a graphic editor that can support the editing of various graphics.
Classes for basic geometry like LineShape and PolygonShape are easier to implement because their drawing and editing capabilities are inherently limited. But for TextShape subclasses that can display and edit text, it is quite difficult to implement, because even basic text editing involves complex screen refresh and buffer management. At the same time, the finished user interface toolkit may already provide a complex TextView class for displaying and editing text. Ideally, we could reuse this TextView class to implement the TextShape class, but the designer of the toolkit did not consider the existence of Shape at the time, so TextView and Shape objects were not interchangeable.
To develop any software of a certain scale, there will always be some functions that you have to choose between implementing and using existing code. The result of this choice is generally affected by several factors: first of all, the difficulty of their own implementation, such as relatively simple, generally choose to implement their own, although it takes a little time, but the programmer can control everything. If the implementation is difficult, you will consider using existing class libraries, and so on. Generally speaking, referencing other libraries means the complication of compilation conditions, debugging and so on. Of course, the class library we use will not have exactly the interface we need.
An application may have classes with different interfaces and these interfaces are not compatible with each other. How can existing and unrelated classes like TextView work together in such an application? We can change the TextView class to make it compatible with the interface of the Shape class, but only if we have the source code for the toolkit. However, even if we get the source code, there is no point in modifying the TextView; because the toolkit should not have to adopt domain-specific interfaces just to implement an application.
What should we do when the class library we need to use doesn't have the interface we want? If the existing library is our own code, a considerable proportion of developers will choose to modify the interface of the existing library to adapt to the new usage. This is not a wise thing to do: because it results in code duplication and does not conform to the DRY (Don't Repeat Yourself) principle.
Instead of the above method, we can define a TextShape class that adapts the interface of TextView and the interface of Shape. We can do this in two ways: 1) inherit the interface of the Shape class and the implementation of TextView, or 2) use an instance of TextView as part of TextShape and use the interface of TextView to implement TextShape. These two methods correspond to the class and object versions of the Adapter schema. We call TextShape the adapter Adapter.
The recommended method is to define an intermediate layer, from the interface point of view, it combines the architectural requirements of the new software, it is a common subclass under a new architecture, with the required interface; from an implementation point of view, it uses the functions of the existing class library and does not need to be implemented from scratch. There are two ways to use existing libraries: one is IS A, that is, inheritance, and the other is the combination of HAS A square machines. These two approaches almost make object-oriented design a permanent topic, and the current trend is to use composition. But either way, it belongs to adapter mode.
After reading this, the article "what is the use of adapter patterns in web design patterns" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.