C # Design Pattern builder (Builder) pattern sample source code

Source: Internet
Author: User
Tags abstract
Design | example | source code
Using System;
Using System.Collections;


Product
public class Computer
{
private string Type;
Private Hashtable info=new Hashtable ();
Public Computer (String type)
{
Type=type;
Console. WriteLine ("Conputer" +type+ "is builded!");
}

public string this[string key]
{
Get
{
Return (string) Info[key];
}
Set
{
Info[key]=value;
}
}
public void Show ()
{
Console. WriteLine ("This is a computer of" +type+ ":");
Console. WriteLine ("" +info["CPU"));
Console. WriteLine ("" "+info[" Ram "]);
}
}

Builder
Public abstract class Builder
{
protected Computer Computer;

Public Computer Computpter
{
Get
{
return computer;
}
}
Public Builder (String type)
{
Computer=new computer (type);
}
Abstract public void buildcpu ();
Abstract public void Builderram ();

}

public class Macbuilder:builder
{
Public Macbuilder (String type): Base (Type)
{}
public override void Buildcpu ()
{
computer["CPU"]= "MACCPU";
}
public override void Builderram ()
{
computer["Ram"]= "MACCPU";
}


}


public class Pcbuilder:builder
{
Public Pcbuilder (String type): Base (Type)
{}
public override void Buildcpu ()
{
computer["CPU"]= "PCCPU";
}
public override void Builderram ()
{
computer["Ram"]= "PCCPU";
}

}

public class Worker
{
public void construct (Builder Builder)
{

Builder. Buildcpu ();
Builder. Builderram ();
}
}


public class CEO
{

public static void Main ()
{
Try
{
Worker worker=new Worker ();
Builder pcbuilder=new pcbuilder ("Pc");
Builder macbuilder=new macbuilder ("Mac");

Worker. Construct (Pcbuilder);
Worker.construct (Macbuilder);

PcBuilder.Computpter.Show ();
MacBuilder.Computpter.Show ();
}
catch (Exception ex)
{
Console.WriteLine ("Error:" +ex.) Source+ "" +ex. Innerexception+ex. Stacktrace+ex. TargetSite);
}


}
}







Related Article

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.