Learn a little design pattern every day-factory method mode

Source: Internet
Author: User
Tags abstract prepare
Factory Method Mode


English name


Factory Method Pattern


definition


Defines an interface for creating objects, but subclasses decide which class to instantiate, and the factory method pattern allows the class to defer instantiation to subclasses.


Principles


1. A variable cannot hold a reference to a specific class

2. Do not let classes derive from specific classes

3. Do not overwrite the implemented methods in the base class


Understanding


1. An abstract method is defined in an abstract interface class that is called directly by another method in the interface class.

2. Abstract methods are implemented by subclasses, and different objects are produced in the implementation of subclasses.


Code


Abstract Interface class: Factory method Definition Class


Public abstract class Pizzastore {public
	
	Pizza Orderpizza (String type) {
		Pizza Pizza;
		Pizza = Createpizza (type);
		
		Pizza.prepare ();
		Pizza.bake ();
		Pizza.cut ();
		Pizza.box ();
		return pizza;
	}
	
	Protected abstract Pizza Createpizza (String type);
		
}

Subclass: produce a specific product


public class Nystylepizzastore extends Pizzastore {

	@Override
	protected Pizza Createpizza (String type) {
		if (type.equals ("cheese")) {
			return new Nystylecheesepizza ();
		} else if (Type.equals ("veggie")) {
			return new Nystylecheesepizza ();
		} else if (type.equals ("clam")) {
			return new Nystyleclampizza ();
		} else if (Type.equals ("pepperoni")) {
			return new Nystylepepperonipizza ();
		} else
			return null;
	}

}

public class Chicagostylepizzastore extends Pizzastore {

	@Override
	protected Pizza Createpizza (String type) { C2/>if (type.equals ("cheese")) {
			return new Chicagostylecheesepizza ();
		} else if (Type.equals ("veggie")) {
			return new Chicagostyleveggiepizza ();
		} else if (type.equals ("clam")) {
			return new Chicagostyleclampizza ();
		} else if (Type.equals ("pepperoni")) {
			return new Chicagostylepepperonipizza ();
		} else
			return null;
	}

}

Commodity abstract class


Public abstract class Pizza {
	String name;
	String Dough;
	String sauce;
	ArrayList toppings = new ArrayList ();
	public void Prepare () {
		System.out.println ("Preparing" + name);
		System.out.println ("tossing dough");
		System.out.println ("Adding toppings");
		for (int i=0;i<toppings.size (); i++) {
			System.out.println ("  " + toppings.get (i));
		}
	}
	public void Bake () {
		System.out.println ("Bake to minutes at");
	}
	
	public void Cut () {
		System.out.println ("cutting the pizza into diagonal slices");
	}
	public void Box () {
		System.out.println ("place pizza in official Pizzastore box");
	
	Public String GetName () {
		return name;
	}
}

Product specific Category


public class Chicagostylecheesepizza extends Pizza {public
	
	Chicagostylecheesepizza () {
		name = "Chicago Style" Deep Dish Cheese Pizza ";
		Dough = "Extra Thick crust dough";
		sauce = "Plum Tomato sauce";
		
		Toppings.add ("shredded mozzarella Cheese");
	}
	
	public void Cut () {
		System.out.println ("cutting the pizza into square slices");
		
		
	}

}

public class Chicagostyleclampizza extends Pizza {

}

public class Chicagostylepepperonipizza extends Pizza {

}

public class Chicagostyleveggiepizza extends Pizza {

}

public class Nystylecheesepizza extends Pizza {public
	
	Nystylecheesepizza () {
		name = "NY Style sauce and Cheese Piz Za ";
		Dough = "Thin crust dough";
		sauce = "marinara sauce";
		
		Toppings.add ("grated Reggiano Cheese");
	}

}

public class Nystyleclampizza extends Pizza {

}

public class Nystylepepperonipizza extends Pizza {

}

public class Nystyleveggiepizza extends Pizza {

}

Test program


public class Pizzatestdrive {public

	static void Main (string[] args) {
		Pizzastore nystore = new Nystylepizzastore ( );

		Pizzastore Chicagostore = new Chicagostylepizzastore ();

		Pizza Pizza = Nystore.orderpizza ("cheese");
		System.out.println ("Ethan ordered a" + pizza.getname () + "\ n");
		Pizza = Chicagostore.orderpizza ("cheese");
		System.out.println ("Joel ordered a" + pizza.getname () + "\ n");}

}

Output results


Preparing NY Style sauce and Cheese Pizza
tossing dough
Adding toppings
grated reggiano Cheese
Bake for Minutes at cutting the
pizza to diagonal slices place
pizza in official Pizzastore box
Ethan ordered A NY Style sauce and Cheese Pizza

Preparing Chicago Style deep Dish Cheese Pizza
tossing dough Adding toppings
shredded mozzarella Cheese
Bake for minutes at cutting the
pizza to square slices place
pizza in official Pizzastore box
Joel Ordered a Chicago Style deep Dish Cheese Pizza



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.