Java design pattern-Simple Factory mode

Source: Internet
Author: User

Simple Factory mode, also known as static method Factory mode. The structure diagram is as follows:

Realize:

Fruit Interface:

 Package com.design.simplefactory;  Public Interface Fruit {    String fruitname ();}

Implementation class:

 package   com.design.simplefactory;  public  class  Apple implements   Fruit {@Override  public   String Fruitname () { return " Big Apple "; }}
 Package com.design.simplefactory;  Public class Implements Fruit {    @Override    public  String fruitname () {        return "Big Banana" ;    }}

Factory:

 Packagecom.design.simplefactory; Public classFruitfactory { Public StaticFruit Createfriut (String name) {Switch(name) { Case"Apple":                return NewApple ();  Case"Banana":                return NewBanana (); default:                return NULL; }    }     Public Static voidMain (string[] args) {Fruit Apple= Fruitfactory.createfriut ("Apple"); Fruit Banana= Fruitfactory.createfriut ("Banana"); Fruit Other= Fruitfactory.createfriut ("Other"); System.out.println (Apple==NULL?" Null: Apple.fruitname ()); System.out.println (Banana==NULL?" Null: Banana.fruitname ()); System.out.println ( Other==NULL?" Null: Other.fruitname ()); }}

Output:

Java design pattern-Simple Factory 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.