none
An OOP Question RRS feed

  • Question

  • Hi Friends,

    I was wondering why would i want to instantiate an object inside of a class?

    For example

    Public Class Customer

    {

    Service service = new Service("Cool Service");

    }

    Why couldn't I just have a method in the Customer class to do what The service class is doing? Thanks !


    • Edited by Ronald Rex Saturday, April 20, 2019 8:59 PM
    Saturday, April 20, 2019 8:56 PM

Answers

  • Hello,

    How I see it in this case is Customer class is a simple container where the service will populate an instance of Customer or some form of list.  A service should stand along, not know about who is calling it or what it will be used for.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ronald Rex Saturday, April 20, 2019 10:42 PM
    Saturday, April 20, 2019 9:20 PM
    Moderator
  • Why couldn't I just have a method in the Customer class to do what The service class is doing? Thanks !

    You could do that, but then you're not taking the advantage of using OO.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.c-sharpcorner.com/UploadFile/yusufkaratoprak/difference-between-loose-coupling-and-tight-coupling/

    https://ardalis.com/new-is-glue

    • Marked as answer by Ronald Rex Saturday, April 20, 2019 10:43 PM
    Saturday, April 20, 2019 9:49 PM
  • I think you may be looking at Service in reference to a Web Service, but I could be wrong. I just used a class and named it service for no particular reason. I just wanted to understand from a very basic fundamental generic instance as to why I would use a class inside of another class. I dont mean to sound sarcastic, but  i just think maybe you applied some additional concepts that flew over my head. 

    A class is a blueprint for an object that gives the object behavior/methods ,variables and public properties, a class is instanced into an object and it lives in memory.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    OO is OO no matter if the OO platform is Java or .NET, becuase the principles are the same.

    A Windows form is a class/object, it's a container for a control that is a class/object too  Why should the Windows form have all the code in it for the Customer logic? Why can't the form class/object instance a Customer object and use the behavior of the Customer object? Why can't the Customer object instance the Order object and use the behavior of the Order object? Why can't the Order object instance the Inventory object and use the Inventory object behavior? Why can't the Customer object finally instance the Shipping object and use the behavior of the Shipping object to ship the order to the customer?

    The base class/object for all classes/objects in.NET  is System.Object and all classes/object derive from the base object. Therefore, a class/object is already in a class/object out the gate. :) 

    https://docs.microsoft.com/en-us/dotnet/api/system.object?view=netframework-4.8

    Finally, why can't an object provide services to another object?

    Capiche?


    • Edited by DA924x Sunday, April 21, 2019 6:44 AM
    • Marked as answer by Ronald Rex Sunday, April 21, 2019 7:30 PM
    Sunday, April 21, 2019 6:44 AM

All replies

  • Hello,

    How I see it in this case is Customer class is a simple container where the service will populate an instance of Customer or some form of list.  A service should stand along, not know about who is calling it or what it will be used for.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ronald Rex Saturday, April 20, 2019 10:42 PM
    Saturday, April 20, 2019 9:20 PM
    Moderator
  • Why couldn't I just have a method in the Customer class to do what The service class is doing? Thanks !

    You could do that, but then you're not taking the advantage of using OO.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    https://www.c-sharpcorner.com/UploadFile/yusufkaratoprak/difference-between-loose-coupling-and-tight-coupling/

    https://ardalis.com/new-is-glue

    • Marked as answer by Ronald Rex Saturday, April 20, 2019 10:43 PM
    Saturday, April 20, 2019 9:49 PM
  • I think you may be looking at Service in reference to a Web Service, but I could be wrong. I just used a class and named it service for no particular reason. I just wanted to understand from a very basic fundamental generic instance as to why I would use a class inside of another class. I dont mean to sound sarcastic, but  i just think maybe you applied some additional concepts that flew over my head. 
    Saturday, April 20, 2019 10:56 PM
  • I think you may be looking at Service in reference to a Web Service, but I could be wrong. I just used a class and named it service for no particular reason. I just wanted to understand from a very basic fundamental generic instance as to why I would use a class inside of another class. I dont mean to sound sarcastic, but  i just think maybe you applied some additional concepts that flew over my head. 

    A class is a blueprint for an object that gives the object behavior/methods ,variables and public properties, a class is instanced into an object and it lives in memory.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    OO is OO no matter if the OO platform is Java or .NET, becuase the principles are the same.

    A Windows form is a class/object, it's a container for a control that is a class/object too  Why should the Windows form have all the code in it for the Customer logic? Why can't the form class/object instance a Customer object and use the behavior of the Customer object? Why can't the Customer object instance the Order object and use the behavior of the Order object? Why can't the Order object instance the Inventory object and use the Inventory object behavior? Why can't the Customer object finally instance the Shipping object and use the behavior of the Shipping object to ship the order to the customer?

    The base class/object for all classes/objects in.NET  is System.Object and all classes/object derive from the base object. Therefore, a class/object is already in a class/object out the gate. :) 

    https://docs.microsoft.com/en-us/dotnet/api/system.object?view=netframework-4.8

    Finally, why can't an object provide services to another object?

    Capiche?


    • Edited by DA924x Sunday, April 21, 2019 6:44 AM
    • Marked as answer by Ronald Rex Sunday, April 21, 2019 7:30 PM
    Sunday, April 21, 2019 6:44 AM
  • Thank You !!! That helped out a lot 
    Sunday, April 21, 2019 7:34 PM