Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Builder pattern examples to ensure compatibility with earlie… #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ void Start()
builder = new MotorCycleBuilder();
shop.Construct(builder);
builder.Vehicle.Show();

}
}

/// <summary>
/// The 'Director' class
/// </summary>
class Shop
{
// Builder uses a complex series of steps
Expand All @@ -52,29 +48,21 @@ public void Construct(VehicleBuilder vehicleBuilder)
}
}

/// <summary>
/// The 'Builder' abstract class
/// </summary>
abstract class VehicleBuilder
{
protected Vehicle vehicle;

// Gets vehicle instance
public Vehicle Vehicle
{
get { return vehicle; }
}

// Abstract build methods
public abstract void BuildFrame();
public abstract void BuildEngine();
public abstract void BuildWheels();
public abstract void BuildDoors();
}

/// <summary>
/// The 'ConcreteBuilder1' class
/// </summary>
class MotorCycleBuilder : VehicleBuilder
{
public MotorCycleBuilder()
Expand Down Expand Up @@ -103,10 +91,6 @@ public override void BuildDoors()
}
}


/// <summary>
/// The 'ConcreteBuilder2' class
/// </summary>
class CarBuilder : VehicleBuilder
{
public CarBuilder()
Expand Down Expand Up @@ -135,9 +119,6 @@ public override void BuildDoors()
}
}

/// <summary>
/// The 'ConcreteBuilder3' class
/// </summary>
class ScooterBuilder : VehicleBuilder
{
public ScooterBuilder()
Expand Down Expand Up @@ -166,22 +147,16 @@ public override void BuildDoors()
}
}

/// <summary>
/// The 'Product' class
/// </summary>
class Vehicle
{
private string _vehicleType;
private Dictionary<string, string> _parts =
new Dictionary<string, string>();
private Dictionary<string, string> _parts = new Dictionary<string, string>();

// Constructor
public Vehicle(string vehicleType)
{
this._vehicleType = vehicleType;
}

// Indexer
public string this[string key]
{
get { return _parts[key]; }
Expand All @@ -198,4 +173,4 @@ public void Show()
Debug.Log(" #Doors : " + _parts["doors"]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ void Start()
public interface IRobotPlan
{
void SetRobotHead(string head);

void SetRobotTorso(string torso);

void SetRobotArms(string arms);

void SetRobotLegs(string legs);
}

Expand Down Expand Up @@ -65,10 +62,6 @@ public string ToStringEX()
}
}




// they're kinda like a blueprint these RobotBuilder classes:
public interface IRobotBuilder
{
Robot GetRobot();
Expand All @@ -78,7 +71,6 @@ public interface IRobotBuilder
void BuildRobotLegs();
}

// for each new robot that you might want to have just create a new RobotBuilder Object
public class OldRobotBuilder : IRobotBuilder
{
protected Robot robot { get; set; }
Expand Down Expand Up @@ -114,9 +106,6 @@ public void BuildRobotLegs()
}
}



// he just calls the method in the Robot Objects (which are defined by the interface, just think of blueprints)
public class RobotEngineer
{
public IRobotBuilder robotBuilder { get; protected set; }
Expand All @@ -139,6 +128,4 @@ public void MakeRobot()
this.robotBuilder.BuildRobotLegs();
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class BuilderStructure : MonoBehaviour
{
void Start ( )
void Start()
{
// Create director and builders
Director director = new Director();
Expand All @@ -27,9 +27,6 @@ void Start ( )
}
}

/// <summary>
/// The 'Director' class
/// </summary>
class Director
{
// Builder uses a complex series of steps
Expand All @@ -40,19 +37,13 @@ public void Construct(Builder builder)
}
}

/// <summary>
/// The 'Builder' abstract class
/// </summary>
abstract class Builder
{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract Product GetResult();
}

/// <summary>
/// The 'ConcreteBuilder1' class
/// </summary>
class ConcreteBuilder1 : Builder
{
private Product _product = new Product();
Expand All @@ -73,9 +64,6 @@ public override Product GetResult()
}
}

/// <summary>
/// The 'ConcreteBuilder2' class
/// </summary>
class ConcreteBuilder2 : Builder
{
private Product _product = new Product();
Expand All @@ -96,9 +84,6 @@ public override Product GetResult()
}
}

/// <summary>
/// The 'Product' class
/// </summary>
class Product
{
private List<string> _parts = new List<string>();
Expand All @@ -110,11 +95,10 @@ public void Add(string part)

public void Show()
{
Debug.Log("\nProduct Parts -------");
Debug.Log("\nProduct Parts -------");
foreach (string part in _parts)
{
Debug.Log(part);
}

}
}
17 changes: 10 additions & 7 deletions Unity-Design-Pattern.sln
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity-Design-Pattern", "Unity-Design-Pattern.csproj", "{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}"
Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity-Design-Pattern", "Assembly-CSharp.csproj", "{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA5D6718-EC1F-D1EA-526D-52AF3C037FBB}.Release|Any CPU.Build.0 = Release|Any CPU
{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8ABF50DE-5C45-E1BE-2FA5-8FD59671116B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = Assembly-CSharp.csproj
EndGlobalSection
EndGlobal