Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Candy
- {
- protected string name;
- public abstract void Made();
- }
- class CaramelCandy: Candy
- {
- public CaramelCandy()
- {
- name = "meller";
- }
- public override void Made()
- {
- Console.WriteLine("{0} was produced", this.name);
- }
- }
- class ChocolateCandy: Candy
- {
- public ChocolateCandy()
- {
- name = "milka";
- }
- public override void Made()
- {
- Console.WriteLine("{0} was produced", this.name);
- }
- }
- class CandyFactory
- {
- private Hashtable candys = new Hashtable();
- public Candy GetCandy(string key)
- {
- Candy candy = candys[key] as Candy;
- if (candy == null)
- {
- switch (key)
- {
- case "caramel": candy = new CaramelCandy(); break;
- case "chocolate": candy = new ChocolateCandy(); break;
- }
- candys.Add(key, candy);
- }
- return candy;
- }
- }
- static void Main(string[] args)
- {
- CandyFactory cf = new CandyFactory();
- Candy candy1 = cf.GetCandy("caramel");
- candy1.Made();
- candy1 = cf.GetCandy("chocolate");
- candy1.Made();
- }
Add Comment
Please, Sign In to add comment