Skip to content

Commit 22b3359

Browse files
committed
add 2 more example
add 2 more example
1 parent b6ae8eb commit 22b3359

10 files changed

Lines changed: 414 additions & 0 deletions

Assets/Behavioral Patterns/Visitor Pattern/Exmaple2.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: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//-------------------------------------------------------------------------------------
2+
// VisitorPatternExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
9+
namespace VisitorPatternExample2
10+
{
11+
public class VisitorPatternExample2 : MonoBehaviour
12+
{
13+
void Start()
14+
{
15+
// Setup employee collection
16+
Employees e = new Employees();
17+
e.Attach(new Clerk());
18+
e.Attach(new Director());
19+
e.Attach(new President());
20+
21+
// Employees are 'visited'
22+
e.Accept(new IncomeVisitor());
23+
e.Accept(new VacationVisitor());
24+
}
25+
26+
27+
/// <summary>
28+
/// The 'Visitor' interface
29+
/// </summary>
30+
interface IVisitor
31+
{
32+
void Visit(Element element);
33+
}
34+
35+
/// <summary>
36+
/// A 'ConcreteVisitor' class
37+
/// </summary>
38+
class IncomeVisitor : IVisitor
39+
{
40+
public void Visit(Element element)
41+
{
42+
Employee employee = element as Employee;
43+
44+
// Provide 10% pay raise
45+
employee.Income *= 1.10;
46+
47+
Debug.Log(string.Format("{0} {1}'s new income: {2:C}",
48+
employee.GetType().Name, employee.Name,
49+
employee.Income));
50+
}
51+
}
52+
53+
/// <summary>
54+
/// A 'ConcreteVisitor' class
55+
/// </summary>
56+
class VacationVisitor : IVisitor
57+
{
58+
public void Visit(Element element)
59+
{
60+
Employee employee = element as Employee;
61+
62+
// Provide 3 extra vacation days
63+
employee.VacationDays += 3;
64+
Debug.Log(string.Format("{0} {1}'s new vacation days: {2}",
65+
employee.GetType().Name, employee.Name,
66+
employee.VacationDays));
67+
}
68+
}
69+
70+
/// <summary>
71+
/// The 'Element' abstract class
72+
/// </summary>
73+
abstract class Element
74+
{
75+
public abstract void Accept(IVisitor visitor);
76+
}
77+
78+
/// <summary>
79+
/// The 'ConcreteElement' class
80+
/// </summary>
81+
class Employee : Element
82+
{
83+
private string _name;
84+
private double _income;
85+
private int _vacationDays;
86+
87+
// Constructor
88+
public Employee(string name, double income,
89+
int vacationDays)
90+
{
91+
this._name = name;
92+
this._income = income;
93+
this._vacationDays = vacationDays;
94+
}
95+
96+
// Gets or sets the name
97+
public string Name
98+
{
99+
get { return _name; }
100+
set { _name = value; }
101+
}
102+
103+
// Gets or sets income
104+
public double Income
105+
{
106+
get { return _income; }
107+
set { _income = value; }
108+
}
109+
110+
// Gets or sets number of vacation days
111+
public int VacationDays
112+
{
113+
get { return _vacationDays; }
114+
set { _vacationDays = value; }
115+
}
116+
117+
public override void Accept(IVisitor visitor)
118+
{
119+
visitor.Visit(this);
120+
}
121+
}
122+
123+
/// <summary>
124+
/// The 'ObjectStructure' class
125+
/// </summary>
126+
class Employees
127+
{
128+
private List<Employee> _employees = new List<Employee>();
129+
130+
public void Attach(Employee employee)
131+
{
132+
_employees.Add(employee);
133+
}
134+
135+
public void Detach(Employee employee)
136+
{
137+
_employees.Remove(employee);
138+
}
139+
140+
public void Accept(IVisitor visitor)
141+
{
142+
foreach (Employee e in _employees)
143+
{
144+
e.Accept(visitor);
145+
}
146+
}
147+
}
148+
149+
// Three employee types
150+
151+
class Clerk : Employee
152+
{
153+
// Constructor
154+
public Clerk()
155+
: base("Hank", 25000.0, 14)
156+
{
157+
}
158+
}
159+
160+
class Director : Employee
161+
{
162+
// Constructor
163+
public Director()
164+
: base("Elly", 35000.0, 16)
165+
{
166+
}
167+
}
168+
169+
class President : Employee
170+
{
171+
// Constructor
172+
public President()
173+
: base("Dick", 45000.0, 21)
174+
{
175+
}
176+
}
177+
178+
179+
180+
}
181+
182+
}
183+

