CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Abstract Class

  1. #1
    Join Date
    Sep 2011
    Posts
    1

    Abstract Class

    I have implemented a program for Facade pattern.

    Below 2 posted are the same code, one using abstract class and another using a concrete class.

    As I am bad with implementing anything using abstract class, I need your help to confirm that whatever I have implemented using the abstract class is correct or not.

    Please HELP!!!

    Code without Abstract class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Facade_CSharp
    {
    class Program
    {
    static void Main(string[] args)
    {
    Facade facade = new Facade();

    facade.ProcessA();
    facade.ProcessB();

    // Wait for user
    Console.ReadKey();
    }
    }

    /// <summary>
    /// The 'Subsystem ClassA' class
    /// </summary>
    class SubSystemOne
    {
    public void MethodOne()
    {
    Console.WriteLine(" SubSystem One");
    }
    }

    /// <summary>
    /// The 'Subsystem ClassB' class
    /// </summary>
    class SubSystemTwo
    {
    public void MethodTwo()
    {
    Console.WriteLine(" SubSystem Two");
    }
    }

    /// <summary>
    /// The 'Subsystem ClassC' class
    /// </summary>
    class SubSystemThree
    {
    public void MethodThree()
    {
    Console.WriteLine(" SubSystem Three");
    }
    }

    /// <summary>
    /// The 'Subsystem ClassD' class
    /// </summary>
    class SubSystemFour
    {
    public void MethodFour()
    {
    Console.WriteLine(" SubSystem Four");
    }
    }

    /// <summary>
    /// The 'Facade' class
    /// </summary>
    class Facade
    {
    private SubSystemOne _one;
    private SubSystemTwo _two;
    private SubSystemThree _three;
    private SubSystemFour _four;

    public Facade()
    {
    Console.WriteLine("\nRequests received from Client System and Facade is in execution... ");

    _one = new SubSystemOne();
    _two = new SubSystemTwo();
    _three = new SubSystemThree();
    _four = new SubSystemFour();
    }

    public void ProcessA()
    {
    Console.WriteLine("\nProcessA of Facade uses the following subsystems to accomplish the task:");
    _one.MethodOne();
    _two.MethodTwo();
    _four.MethodFour();
    }

    public void ProcessB()
    {
    Console.WriteLine("\nProcessB of Facade uses the following subsystems to accomplish the task:");
    _two.MethodTwo();
    _three.MethodThree();
    }
    }
    }

    Code with Abstract Class: (This needs to be confirmed if its correctly implemented)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Facade_abstract
    {
    class Program
    {
    static void Main(string[] args)
    {
    FacadeAbs facade = new FacadeAbs();

    facade.ProcessA();
    facade.ProcessB();

    // Wait for user
    Console.ReadKey();

    }
    }

    class SubSystemOne
    {
    public void MethodOne()
    {
    Console.WriteLine(" SubSystem One");
    }
    }

    /// <summary>
    /// The 'Subsystem ClassB' class
    /// </summary>
    class SubSystemTwo
    {
    public void MethodTwo()
    {
    Console.WriteLine(" SubSystem Two");
    }
    }

    /// <summary>
    /// The 'Subsystem ClassC' class
    /// </summary>
    class SubSystemThree
    {
    public void MethodThree()
    {
    Console.WriteLine(" SubSystem Three");
    }
    }

    /// <summary>
    /// The 'Subsystem ClassD' class
    /// </summary>
    class SubSystemFour
    {
    public void MethodFour()
    {
    Console.WriteLine(" SubSystem Four");
    }
    }

    /// <summary>
    /// The 'Facade' class
    /// </summary>
    public abstract class Facade
    {
    //public abstract Facade();

    public abstract void ProcessA();

    public abstract void ProcessB();

    }

    public class FacadeAbs : Facade
    {
    private SubSystemOne _one;
    private SubSystemTwo _two;
    private SubSystemThree _three;
    private SubSystemFour _four;

    public FacadeAbs()
    {
    Console.WriteLine("\nRequests received from Client System and Facade is in execution... ");

    _one = new SubSystemOne();
    _two = new SubSystemTwo();
    _three = new SubSystemThree();
    _four = new SubSystemFour();
    }


    public override void ProcessA()
    {
    Console.WriteLine("\nProcessA of Facade uses the following subsystems to accomplish the task:");
    _one.MethodOne();
    _two.MethodTwo();
    _four.MethodFour();
    }

    public override void ProcessB()
    {
    Console.WriteLine("\nProcessB of Facade uses the following subsystems to accomplish the task:");
    _two.MethodTwo();
    _three.MethodThree();
    }

    }
    }

    Output of both the programs:

    Requests received from Client System and Facade is in execution...

    ProcessA of Facade uses the following subsystems to accomplish the task:
    SubSystem One
    SubSystem Two
    SubSystem Four

    ProcessB of Facade uses the following subsystems to accomplish the task:
    SubSystem Two
    SubSystem Three
    Thanks in advance.

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Abstract Class

    Welcome to the forum

    please use [ code ] [ /code ] around your code to make it more readable.
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Abstract Class

    "Correct" how? What are the problems you are having?
    If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.

    Yes; I have a blog too - http://the-angry-gorilla.com/

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured