Creation mode of Android design mode

Source: Internet
Author: User

Factory method Mode

Definition: Defines an interface that is used to create an object, letting subclasses decide which class to instantiate.

A factory method is an instantiation of a class deferred to its subclasses

/************************* Baker *****************************///Baker class   public class breadmaker {    public void getbread () {}}//black bread public class blackbread  extends breadmaker{     @Override     public void  Getbread () {        system.out.println ("Baked Black bread");     }}//Honey Bread public class honeybread extends breadmaker{     @Override     public void getbread () {         System.out.println ("Baked honey Bread");     }}//white bread public class whitebread extends  BreadMaker{     @Override     public void getbread () {         system.out.println ("Baked white bread");     }}/******* Factory ***********************Factory interface interface ifactory {    public breadmaker  Createbread ();} Black Bread Factory public class blackbreadfactory implements ifactory{    @ Override    public breadmaker createbread ()  {         // TODO Auto-generated method stub         return new blackbread ();     }}//Honey Bread Factory public class  honeybreadfactory implements ifactory{         @Override     public breadmaker createbread ()  {         // TODO Auto-generated method stub         return new honeybread ();     }}//white bread Factory public class  Whitebreadfactory implements ifactory{         @Override     public breadmaker  createbread ()  {        // todo auto-generated  method stub        return new whitebread ();     }}/***************************  Client Implementation *******************************/public static  void main (String[] args) {        //Bakery factory      ifactory breadfactory;     //Baker     breadmaker  breadmaker;        //customer wants black bread     breadfactory  = new blackbreadfactory ();//instantiation of the Black Bread Factory     breadMaker =  Breadfactory.blackbread ();//Instantiate Black Baker     breadmaker.getbread ();//Make Black bread          //Customers want honey noodlesPackage     breadfactory = new honeybreadfactory ();//Instantiate Honey Bread Factory      breadmaker = breadfactory.honeybread ();//Instantiate Honey Baker     breadmaker.getbread () ;//Make Honey bread         //customers want white bread     breadfactory =  new whitebreadfactory ();//instantiation of white bread factory     breadMaker =  Breadfactory.whitebread ();//Instantiate White Baker     breadmaker.getbread ();//Make white bread      }

Scope of Use: 1. When a class does not know the class of the object it creates.  2. A class wants its subclasses to specify the objects it creates. 3. The class delegates the responsibility to create an object to one of several subclasses.

If the product variety is relatively small can be used other simpler methods, such as a simple factory model, if the product variety is more abundant can choose the factory method mode

Abstract Factory mode

Definition: Provides an interface that creates a series of related or interdependent objects without specifying their specific classes.

code example: If you still want to sell pizza, and the baker will make pizza, add a pizza-making interface to the factory interface above

Pizzamaker Createpizza (); Adding Pizzamaker to the Ifactory interface is the pizza class, similar to the Baker class

Scope of Use: 1. A series to be independent of its product creation, composition and presentation.

2. When a system is to be configured by one of multiple product families

3. When you want to provide a product class library, and simply display their interfaces instead of the implementation

4. To emphasize the design of a series of related product objects in order to make joint

Cons: If the product needs change, you may need to make a lot of changes where the product class has been declared. Can be combined by reflection + abstract Factory mode





Creation mode of Android design mode

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.