当前位置:首页 > 设计模式 > 正文内容

工厂方法模式

canca19年前 (2007-03-26)设计模式449

//工厂方法模式:现实例子
//CopyRight(C)CAnca Software Office. 2006
//Created by CAnca.

import java.util.*;

public class Real_World_Example{
 public static void main(String[] args){
  Product[] products = new Product[2];
  products[0] = new concreteProduct1();
  products[1] = new concreteProduct2();
  for(int i = 0 ; i < products.length ; i++){
   products[i].show();
  }
 }
}

abstract class Product{
 protected ArrayList al;
 public Product(){
  al = new ArrayList();
  setFood();  
 }
 abstract void setFood();
 public void show(){
  System.out.println("食物有:");
  for(int i = 0 ; i < al.size() ; i++){
   System.out.println((i+1) + "、" + al.get(i));
  }
  System.out.println();
 }
}


class concreteProduct1 extends Product{
 public void setFood(){
  this.al.add("Apple");
  this.al.add("Banana");
  this.al.add("Pear");
 }
}

class concreteProduct2 extends Product{
 public void setFood(){
  this.al.add("苹果");
  this.al.add("香蕉");
  this.al.add("莉子");
 }
}

 

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/667.html

标签: 设计模式
分享给朋友:
返回列表

上一篇:单例模式

下一篇:建造者模式

“工厂方法模式” 的相关文章

抽象工厂模式

//抽象工厂模式//Copyright CAnca Software office//Created by CAnca.2006public class AbstractFactory{ public static void main(String[] args){  ...

单例模式

//CopyRight CAnca Software Office. 2006//Created by CAnca. import java.util.*; public class singletonMode{ public static void main(String[] args)...

原型模式

//原型模式//CopyRight(C) CAnca Software Office.2006//Created by CAnca. public class PrototypeMode{ public static void main(String[] args){  ...

代理模式

//代理模式--真实例子//CopyRight(C)CAnca Software Office.2006//Created by CAnca.   public class ProxyMode_RealWorld{     public static void...

外观模式

//外观模式//CopyRight(C)CAnca Software Office. 2006//Created by CAnca. public class FacadeMode{ public static void main(String[] args){  Fa...

备忘录模式

//备忘录模式//CopyRight(C) CAnca software Office.2006//Created by CAnca. public class MementoMode{ public static void main(String[] args){  ...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。