Learn a little design pattern every day-abstract Factory mode

Source: Internet
Author: User
Tags abstract prepare stub
Abstract Factory mode


English name


Abstract Factory Pattern


definition


Provides an interface for creating related or object families without specifying a specific class


Principles


To rely on abstraction, do not rely on specific classes
Understanding


1. Direct communication with each other is the factory abstract class, you do not need to know his specific implementation of the class is which. This is done for the abstract factory class to program.

2. The method of producing all products is defined in the abstract factory class. Only each product varies by geographical difference, but it belongs to a category of products. This product is also to be abstracted into an interface.

3. The different products we want at this time are determined by the factory subclass. This subclass is only known in the dynamic running process. The coding phase is completely free of consideration. We just need to know, I want the product, the factory will give me to produce it is finished.


Code


Abstract Factory Interface


Public interface Pizzaingredientfactory {public
	dough createdough ();

	Public Sauce createsauce ();

	Public Cheese Createcheese ();

	Public veggies[] Createveggies ();

	Public pepperoni Createpepperoni ();

	Public clams Createclam ();
}

Implementation class


public class Nypizzaingredientfactory implements Pizzaingredientfactory {     @Override    & Nbsp;public Dough Createdough () {        //TODO auto-generated method stubs    &N
bsp;    return new Thincrustdough ();     }      @Override     public Sauce createsauce () {    &NB
sp;   //TODO auto-generated method stub         return new Marinarasauce ();     }      @Override     public Cheese Createcheese () {    &
nbsp;   //TODO auto-generated method stub         return new Reggianocheese ();     }      @Override     public veggies[] createveggies () {   &NB sp;    //TODO auto-generated method stub         veggies VeggiEs[] = {new Garlic (), New Onion (), New Mushroom (), New Redpepper ()};
        return veggies;    &nbsp,}      @Override     public Pepperoni Createpepperoni () {   & nbsp;    //TODO auto-generated method stub         return New
Slicedpepperoni ();     }      @Override     public clams Createclam () {    &nbs
p;   //TODO auto-generated method stub         return new Freshclams ();


     }}

The entity class interface used in it and the specific implementation


/**
 * Description: Raw, cheese */public
interface Cheese {

}

public class Reggianocheese implements Cheese {

}

/**
raw Material, clam */public
interface Clams {

}

public class Freshclams implements clams {

}

/**

 * Description: Raw material, dough */public
interface Dough {

}

public class Thincrustdough implements dough {
	
}

/**

 * Description: raw materials, vegetables */public
interface Veggies {

}

public class Garlic implements veggies {

}
public class Mushroom implements veggies {

}

public class Onion implements veggies {

}

public class Redpepper implements veggies {

}


/**

 * Description: raw material, sauce */public
interface Sauce {

}

public class Marinarasauce implements Sauce {

}

/**

 * Description: Raw, spicy sausage
 */Public
interface Pepperoni {

}

public class Slicedpepperoni implements pepperoni {

}

Transform the Pizza class of the previous article,


Public abstract class Pizza {
	String name;
	Dough dough;
	Sauce Sauce;
	Veggies veggies[];
	Cheese Cheese;
	Pepperoni Pipperoni;
	Clams clam;

	public abstract void Prepare ();

	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;
	}

	public void SetName (String name) {
		this.name = name;
	}
}

Now do not need to have Nystylepizza, directly to the pizza raw materials to produce different pizza on the line

Cheese Pizza

public class Cheesepizza extends Pizza {
	pizzaingredientfactory ingredientfactory;

	Public Cheesepizza (Pizzaingredientfactory ingredientfactory) {
		this.ingredientfactory = ingredientfactory;
	}

	@Override public
	Void Prepare () {
		System.out.println ("Preparing" + name);
		Dough = Ingredientfactory.createdough ();
		sauce = Ingredientfactory.createsauce ();
		Cheese = Ingredientfactory.createcheese ();
	}

}

Clam Pizza


public class Clampizza extends Pizza {
	pizzaingredientfactory ingregientfactory;
	
	Public Clampizza (Pizzaingredientfactory ingregientfactory) {
		this.ingregientfactory = ingregientfactory;
	}
	
	@Override public
	Void Prepare () {
		System.out.println ("Preparing" + name);
		Dough = Ingregientfactory.createdough ();
		sauce = Ingregientfactory.createsauce ();
		Cheese = Ingregientfactory.createcheese ();
		Clam = Ingregientfactory.createclam ();
	}

}

Pizza Store


public class Nystylepizzastore extends Pizzastore {

	@Override
	protected Pizza Createpizza (String item) {
		Pizza Pizza = null;
		Pizzaingredientfactory ingredientfactory = new Nypizzaingredientfactory ();
		if (item.equals ("cheese")) {
			pizza = new Cheesepizza (ingredientfactory);
			Pizza.setname ("New York Style Cheese Pizza");
		else if (item.equals ("clam")) {
			pizza = new Clampizza (ingredientfactory);
		}
		return pizza;

	}

}

Test program


public class Pizzatestdrive {public

	static void Main (string[] args) {
		Pizzastore nystore = new Nystylepizzastor E ();
		Pizza Pizza = Nystore.orderpizza ("cheese");
		System.out.println ("Ethan ordered a" + pizza.getname () + "\ n");}

}

Output


Preparing New York Style Cheese Pizza Bake for minutes at + cutting the
Pizza into diagonal slices place
Pizza in official Pizzastore box
Ethan ordered a New York Style 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.