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

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
V
V2EX
S
Security Affairs
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
J
Java Code Geeks
The Register - Security
The Register - Security
U
Unit 42
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Project Zero
Project Zero
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
博客园 - 司徒正美
V
Vulnerabilities – Threatpost
T
Tor Project blog
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Palo Alto Networks Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta

博客园 - 冷风.net

以OO的思想利用JS來實現五子棋 開放幾個原來寫的js代碼,供大家娛樂一下 使用JS寫的第一個游戲[俄羅斯方塊] 假如現在讓你去說服客戶使用asp.net2.0開發系統,你會怎麼說服呢? 對象化javascript日期控件 - 冷风.net - 博客园 工作排程 防止圖片在WEB頁面上下載 - 冷风.net - 博客园 今天從新整理的大小寫數據轉換 - 冷风.net - 博客园 常用的XPATH說明 XmlHttp在DoNet中的完全应用---前/后台完成分离篇 系统用户权限与角色分析 学习设计模式之Composite 模式 改進Richer的WEB頁面進度條! 学习设计模式之Bridge模式 再谈Abstract Factory模式来实现数据库操作的类 Factory Method来实现数据库操作的类 不周表結構合併問題 今天下午困惑的問題,終於TMD搞出來了 利用線程實現錠時執行的運行模型
学习设计模式之生成器(Builder Pattern)模式
冷风.net · 2005-03-01 · via 博客园 - 冷风.net

对于生成器模式我这里以生产自行车为例进行讲解,自行车由车轮与车架组成,由不同样式的车轮与不同样式的车架就可以组装成不同的自行车(如赛车,山地车等),下面的程式进行说明:

/// <summary>定義自行車架構</summary>
    public struct Bike
    
{
        
private string bikeWheel;
        
private string bikeFrame;
        
public Bike(string wheel,string frame)
        
{
            
this.bikeWheel = wheel;
            
this.bikeFrame = frame;
        }

        
public string GetBike
        
{
            
get
            
{
                
return bikeWheel + bikeFrame;
            }

        }

    }

    
/// <summary>定義接口</summary>
    public interface MainInterface    
    
{
        
void CreateWheel(string bikeType);
        
void CreateFrame(string bikeType);

        Bike GetBike();
    }

    
/// <summary>創建自行車的各個部件</summary>
    public class ConcreteClass : MainInterface
    
{
        
string wheel;
        
string frame;
        
/// <summary>創建自行車的車輪</summary>
        public void CreateWheel(string bikeType)
        
{
            
switch(bikeType)
            
{
                
case "山地車":
                    wheel
="山地車的車輪為@@@";
                    
break;
                
case "賽車":
                    wheel
="賽車的車輪為***";
                    
break;
            }

        }

        
/// <summary>創建自行車的車架</summary>
        public void CreateFrame(string bikeType)
        
{
            
switch(bikeType)
            
{
                
case "山地車":
                    frame
="山地車的車架為^^^";
                    
break;
                
case "賽車":
                    frame
="賽車的車架為+++";
                    
break;
            }

        }

        
/// <summary>這裡用於獲得自行車</summary>
        public Bike GetBike()
        
{
            
return new Bike(wheel,frame);
        }

    }


    
/// <summary>用於組裝成不同樣式的自行車</summary>
    public class CreateBike
    
{
        
private MainInterface objMain;
        
private string bikeType;
        
public CreateBike(MainInterface objMain,string bikeType)
        
{
            
this.objMain = objMain;
            
this.bikeType = bikeType;
        }

        
public void Create()
        
{
            objMain.CreateWheel(bikeType);
            objMain.CreateFrame(bikeType);
        }

    }

下面來看看調用:

string bikeType="山地車";
            ConcreteClass objCon 
= new ConcreteClass();
            CreateBike objCB 
= new CreateBike(objCon,bikeType);
            objCB.Create();
            Bike objB 
= objCon.GetBike();
            Response.Write(objB.GetBike);

下面来比较抽象工厂模式与生成器模式的区别:
以生产自行车为例,抽象工厂模式获得的是不同名牌的自行车而不管自行车是由什么部件组装而成的;而生成器模式获得是不同样式的自行车,所关心的是自行车组成部件的构成.所以看出抽象工厂模式获得是一系列相关的类(如凤凰牌自行车,XX牌自车等)而生成器模式获得的是提供给它数据(如上例中的“山地车“)一步一步构建的一个复杂的对象(即山地车)