Advertisement
Slash18

C# Builder

Jun 21st, 2016
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. //    Full Tutorial on indiedevart.wordpress.com
  2.  
  3. // Indie dev Art example of Builder Design Pattern
  4. //https://indiedevart.wordpress.com/2016/06/21/c-builder-design-pattern/
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ConsoleApplication4
  12. {
  13.     class EnemyCreator
  14.     {
  15.         private readonly IEnemy enemyBuilder;
  16.  
  17.         public EnemyCreator( IEnemy enemyType)
  18.         {
  19.             enemyBuilder = enemyType;
  20.         }
  21.         public Enemy ConstructEnemy()
  22.         {
  23.             enemyBuilder.SelectBehaviour();
  24.             enemyBuilder.SelectModel();
  25.             enemyBuilder.AddComponents();
  26.            
  27.             return enemyBuilder.GetEnemy();
  28.     }
  29.     }
  30.  
  31.  
  32.     class Program
  33.     {
  34.         static void Main(string[] args)
  35.         {
  36.             var enemyCreator = new EnemyCreator(new EnemyTypeA());
  37.             var enemy = enemyCreator.ConstructEnemy();
  38.             enemy.ShowInfo();
  39.             Console.WriteLine("_______________________");
  40.             enemyCreator = new EnemyCreator(new EnemyTypeB());
  41.             enemy = enemyCreator.ConstructEnemy();
  42.             enemy.ShowInfo();
  43.             Console.ReadLine();
  44.         }
  45.     }
  46.     interface IEnemy{
  47.         void SelectModel();
  48.         void SelectBehaviour();
  49.         void AddComponents();
  50.  
  51.         Enemy GetEnemy();
  52.  
  53.     }
  54.     class EnemyTypeA : IEnemy
  55.     {
  56.         Enemy enemy = new Enemy();
  57.         public void AddComponents()
  58.         {
  59.             enemy.Components= "Components Added";
  60.         }
  61.  
  62.         public void SelectBehaviour()
  63.         {
  64.             enemy.Behaviour= "Behaviour Type A Selected";
  65.         }
  66.  
  67.         public void SelectModel()
  68.         {
  69.             enemy.Model = "Body Type A Selected";
  70.         }
  71.         public Enemy GetEnemy()
  72.         {
  73.             return enemy;
  74.         }
  75.     }
  76.     class EnemyTypeB : IEnemy
  77.     {
  78.         Enemy enemy = new Enemy();
  79.         public void AddComponents()
  80.         {
  81.             enemy.Components = "Components Added";
  82.         }
  83.  
  84.         public void SelectBehaviour()
  85.         {
  86.             enemy.Behaviour = "Behaviour Type B Selected";
  87.         }
  88.  
  89.         public void SelectModel()
  90.         {
  91.             enemy.Model = "Body Type B Selected";
  92.         }
  93.         public Enemy GetEnemy()
  94.         {
  95.             return enemy;
  96.         }
  97.     }
  98.     class Enemy
  99.     {
  100.         public string Model;
  101.         public string Behaviour;
  102.         public string Components;
  103.  
  104.         public void ShowInfo()
  105.         {
  106.             Console.WriteLine(Model);
  107.             Console.WriteLine(Behaviour);
  108.             Console.WriteLine(Components);
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement