2 Simple Factory mode, Factory mode, abstract Factory mode

Source: Internet
Author: User

Simple Factory mode:




-----------------------------------Pizza.java--------------------


Package com;


Public abstract class Pizza {


public abstract void prepare ();
public abstract void bake ();
public abstract void cut ();
}



-----------------------------------Apizza.java--------------------

Package com;


public class Apizza extends Pizza {


@Override
public void Prepare () {


System.out.println ("A prepare");
}


@Override
public void bake () {
System.out.println ("A bake");


}


@Override
public void Cut () {


System.out.println ("A cut");
}


}

-----------------------------------Bpizza.java--------------------


Package com;


public class Bpizza extends Pizza {


@Override
public void Prepare () {


System.out.println ("B prepare");
}


@Override
public void bake () {
System.out.println ("B bake");


}


@Override
public void Cut () {


System.out.println ("B cut");
}


}

-----------------------------------Cpizza.java--------------------


Package com;


public class Cpizza extends Pizza {


@Override
public void Prepare () {


System.out.println ("C prepare");
}


@Override
public void bake () {
System.out.println ("C bake");


}


@Override
public void Cut () {


System.out.println ("C cut");
}


}

-----------------------------------Simplepizzafactory.java--------------------

Package com;


public class Simplepizzafactory {
Public Pizza Createpizza (String type) {
if (Type.equals ("A")) {
return new Apizza ();
} else if (Type.equals ("B")) {
return new Bpizza ();
} else {
return new Cpizza ();
}
}


}

-----------------------------------Pizzastore.java--------------------


Package com;


public class Pizzastore {


Private Simplepizzafactory simplepizzafactory;

Public Pizzastore (Simplepizzafactory simplepizzafactory) {
This.simplepizzafactory = simplepizzafactory;
}


Public Pizza Orderpizza (String type) {
Pizza Pizza;
Pizza = Simplepizzafactory.createpizza (type);
Pizza.prepare ();
Pizza.bake ();
Pizza.cut ();
return pizza;
}

// public static void Main (String[]args) {
// Pizzastore Pizzastore = new Pizzastore (new Simplepizzafactory ());
// Pizzastore.orderpizza ("A");
// }
}




Analysis:

Factory mode

Creater (creator): Pizzastore

Producter (product): Pizza



Abstract Factory mode:

Programming for Interfaces

Interface



Summarize:

The methods of-----abstract factories are often implemented in the form of factory methods.

The task of-----Abstract Factory is to define an interface that is responsible for creating a set of products. Each method within this interface is responsible for creating a specific product.

-----We provide these specific practices by implementing subclasses of the abstract factory.


Abstract Factory VS Factory method

----Abstract factory for Interface programming

----Factory method for specific implementation class programming


2 Simple Factory mode, Factory mode, abstract 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.