Guest User

Untitled

a guest
Oct 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. namespace FactoryPattern_MobileFactory
  2. {
  3. class MainProgram
  4. {
  5. static void Main(string[] args)
  6. {
  7. MobileFactory[] factory = new MobileFactory[2];
  8.  
  9. factory[0] = new IPhoneCreator();
  10. factory[1] = new AndroidCreator();
  11.  
  12. foreach(MobileFactory creator in factory)
  13. {
  14. creator.CreateMobileDevice();
  15. }
  16. }
  17. }
  18.  
  19. public abstract class MobileFactory
  20. {
  21. public abstract IMobile CreateMobileDevice();
  22. }
  23.  
  24. public class IPhoneCreator : MobileFactory
  25. {
  26. public override IMobile CreateMobileDevice()
  27. {
  28. return new IPhone();
  29. }
  30. }
  31.  
  32. public class AndroidCreator : MobileFactory
  33. {
  34. public override IMobile CreateMobileDevice()
  35. {
  36. return new Android();
  37. }
  38. }
  39.  
  40. public interface IMobile
  41. {
  42. }
  43.  
  44. public class IPhone : IMobile
  45. {
  46. public IPhone()
  47. {
  48. Console.WriteLine("Hello IPhone!" );
  49. }
  50. }
  51.  
  52. public class Android : IMobile
  53. {
  54. public Android()
  55. {
  56. Console.WriteLine("Hello Android!");
  57. }
  58. }
  59.  
  60. }
Add Comment
Please, Sign In to add comment