Skip to content

Commit bcf69a0

Browse files
committed
add more example
1 parent 35bfc6a commit bcf69a0

41 files changed

Lines changed: 393 additions & 1862 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Binary file not shown.

Assets/Structural Patterns/Decorator 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: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
//-------------------------------------------------------------------------------------
2+
// DecoratorPatternExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
namespace DecoratorPatternExample2
9+
{
10+
public class DecoratorPatternExample2 : MonoBehaviour
11+
{
12+
void Start()
13+
{
14+
// Make Pizzas:
15+
IPizza basicPizza = new TomatoSauce(new Mozzarella(new PlainPizza()));
16+
Debug.Log("Ingredients of Pizza: " + basicPizza.GetDescription());
17+
Debug.Log("Total Cost: " + basicPizza.GetCost());
18+
}
19+
}
20+
21+
22+
23+
public interface IPizza
24+
{
25+
string GetDescription();
26+
double GetCost();
27+
}
28+
29+
30+
public class PlainPizza : IPizza
31+
{
32+
public string GetDescription()
33+
{
34+
return "Thin Dough";
35+
}
36+
37+
public double GetCost()
38+
{
39+
return 4.00;
40+
}
41+
}
42+
43+
44+
45+
public abstract class ToppingDecorator : IPizza
46+
{
47+
protected IPizza tempPizza;
48+
49+
public ToppingDecorator(IPizza newPizza)
50+
{
51+
this.tempPizza = newPizza;
52+
}
53+
54+
55+
public virtual string GetDescription()
56+
{
57+
return tempPizza.GetDescription();
58+
}
59+
60+
public virtual double GetCost()
61+
{
62+
return tempPizza.GetCost();
63+
}
64+
}
65+
66+
67+
68+
public class Mozzarella : ToppingDecorator
69+
{
70+
public Mozzarella(IPizza newPizza) : base(newPizza)
71+
{
72+
Debug.Log("Adding Dough");
73+
Debug.Log("Adding Morarella");
74+
}
75+
76+
public override string GetDescription()
77+
{
78+
return tempPizza.GetDescription() + ", Mozzarella";
79+
}
80+
81+
public override double GetCost()
82+
{
83+
return tempPizza.GetCost() + 0.50;
84+
}
85+
}
86+
87+
88+
public class TomatoSauce : ToppingDecorator
89+
{
90+
public TomatoSauce(IPizza newPizza) : base(newPizza)
91+
{
92+
Debug.Log("Adding Sauce");
93+
}
94+
95+
public override string GetDescription()
96+
{
97+
return tempPizza.GetDescription() + ", Tomato Sauce";
98+
}
99+
100+
public override double GetCost()
101+
{
102+
return tempPizza.GetCost() + 0.35;
103+
}
104+
}
105+
106+
107+
108+
109+
namespace BadStyleExample
110+
{
111+
public abstract class Pizza
112+
{
113+
public abstract void SetDescription(string newDescription);
114+
public abstract string GetDescription();
115+
public abstract double GetCost();
116+
//public abstract bool HasFontina();
117+
}
118+
119+
// this way would force to create an infinite amount of subclasses for each type of pizza
120+
// and if cost if calculated off of individual topings you would have to change cost for all pizzas
121+
// if cost for one topping chages
122+
public class ThreeCheesePizza : Pizza
123+
{
124+
public override void SetDescription(string newDescription)
125+
{
126+
}
127+
128+
public override string GetDescription()
129+
{
130+
return "Mozarella, Fontina, Parmesan, Cheese Pizza";
131+
}
132+
133+
public override double GetCost()
134+
{
135+
return 10.00;
136+
}
137+
138+
}
139+
}
140+
141+
}

Assets/Structural Patterns/Decorator Pattern/Exmaple2/DecoratorPatternExample2.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/Structural Patterns/Decorator Pattern/Exmaple2/DecoratorPatternExample2.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/Structural Patterns/Facade 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: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
//-------------------------------------------------------------------------------------
2+
// FacadePatternExample2.cs
3+
//-------------------------------------------------------------------------------------
4+
5+
using UnityEngine;
6+
using System.Collections;
7+
8+
namespace FacadePatternExample2
9+
{
10+
public class FacadePatternExample2 : MonoBehaviour
11+
{
12+
void Start()
13+
{
14+
BankAccountFacade bankAccount = new BankAccountFacade(12345678, 1234);
15+
16+
Debug.Log("\"I want to withdraw $50 dollars!\"");
17+
bankAccount.WithdrawCash(50.00);
18+
19+
Debug.Log("\"Mh ok now let me withdraw $5000 dollars!?\"");
20+
bankAccount.WithdrawCash(5000.00);
21+
22+
Debug.Log("\"What tf... maybe save some cash, here are $300 bucks ;-)\"");
23+
bankAccount.DepositCash(300.00);
24+
}
25+
}
26+
27+
public class WelcomeToBank
28+
{
29+
public WelcomeToBank()
30+
{
31+
Debug.Log("Welcome to ABC Bank");
32+
Debug.Log("We are happy to deposit your money :-)");
33+
}
34+
}
35+
36+
public class AccountNumberCheck
37+
{
38+
private int accountNumber = 12345678;
39+
40+
public int GetAccountNumber()
41+
{
42+
return accountNumber;
43+
}
44+
45+
public bool AccountActive(int accNumber)
46+
{
47+
if (accNumber == accountNumber)
48+
return true;
49+
else
50+
return false;
51+
}
52+
}
53+
54+
public class SecurityCodeCheck
55+
{
56+
private int securityCode = 1234;
57+
58+
public int GetSecurityCode()
59+
{
60+
return securityCode;
61+
}
62+
63+
public bool IsCodeCorrect(int code)
64+
{
65+
if (code == securityCode)
66+
return true;
67+
else
68+
return false;
69+
}
70+
}
71+
72+
public class FundsCheck
73+
{
74+
private double cashInAccount = 1000.00;
75+
76+
public double GetCashInAccount()
77+
{
78+
return cashInAccount;
79+
}
80+
81+
protected void DecreaseCashInAccount(double cash)
82+
{
83+
cashInAccount -= cash;
84+
}
85+
86+
protected void IncreaseCashInAccount(double cash)
87+
{
88+
cashInAccount += cash;
89+
}
90+
91+
public bool HaveEnoughMoney(double cashToWithdraw)
92+
{
93+
if (cashToWithdraw > GetCashInAccount())
94+
{
95+
Debug.Log("You don't have enouth money.");
96+
return false;
97+
}
98+
else
99+
{
100+
return true;
101+
}
102+
}
103+
104+
public void MakeDeposit(double cash)
105+
{
106+
IncreaseCashInAccount(cash);
107+
Debug.Log("Deposit complete. Current Balance is: " + GetCashInAccount());
108+
}
109+
110+
public void WithdrawMoney(double cash)
111+
{
112+
DecreaseCashInAccount(cash);
113+
Debug.Log("Withdraw complete. Current Balance is: " + GetCashInAccount());
114+
}
115+
}
116+
117+
public class BankAccountFacade
118+
{
119+
private int accountNumber;
120+
private int securityCode;
121+
AccountNumberCheck accChecker;
122+
SecurityCodeCheck codeChecker;
123+
FundsCheck fundChecker;
124+
WelcomeToBank bankWelcome;
125+
126+
public BankAccountFacade(int accountNumber, int newSecurityCode)
127+
{
128+
this.accountNumber = accountNumber;
129+
this.securityCode = newSecurityCode;
130+
131+
bankWelcome = new WelcomeToBank();
132+
codeChecker = new SecurityCodeCheck();
133+
accChecker = new AccountNumberCheck();
134+
fundChecker = new FundsCheck();
135+
}
136+
137+
public int GetAccountNumber()
138+
{
139+
return accountNumber;
140+
}
141+
142+
public int GetSecurityCode()
143+
{
144+
return securityCode;
145+
}
146+
147+
public void WithdrawCash(double cash)
148+
{
149+
if (accChecker.AccountActive(GetAccountNumber())
150+
&& codeChecker.IsCodeCorrect(GetSecurityCode())
151+
&& fundChecker.HaveEnoughMoney(cash))
152+
{
153+
fundChecker.WithdrawMoney(cash);
154+
Debug.Log("Transaction complete.");
155+
}
156+
else
157+
{
158+
Debug.Log("Transaction failed.");
159+
}
160+
}
161+
162+
public void DepositCash(double cash)
163+
{
164+
if (accChecker.AccountActive(GetAccountNumber())
165+
&& codeChecker.IsCodeCorrect(GetSecurityCode()))
166+
{
167+
fundChecker.MakeDeposit(cash);
168+
Debug.Log("Transaction complete.");
169+
}
170+
else
171+
{
172+
Debug.Log("Transaction failed.");
173+
}
174+
}
175+
}
176+
177+
}

Assets/Structural Patterns/Facade Pattern/Exmaple2/FacadePatternExample2.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.

0 commit comments

Comments
 (0)