Simple Factory Factory

Source: Internet
Author: User
Package net.util;/** * @ Project Name: spring2.5 * @ Pack Name: Net.util * @ file name: Factorydemo.java * @ Date: June, 4:37:02 PM * @ Note: Factory mode * @ By: Apple */public class Factorydemo {/*** @param args*/public static void Main (string[] args) {//TODO auto-generated Metho D stubcar c=factory.getcarinstance ("Banz"); if (c!=null) {c.run (); C.stop ();} ELSE{SYSTEM.OUT.PRINTLN ("cannot be created");}}} Class Factory{public static car Getcarinstance (String type) {car c=null;try {c= (CAR) class.forname ("Net.util.") +type). newinstance ();} catch (Instantiationexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception  e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (ClassNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} return c;}} Interface car{public void Run ();p ublic void Stop (); Class Banz implements Car{public void Run () {//TODO auto-generated method StubSystem.out.println ("Banz Run");} public void Stop () {//TODO auto-generated method StubSystem.out.println ("BaNZ stop ");}} Class Ford implements Car{public void Run () {//TODO auto-generated method StubSystem.out.println ("Ford Run");} public void Stop () {//TODO auto-generated method StubSystem.out.println ("Ford Stop");}}
  • Related Article

    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.