Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Full Tutorial on indiedevart.wordpress.com
- // Indie dev Art example of Builder Design Pattern
- //https://indiedevart.wordpress.com/2016/06/21/c-builder-design-pattern/
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConsoleApplication4
- {
- class EnemyCreator
- {
- private readonly IEnemy enemyBuilder;
- public EnemyCreator( IEnemy enemyType)
- {
- enemyBuilder = enemyType;
- }
- public Enemy ConstructEnemy()
- {
- enemyBuilder.SelectBehaviour();
- enemyBuilder.SelectModel();
- enemyBuilder.AddComponents();
- return enemyBuilder.GetEnemy();
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var enemyCreator = new EnemyCreator(new EnemyTypeA());
- var enemy = enemyCreator.ConstructEnemy();
- enemy.ShowInfo();
- Console.WriteLine("_______________________");
- enemyCreator = new EnemyCreator(new EnemyTypeB());
- enemy = enemyCreator.ConstructEnemy();
- enemy.ShowInfo();
- Console.ReadLine();
- }
- }
- interface IEnemy{
- void SelectModel();
- void SelectBehaviour();
- void AddComponents();
- Enemy GetEnemy();
- }
- class EnemyTypeA : IEnemy
- {
- Enemy enemy = new Enemy();
- public void AddComponents()
- {
- enemy.Components= "Components Added";
- }
- public void SelectBehaviour()
- {
- enemy.Behaviour= "Behaviour Type A Selected";
- }
- public void SelectModel()
- {
- enemy.Model = "Body Type A Selected";
- }
- public Enemy GetEnemy()
- {
- return enemy;
- }
- }
- class EnemyTypeB : IEnemy
- {
- Enemy enemy = new Enemy();
- public void AddComponents()
- {
- enemy.Components = "Components Added";
- }
- public void SelectBehaviour()
- {
- enemy.Behaviour = "Behaviour Type B Selected";
- }
- public void SelectModel()
- {
- enemy.Model = "Body Type B Selected";
- }
- public Enemy GetEnemy()
- {
- return enemy;
- }
- }
- class Enemy
- {
- public string Model;
- public string Behaviour;
- public string Components;
- public void ShowInfo()
- {
- Console.WriteLine(Model);
- Console.WriteLine(Behaviour);
- Console.WriteLine(Components);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement