Skip to content

Commit e75f5c2

Browse files
committed
add creational pattern exmaples
1 parent f049aac commit e75f5c2

27 files changed

+824
-6
lines changed

Assets/Creational Patterns/Abstract Factory Pattern/Example1.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//-------------------------------------------------------------------------------------
2+
// AbstractFactoryPatternExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
9+
//This structural code demonstrates the Abstract Factory pattern creating parallel hierarchies of objects.
10+
//Object creation has been abstracted and there is no need for hard-coded class names in the client code.
11+
12+
namespace AbstractFactoryPatternExample1
13+
{
14+
public class AbstractFactoryPatternExample1 : MonoBehaviour
15+
{
16+
void Start()
17+
{
18+
// Create and run the African animal world
19+
ContinentFactory africa = new AfricaFactory();
20+
AnimalWorld world = new AnimalWorld(africa);
21+
world.RunFoodChain();
22+
23+
// Create and run the American animal world
24+
ContinentFactory america = new AmericaFactory();
25+
world = new AnimalWorld(america);
26+
world.RunFoodChain();
27+
}
28+
}
29+
30+
31+
/// <summary>
32+
/// The 'AbstractFactory' abstract class
33+
/// </summary>
34+
abstract class ContinentFactory
35+
{
36+
public abstract Herbivore CreateHerbivore();
37+
public abstract Carnivore CreateCarnivore();
38+
}
39+
40+
/// <summary>
41+
/// The 'ConcreteFactory1' class
42+
/// </summary>
43+
class AfricaFactory : ContinentFactory
44+
{
45+
public override Herbivore CreateHerbivore()
46+
{
47+
return new Wildebeest();
48+
}
49+
public override Carnivore CreateCarnivore()
50+
{
51+
return new Lion();
52+
}
53+
}
54+
55+
/// <summary>
56+
/// The 'ConcreteFactory2' class
57+
/// </summary>
58+
class AmericaFactory : ContinentFactory
59+
{
60+
public override Herbivore CreateHerbivore()
61+
{
62+
return new Bison();
63+
}
64+
public override Carnivore CreateCarnivore()
65+
{
66+
return new Wolf();
67+
}
68+
}
69+
70+
/// <summary>
71+
/// The 'AbstractProductA' abstract class
72+
/// </summary>
73+
abstract class Herbivore
74+
{
75+
}
76+
77+
/// <summary>
78+
/// The 'AbstractProductB' abstract class
79+
/// </summary>
80+
abstract class Carnivore
81+
{
82+
public abstract void Eat(Herbivore h);
83+
}
84+
85+
/// <summary>
86+
/// The 'ProductA1' class
87+
/// </summary>
88+
class Wildebeest : Herbivore
89+
{
90+
}
91+
92+
/// <summary>
93+
/// The 'ProductB1' class
94+
/// </summary>
95+
class Lion : Carnivore
96+
{
97+
public override void Eat(Herbivore h)
98+
{
99+
// Eat Wildebeest
100+
Debug.Log(this.GetType().Name +" eats " + h.GetType().Name);
101+
}
102+
}
103+
104+
/// <summary>
105+
/// The 'ProductA2' class
106+
/// </summary>
107+
class Bison : Herbivore
108+
{
109+
}
110+
111+
/// <summary>
112+
/// The 'ProductB2' class
113+
/// </summary>
114+
class Wolf : Carnivore
115+
{
116+
public override void Eat(Herbivore h)
117+
{
118+
// Eat Bison
119+
Debug.Log(this.GetType().Name +" eats " + h.GetType().Name);
120+
}
121+
}
122+
123+
/// <summary>
124+
/// The 'Client' class
125+
/// </summary>
126+
class AnimalWorld
127+
{
128+
private Herbivore _herbivore;
129+
private Carnivore _carnivore;
130+
131+
// Constructor
132+
public AnimalWorld(ContinentFactory factory)
133+
{
134+
_carnivore = factory.CreateCarnivore();
135+
_herbivore = factory.CreateHerbivore();
136+
}
137+
138+
public void RunFoodChain()
139+
{
140+
_carnivore.Eat(_herbivore);
141+
}
142+
}
143+
}

Assets/Creational Patterns/Abstract Factory Pattern/Example1/AbstractFactoryPatternExample1.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

Assets/Creational Patterns/Abstract Factory Pattern/Example1/AbstractFactoryPatternExample1.unity.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Creational Patterns/Builder Pattern/Example1.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
//-------------------------------------------------------------------------------------
2+
// BuilderPatternExample1.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using System;
6+
using UnityEngine;
7+
using System.Collections;
8+
using System.Collections.Generic;
9+
10+
//This real-world code demonstates the Builder pattern in which different vehicles are assembled in a step-by-step fashion.
11+
//The Shop uses VehicleBuilders to construct a variety of Vehicles in a series of sequential steps.
12+
13+
namespace BuilderPatternExample1
14+
{
15+
public class BuilderPatternExample1 : MonoBehaviour
16+
{
17+
void Start()
18+
{
19+
VehicleBuilder builder;
20+
21+
// Create shop with vehicle builders
22+
Shop shop = new Shop();
23+
24+
// Construct and display vehicles
25+
builder = new ScooterBuilder();
26+
shop.Construct(builder);
27+
builder.Vehicle.Show();
28+
29+
builder = new CarBuilder();
30+
shop.Construct(builder);
31+
builder.Vehicle.Show();
32+
33+
builder = new MotorCycleBuilder();
34+
shop.Construct(builder);
35+
builder.Vehicle.Show();
36+
37+
}
38+
}
39+
40+
/// <summary>
41+
/// The 'Director' class
42+
/// </summary>
43+
class Shop
44+
{
45+
// Builder uses a complex series of steps
46+
public void Construct(VehicleBuilder vehicleBuilder)
47+
{
48+
vehicleBuilder.BuildFrame();
49+
vehicleBuilder.BuildEngine();
50+
vehicleBuilder.BuildWheels();
51+
vehicleBuilder.BuildDoors();
52+
}
53+
}
54+
55+
/// <summary>
56+
/// The 'Builder' abstract class
57+
/// </summary>
58+
abstract class VehicleBuilder
59+
{
60+
protected Vehicle vehicle;
61+
62+
// Gets vehicle instance
63+
public Vehicle Vehicle
64+
{
65+
get { return vehicle; }
66+
}
67+
68+
// Abstract build methods
69+
public abstract void BuildFrame();
70+
public abstract void BuildEngine();
71+
public abstract void BuildWheels();
72+
public abstract void BuildDoors();
73+
}
74+
75+
/// <summary>
76+
/// The 'ConcreteBuilder1' class
77+
/// </summary>
78+
class MotorCycleBuilder : VehicleBuilder
79+
{
80+
public MotorCycleBuilder()
81+
{
82+
vehicle = new Vehicle("MotorCycle");
83+
}
84+
85+
public override void BuildFrame()
86+
{
87+
vehicle["frame"] = "MotorCycle Frame";
88+
}
89+
90+
public override void BuildEngine()
91+
{
92+
vehicle["engine"] = "500 cc";
93+
}
94+
95+
public override void BuildWheels()
96+
{
97+
vehicle["wheels"] = "2";
98+
}
99+
100+
public override void BuildDoors()
101+
{
102+
vehicle["doors"] = "0";
103+
}
104+
}
105+
106+
107+
/// <summary>
108+
/// The 'ConcreteBuilder2' class
109+
/// </summary>
110+
class CarBuilder : VehicleBuilder
111+
{
112+
public CarBuilder()
113+
{
114+
vehicle = new Vehicle("Car");
115+
}
116+
117+
public override void BuildFrame()
118+
{
119+
vehicle["frame"] = "Car Frame";
120+
}
121+
122+
public override void BuildEngine()
123+
{
124+
vehicle["engine"] = "2500 cc";
125+
}
126+
127+
public override void BuildWheels()
128+
{
129+
vehicle["wheels"] = "4";
130+
}
131+
132+
public override void BuildDoors()
133+
{
134+
vehicle["doors"] = "4";
135+
}
136+
}
137+
138+
/// <summary>
139+
/// The 'ConcreteBuilder3' class
140+
/// </summary>
141+
class ScooterBuilder : VehicleBuilder
142+
{
143+
public ScooterBuilder()
144+
{
145+
vehicle = new Vehicle("Scooter");
146+
}
147+
148+
public override void BuildFrame()
149+
{
150+
vehicle["frame"] = "Scooter Frame";
151+
}
152+
153+
public override void BuildEngine()
154+
{
155+
vehicle["engine"] = "50 cc";
156+
}
157+
158+
public override void BuildWheels()
159+
{
160+
vehicle["wheels"] = "2";
161+
}
162+
163+
public override void BuildDoors()
164+
{
165+
vehicle["doors"] = "0";
166+
}
167+
}
168+
169+
/// <summary>
170+
/// The 'Product' class
171+
/// </summary>
172+
class Vehicle
173+
{
174+
private string _vehicleType;
175+
private Dictionary<string, string> _parts =
176+
new Dictionary<string, string>();
177+
178+
// Constructor
179+
public Vehicle(string vehicleType)
180+
{
181+
this._vehicleType = vehicleType;
182+
}
183+
184+
// Indexer
185+
public string this[string key]
186+
{
187+
get { return _parts[key]; }
188+
set { _parts[key] = value; }
189+
}
190+
191+
public void Show()
192+
{
193+
Debug.Log("\n---------------------------");
194+
Debug.Log("Vehicle Type: " + _vehicleType);
195+
Debug.Log(" Frame : " + _parts["frame"]);
196+
Debug.Log(" Engine : " + _parts["engine"]);
197+
Debug.Log(" #Wheels: " + _parts["wheels"]);
198+
Debug.Log(" #Doors : " + _parts["doors"]);
199+
}
200+
}
201+
}

0 commit comments

Comments
 (0)