Android Common Design Patterns

Source: Internet
Author: User
Tags pear pears

Java has 23 of design patterns, Android also used a lot of design patterns, this article on the android in several common design patterns

First, the Common factory design mode

The Common factory design pattern is to create a factory class that is responsible for creating objects that users need to get the products they want from the factory class. Here are the three related

The words are: factories, products and users; Here to illustrate is that the product is a common feature, the common features extracted to create an interface, all products to achieve this interface

Then the factory class is responsible for creating these products, the user needs what products to be obtained in the factory class;

The code is implemented as follows:

Product common Feature Interface

/** * Product common Feature interface *   @author JIAOCG *   */  Public Interface myproduct {    //  What common features can be defined here is the public    void  Createproduct (String product);}

Apple class

1  Public class Implements myproduct {23    @Override4public     void createproduct (String product) {5         System.out.println ("I am an apple"); 6     }78 }

Pear type

1  Public class Implements myproduct {23    @Override4public     void createproduct (String product) {5         System.out.println ("I am a pear"); 6     }7 }

Factory class

1 /**2 * Factory class3  * 4  * @authorJIAOCG5  * 6  */7  Public classProductfactory {8 9      Publicmyproduct getproduct (String puduct) {Ten         if("Apple". Equals (puduct)) { One             return NewApple ();//Apple will return to Apple A         } -         return NewPear ();//pear is returned to pear -     } the}

Using Factory classes

1 New productfactory (); 2 // I want an apple . 3 myproduct apple = productfactory.getproduct ("apple"); 4 // I want pears . 5 myproduct pear = productfactory.getproduct ("pear");

Second, abstract factory design mode

Abstract Factory design mode is the expansion of the general factory design pattern, the disadvantage of ordinary factory design mode is that if there are new products, we will modify the factory class, to add new products,

This undermines the principle of program closure, while the abstract factory design pattern solves this problem. is to separate the factory, as mentioned in the previous example, the factory class can create apples and create pears

And the abstract factory is a factory class is only responsible for creating a product, so the production of a separate factory, such as Apple factory only produce apples, pear factory only produce pears, you add new products, I will create a new

Factory class; Because the factory is independent, and has the common characteristics, so to extract an interface, all factories to implement this interface; This is the abstract factory design pattern;

The code examples are as follows:

Apple and Pear objects or objects above

We first extract the factory class interface

  1  /**    * Factory class interface    *   4   *   @author   JIAOCG   5   *   6  */  7  public  interface   MyFactory {  8   public   MyProduct createproduct ();  10 } 

Apple Factory

1 /**2 * Apple Factory3  * 4  * @authorJIAOCG5  * 6  */7  Public classApplefactoryImplementsMyFactory {8 9 @OverrideTen      Publicmyproduct createproduct () { One         return NewApple (); A}

Pear Factory

1 /**2 * Pear Factory3  * 4  * @authorJIAOCG5  * 6  */7  Public classPearfactoryImplementsMyFactory {8 9 @OverrideTen      Publicmyproduct createproduct () { One         return NewPear (); A}

Creating objects using the factory

1 applefactory applefactory=new  applefactory (); 2 // I want an apple . 3 myproduct apple = applefactory.createproduct (); 4         5 pearfactory pearfactory=new  pearfactory (); 6 // I want pears . 7 myproduct pear = pearfactory.createproduct ();

The above is a simple abstract factory design pattern, if there is a new product, we just need to create a new factory class can;

Android Common Design Patterns

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.