java设计模式学习笔记-2-工厂模式
程序员文章站
2024-01-23 19:54:46
...
简单工厂模式
public abstract class Pizza{
protected String name;
public abstract void prepare();
public void bake(){
}
public void cut(){
}
public void box(){
}
public void setName(String name){
}
}
public class ChesePizza extends Pizza{
public void prepare(){
}
}
public class GreekPizza extends Pizza{
pulic void prepare(){
}
}
public class PepperPizza extends Pizza{
public void peroare(){
}
}
public class OrderPizza{
public OrderPizza(){
Pizza pizza = null;
String orderType;
do{
orderType = getType;
if(orderType.equals("greek")){
pizza = new GreekPizza();
pizza.setName("xxxx");
}else if(orderType.equals("cheese")){
pizza = new CheesePizza();
pizza.setName("xxxx");
}else if(orderType.equals("pepper")){
pizza = new PepperPizza();
pizza.setName("xxxx");
}else{
break;
}
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.cut();
}while(true);
}
private String getType(){
try{
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
String str = strin.readLine();
return str;
}catch(IOException e){
e.printStackTreace();
return "";
}
}
}
public PizzaStore{
public static void main(String[] args){
new OrderPizza();
}
}
//简单工厂类
public class SimpleFactory{
//根据orderType返回对应Pizza
public Pizza createPizza(String orderType){
Pizza pizza = null;
if(orderType.equals("greek")){
pizza = new GreekPizza();
pizza.setName("xxxx");
}else if(orderType.equals("cheese")){
pizza = new CheesePizza();
pizza.setName("xxxx");
}else if(orderType.equals("pepper")){
pizza = new PepperPizza();
pizza.setName("xxxx");
}
return pizza;
}
}
public class OrderPizza{
SimpleFactory simpleFactory;
Pizza pizza = null;
public OrderPizza(SimpleFactory simpleFactory){
setFactory(simpleFactory);
}
public void setFactory(SimpleFactory simpleFactory){
String orderType = "";
this.simpleFactory = simpleFactory;
do{
orderType = getType();
pizza = this.simpleFactory.createPizza(orderType);
if(pizza!=null){
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else{
System.out.println("失败");
}
}while(true);
}
private String getType(){
try{
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
String str = strin.readLine();
return str;
}catch(IOException e){
e.printStackTreace();
return "";
}
}
}
//简单工厂类
public class SimpleFactory{
//根据orderType返回对应Pizza
public Pizza createPizza(String orderType){
Pizza pizza = null;
if(orderType.equals("greek")){
pizza = new GreekPizza();
pizza.setName("xxxx");
}else if(orderType.equals("cheese")){
pizza = new CheesePizza();
pizza.setName("xxxx");
}else if(orderType.equals("pepper")){
pizza = new PepperPizza();
pizza.setName("xxxx");
}
return pizza;
}
public static Pizza createPizza2(String orderType){
Pizza pizza = null;
if(orderType.equals("greek")){
pizza = new GreekPizza();
pizza.setName("xxxx");
}else if(orderType.equals("cheese")){
pizza = new CheesePizza();
pizza.setName("xxxx");
}else if(orderType.equals("pepper")){
pizza = new PepperPizza();
pizza.setName("xxxx");
}
return pizza;
}
}
public class OrderPizza2{
Pizza pizza = null;
String orderType = "";
public OrderPizza2(){
do{
orderType = getType();
pizza = SimpleFactory.createPizza2(orderType);
if(pizza!=null){
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else{
System.out.println("失败");
}
}while(true);
}
private String getType(){
try{
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
String str = strin.readLine();
return str;
}catch(IOException e){
e.printStackTreace();
return "";
}
}
}
工厂方法模式
public abstract class Pizza{
protected String name;
public abstract void prepare();
public void bake(){}
public void cut(){}
public void box(){}
public void setName(String name){
}
}
public class BJCheesePizza extends Pizza{
//北京奶酪
public void prepare(){
}
}
public class BJPepperPizza extends Pizza{
//北京胡椒
public void prepare(){
}
}
public class LDCheesePizza extends Pizza{
//伦敦奶酪
public void prepare(){
}
}
public class LDPepperPizza extends Pizza{
//伦敦胡椒
public void prepare(){
}
}
public abstract class OrderPizza{
abstract Pizza createPizza(String orderType);
public OrderPizza(){
Pizza pizza = null;
String orderType;
do{
orderType = getType();
pizza = createPizza(orderType);
if(pizza!=null){
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else{
System.out.println("失败");
}
}while(true);
}
private String getType(){
try{
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
String str = strin.readLine();
return str;
}catch(IOException e){
e.printStackTreace();
return "";
}
}
}
public classBJOrderPizza extends OrderPizza{
Pizza createPizza(String orderType){
Pizza pizza = null;
if(orderType.equals("cheese")){
pizza = new BJCheesePizza();
}esle if(orderType.equals("pepper")){
pizza = new BJPepperPizze();
}
return pizza;
}
}
public classLDOrderPizza extends OrderPizza{
Pizza createPizza(String orderType){
Pizza pizza = null;
if(orderType.equals("cheese")){
pizza = new LDCheesePizza();
}esle if(orderType.equals("pepper")){
pizza = new LDPepperPizze();
}
return pizza;
}
}
抽象工厂模式
public interface AbsFactory{
public Pizza createPizza(String orderType);
}
public class BJFactory implements AbsFactory{
public Pizza cratePizza(String orderType){
Pizza pizza = null;
if(orderType.equals("cheese")){
pizza = new BJCheesePizza();
}else if(orderType.equals("pepper")){
pizza = new BJPepperPizza();
}
return pizza;
}
}
public class LDFactory implements AbsFactory{
public Pizza cratePizza(String orderType){
Pizza pizza = null;
if(orderType.equals("cheese")){
pizza = new LDCheesePizza();
}else if(orderType.equals("pepper")){
pizza = new LDPepperPizza();
}
return pizza;
}
}
public class OrderPizza{
AbsFactory factory;
public OrderPizza(AbsFactory factory){
setFactory(factory);
}
private void setFactory(AbsFactory factory){
Pizza pizza = null;
String orderType = "";
this.factory = factory;
do{
orderType = getType();
pizza = factory.createPizza(orderType);
if(pizza!=null){
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
}else{
System.out.println("失败");
}
}ehile(true);
}
private String getType(){
try{
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
String str = strin.readLine();
return str;
}catch(IOException e){
e.printStackTreace();
return "";
}
}
}
上一篇: python 输出所有大小写字母的方法
下一篇: python调用百度语音REST API