Design Mode Study Notes

Source: Internet
Author: User

Design Mode Study Notes
HeadFirst Design Mode

I. Adapter mode | Adapter Pattern

ListView is used to display list data. However, as a list dataset, there are many forms, including Array and Cursor. We need the corresponding adapter as a bridge, process the corresponding data (and form the view required by ListView ).
Only by defining these adapter interfaces and adapter classes can we make our data simple, flexible, and correctly displayed to the adapterview implementation class.

In the design pattern system, there are three types of structural pattern:

Class adapter mode Object Adapter mode interface adapter Mode

Because the adapter mode of classes and interfaces requires the adapter to inherit the original classes, the only object-oriented languages Java and C # do not support multi-inheritance, this restricts the use of the adapter mode of the class and interface to a certain extent, so pay attention to it when using it.

Purpose: To convert an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to interface incompatibility to work together.

Class adapter Mode

Note:
When a new interface appears, because it is not compatible with the old interface, we use an adapter class to implement the new interface and inherit the old business class, in this way, new and old businesses can be processed simultaneously.
Sample Code:

/*** Original class */class Source {public void oldFunction () {System. out. println (oldFunction: Source) ;}} interface Target {void oldFunction (); // new interface void newFunction () ;}// adapter, used for Interface Conversion class Adapter extends Source implements Target {public Adapter () {super () ;}@ Override public void newFunction () {System. out. println (newFunction: Adapter) ;}} public class A {public static void main (String args []) {Target target = new Adapter (); target. oldFunction (); target. newFunction ();}}

Output:

oldFunction:SourcenewFunction:Adapter
Object Adapter Mode

Note: When a new interface appears, because it is not compatible with the old interface, we use an adapter class to implement the new interface and hold an old business logic object internally, in this way, all the calls to the old interface can be transferred to the old business logic to solve the interface compatibility problem.
Sample Code:

/*** Original class */class Source {public void oldFunction () {System. out. println (oldFunction: Source) ;}} interface Target {void oldFunction (); // new interface void newFunction () ;}// adapter, used for Interface Conversion class Adapter implements Target {// original class Object held inside the Adapter private Source mSource; public Adapter (Source source) {mSource = source ;} @ Override public void oldFunction () {// call the mSource method in the original class. oldFunction () ;}@ Override public void newFunction () {System. out. println (newFunction: Adapter) ;}} public class A {public static void main (String args []) {Source source = new Source (); target target = new Adapter (source); target. oldFunction (); target. newFunction ();}}

Output:

oldFunction:SourcenewFunction:Adapter
Interface adapter Mode

Note: This is also called the default adapter mode, which mainly solves the interface reuse problem: Sometimes our business only needs to use a method in the interface instead of all methods, however, due to the language characteristics of the interface, you have to implement all the abstract methods. This will make the interface very difficult to use, especially when there are many abstract methods in the interface. In the face of this type of interface problem, we can use an abstract class (or not abstract class) to implement the interface, and then let our class inherit this abstract class and only rewrite the method we are interested in.
Sample Code:

Interface Target {void function1 (); void function2 (); void function3 (); void function4 () ;}// TargetWrapper implements the Target interface, the default Implementation of abstract class TargetWrapper implements Target {@ Override public void function1 () {System. out. println (function1: TargetWrapper) ;}@ Override public void function2 () {}@ Override public void function3 () {}@ Override public void function4 () {}} // adapter, inherit TargetWrapper and Override the method class Adapter extends TargetWrapper {public Adapter () {super () ;}@ Override public void function2 () {System. out. println (function4: Adapter) ;}} public class A {public static void main (String args []) {Target target = new Adapter (); target. function1 (); target. function2 ();}}

Output:

function1:TargetWrapperfunction4:Adapter
Ii. Factory mode | Factory Pattern

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.