Factory method pattern of Java Design Pattern

Source: Internet
Author: User

1. Factory method mode (each specific role is generated by its specific factory), ①, abstract role ②, specific role ③, abstract factory ④, factory abstraction factory mode (the difference between factory mode and factory mode lies in the complexity of object creation) ①. Abstract role: package method. factory; public interface implements actmove {public void run () ;}②, specific role package method. factory; public class Bus implements actmove {@ Overridepublic void run () {// TODO Auto-generated method stubSystem. out. println ("This is a Bus running") ;}} package method. factory; public class Plan implements actmove {@ Overridepublic void run () {// TODO Auto-generated method stubSystem. out. println ("This is Plan running") ;}③, abstract factory package method. factory; public abstract class implements actfactory {abstract AbstractMove create ();} ④, the specific factory package method. factory; public class BusFactory extends actfactory {@ overridemo-actmove create () {// TODO Auto-generated method stubreturn new Bus () ;}} package method. factory; public class PlanFactory extends actfactory {@ overridemo-actmove create () {// TODO Auto-generated method stubreturn new Plan () ;}} Test: package method. factory; public class Test {public static void main (String [] args) {// TODO Auto-generated method stub1_actfactory af = new PlanFactory (); AbstractMove move = af. create (); move. run (); AbstractFactory af1 = new BusFactory (); AbstractMove move1 = af1.create (); move1.run () ;}} result: This is Plan runningThis is a Bus running

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.