Case Study: Design Patterns and structural characteristics of the code - adapter mode

One. Introduction to Design Patterns

Design patterns (Design pattern) represents a best practice, commonly used by experienced object-oriented software developers. Design patterns are solutions to common problems faced by software developers in the software development process. These solutions are numerous software developers through trial and error for a long period of time summed up.

Design patterns are set to be used repeatedly, most people know, after cataloging, code design experience summary. Use design patterns to reuse code, make the code easier to understand others, to ensure the reliability of the code. There is no doubt, in others in design mode systems are win-win for others, design patterns make code compiled real engineering, design patterns is the cornerstone of software engineering, like a piece of brick stone building of the same. Project rational use of design patterns can be the perfect solution to many problems, each mode corresponding to the principle of reality has its corresponding, each mode describes a constantly recurring problem around us, and the problem core solution, which is why design patterns that can be widely used.

two. The use of design patterns

Design Patterns in software development for two main purposes.

1. The common platform developers

Design pattern provides a standard term system, and in particular to a specific scenario. For example, Singleton design pattern implies the use of a single object, so that all are familiar with singleton design pattern developers can use a single object, and can tell each other this way, the program uses a singleton.

2. Best Practices

Design pattern has been developing for a long period of time, they provide the best solutions to common problems faced by the software development process. These learning mode helps inexperienced developer through a quick and easy way to learn the software design.

three. Type design patterns

Type design patterns

The reference design patterns Design Patterns - Elements of Reusable Object-Oriented Software (Chinese translation: Design Mode - object-oriented software elements, reusable) mentioned, a total of 23 design patterns. These patterns can be divided into three categories: Creating a schema (Creational Patterns), structural model (Structural Patterns), type of model (Behavioral Patterns). Of course, we will discuss another type of design patterns: J2EE design patterns.

The relationship between design patterns is shown below:

IV. Adapter mode

Adapter mode (Adapter Pattern) is used as a bridge between two of incompatible interfaces. Converting the interface of a class clients expect another interface. Adapter pattern makes those classes otherwise because of incompatible interfaces can not work together to work together. This type of design pattern belongs structural model, which combines the functionality of two separate interfaces. The main solution in the software system, often want some "existing object" into the new environment, new environmental requirements and the interface is now the object can not be met. For real-life examples, the reader is as an adapter between the memory card and laptop. Your memory card into the card reader, then the reader into the notebook, so you can read the memory card via a laptop.

Fives. Examples of application adapter

  In the foregoing basic concept introduces the adapter mode, in this mode, the adapter part to demonstrate the application in practice by some code examples. Andrews use adapter is very common, the code will be described below Adapter two practical examples of application.

1. ListView

Case code for the target: the left controls the user information entered in the database according to the attribute table stored in the read-related columns, then read data from the relational database tables, which will be displayed in the ListView control.

 

adapter implementation:

1) gets control content into the database

2) to query data from a database, the data is written to an array of data strings

 3) 将字符串数组中的值通过ArrayAdapter,显示在ListView控件中.

其中ArrayAdapter在安卓中为适配器的实现类,可以直接使用。

2.RecyclerView

ListView的扩展性相较于RecyclerView不够好,它只能实现数据纵向滚动的效果,如果我们想实现横向滚动和瀑布流的话,ListView是做不到的。

案例代码目标:将左图控件中用户填写的借款信息按照属性读取存储进数据库关系表的相关列,再从数据库的关系表中读取数据,将其显示在RecyclerView控件中。

 

②适配器的实现:

1)获取控件内容,写入数据库

2)自定义适配器类在RecyclerView中每一行的显示格式

 3)获取自定义每一行要显示的控件,从数据库中读取的数据,将其显示在控件中

 

4)定义RecyclerView中行行之间的样式,横向滚动或者为瀑布流

 六.总结

  Adapter 模式的经典实现在于将原本不兼容的接口融合在一起,使之能够很好的进行合作。但是,在实际开发中,Adapter模式也会可以根据实际情况进行适当的变更,最典型的就是 ListView 和 RecyclerView了,这种设计方式使得整个 UI 架构变得非常灵活,能够拥抱变化。所以在实际使用的时候,遵循上面说过的三种场景

1.系统需要使用现有的类,而此类的接口不符合系统的需要,即接口不兼容;

2.想要建立一个可以重复使用的类,用于与一些彼此之间没有太大联系的一些类,包括一些可能在将来引进的类一起工作;

3.需要一个统一的输出接口,而输入端的类型不可预知。

Adapter模式的优点

1.更好的复用性:系统需要使用现有的类,而此类的接口不符合系统的需要,那么通过适配器模式就可以让这些功能得到更好的复用;

2.更好的扩展性:在实现适配器功能的时候,可以调用自己开发的功能,从而自然地扩展系统的功能。

复用性:系统需要使用现有的类,而此类的接口不符合系统的需要。那么通过适配器模式就可以让这些功能得到更好的复用。

3.低耦合:无需修改原有代码(遵循开闭原则)

Adapter模式的缺点

如果在一个系统中过多的使用适配器模式,会让系统非常零乱,不易整体把握。例如,明明看到调用的是 A 接口,其实内部被适配成 B 类的实现,这样就增加了维护性,过多的使用就显得很没有必要了,不如直接对系统进行重构。

适配器模式在实际使用过程中有常用的“两种”方式:对象适配器和类适配器。

对象适配器:

 类适配器:

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/liqinimmanuel/p/11998929.html