惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - AlanPaoPao

23)Visitor 22)Template 21)Strategy 20)State 19)Observer 18)Memento 17)Mediator 16)Iterator 15)Interpreter 14)Command 13)Chain Of Responsibility 12)Proxy 11)Flyweight 10)Facade 09)Decorator 08)Composite 07)Bridge 06)Adapter 05)Prototype
04)Factory Method
AlanPaoPao · 2007-09-15 · via 博客园 - AlanPaoPao

    工厂方法模式的目的是: 使一个类的实例化延迟到其子类。
    实例代码:

abstract class Food  //定义抽象食物类
{
}

class Apple : Food //苹果类
{
  
public override string ToString()
  
{
    
return "This is apple";
  }

}

class Banana : Food //香蕉类
{
  
public override string ToString()
  
{
    
return "This is banana";
  }

}

class Peach : Food //桃子类
{
  
public override string ToString()
  
{
    
return "This is peach";
  }

}

class Coke : Food //可乐类
{
  
public override string ToString()
  
{
    
return "This is coke";
  }

}

class Coffee : Food //咖啡类
{
  
public override string ToString()
  
{
    
return "This is coffee";
  }

}

class Hamburger : Food //汉堡包类
{
  
public override string ToString()
  
{
    
return "This is hamburger";
  }

}

class Noodle : Food //面条类
{
  
public override string ToString()
  
{
    
return "This is noodle";
  }

}

class Meal : Food //米饭类
{
  
public override string ToString()
  
{
    
return "This is meal";
  }

}

abstract class Eat  //食用类
{
  
private List<Food> _foods = new List<Food>();  //食物清单
  public Eat()
  
{
    
this.CreateFoods();
  }

  
public List<Food> Foods
  
{
    
get return this._foods; }
  }

  
public abstract void CreateFoods();
}

class Fruit : Eat  //水果类
{
  
public override void CreateFoods()
  
{
    Foods.Add(
new Apple());
    Foods.Add(
new Banana());
    Foods.Add(
new Peach());
  }

  
public override string ToString()
  
{
    
return "水果";
  }

}

class Drink : Eat  //饮料类
{
  
public override void CreateFoods()
  
{
    Foods.Add(
new Coke());
    Foods.Add(
new Coffee());
  }

  
public override string ToString()
  
{
    
return "饮料";
  }

}

class StapleFood : Eat  //主食类
{
  
public override void CreateFoods()
  
{
    Foods.Add(
new Hamburger());
    Foods.Add(
new Noodle());
    Foods.Add(
new Meal());
  }

  
public override string ToString()
  
{
    
return "主食";
  }

}

class MainApp //测试
{
  
static void Main()
  
{
    Eat[] eats 
= new Eat[3];
    eats[
0= new Fruit();
    eats[
1= new Drink();
    eats[
2= new StapleFood();
    
foreach (Eat eat in eats)
    
{
      Console.WriteLine(
"小胖开始吃: " + eat.ToString() + "");
      
foreach (Food food in eat.Foods)
      
{
        Console.WriteLine(
" " + food.ToString());
      }

    }

    Console.WriteLine(
"小胖吃饱咯:)");
    Console.Read();
  }

}