Assets/Behavioral Patterns/Visitor Pattern/Exmaple2/VisitorPatternExample2.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/Behavioral Patterns/Visitor Pattern/Exmaple2/VisitorPatternExample2.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/Abstract Factory Pattern/Example2.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: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
//-------------------------------------------------------------------------------------
2+
// AbstractFactoryPatternExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
// allows a family of related objects without specifying the concrete class
6+
// when there are many objects that can be added or changed dynamically
7+
// you can model everything you can image and have those objects interact through common interfaces
8+
9+
// negative: can get very complicated
10+
11+
12+
using UnityEngine;
13+
using System.Collections;
14+
15+
namespace AbstractFactoryPatternExample2
16+
{
17+
18+
public class AbstractFactoryPatternExample2 : MonoBehaviour
19+
{
20+
void Start ( )
21+
{
22+
EnemyShipBuilding ufoBuilder = new UFOEnemyShipBuilding();
23+
ufoBuilder.orderShip(ShipType.UFO);
24+
}
25+
}
26+
27+
public enum ShipType
28+
{
29+
UFO
30+
}
31+
32+
33+
public abstract class EnemyShipBuilding
34+
{
35+
// abstract order form:
36+
protected abstract EnemyShip MakeEnemyShip(ShipType type);
37+
38+
public EnemyShip orderShip(ShipType type)
39+
{
40+
EnemyShip ship = MakeEnemyShip(type);
41+
42+
ship.MakeShip();
43+
ship.DisplayShip();
44+
ship.FollowHeroShip();
45+
ship.Shoot();
46+
47+
return ship;
48+
}
49+
}
50+
51+
public class UFOEnemyShipBuilding : EnemyShipBuilding
52+
{
53+
// Make Ship varies per ship type...
54+
protected override EnemyShip MakeEnemyShip(ShipType type)
55+
{
56+
EnemyShip ship = null;
57+
58+
if (type == ShipType.UFO)
59+
{
60+
IEnemyShipFactory factory = new UFOEnemyShipFactory();
61+
ship = new UFOEnemyShip(factory);
62+
ship.name = "UFO";
63+
}
64+
65+
return ship;
66+
}
67+
}
68+
69+
70+
71+
72+
73+
public interface IEnemyShipFactory
74+
{
75+
IESWeapon AddESGun();
76+
IESEngine AddESEngine();
77+
}
78+
79+
public class UFOEnemyShipFactory : IEnemyShipFactory
80+
{
81+
// each factory can add different weapons and stuff
82+
public IESWeapon AddESGun()
83+
{
84+
return new ESUFOGun();
85+
}
86+
87+
public IESEngine AddESEngine()
88+
{
89+
return new ESUFOEngine();
90+
}
91+
}
92+
93+
94+
95+
96+
97+
public abstract class EnemyShip
98+
{
99+
public string name;
100+
protected IESEngine engine;
101+
protected IESWeapon weapon;
102+
103+
public abstract void MakeShip();
104+
105+
public void DisplayShip()
106+
{
107+
Debug.Log(name + " is on the screen.");
108+
}
109+
110+
public void FollowHeroShip()
111+
{
112+
Debug.Log(name + " follows hero ship with " + engine.ToString());
113+
}
114+
115+
public void Shoot()
116+
{
117+
Debug.Log(name + " shoots and does " + weapon.ToString());
118+
}
119+
120+
public string ToString()
121+
{
122+
return "The " + name + " has a speed of " + engine.ToString() + " a firepower of " + weapon.ToString();
123+
}
124+
}
125+
126+
public class UFOEnemyShip : EnemyShip
127+
{
128+
IEnemyShipFactory factory;
129+
130+
public UFOEnemyShip(IEnemyShipFactory factory)
131+
{
132+
this.factory = factory;
133+
}
134+
135+
public override void MakeShip()
136+
{
137+
Debug.Log("Making enemy ship " + name);
138+
weapon = factory.AddESGun();
139+
engine = factory.AddESEngine();
140+
}
141+
}
142+
143+
144+
145+
// possible Weapons to swap in and out
146+
public interface IESWeapon
147+
{
148+
string ToString();
149+
}
150+
151+
public interface IESEngine
152+
{
153+
string ToString();
154+
}
155+
156+
public class ESUFOGun : IESWeapon
157+
{
158+
public string ToString()
159+
{
160+
return "20 damage";
161+
}
162+
}
163+
public class ESUFOEngine : IESEngine
164+
{
165+
public string ToString()
166+
{
167+
return "1000 mph";
168+
}
169+
}
170+
171+
172+
}
173+

0 commit comments

Comments
 (0)