Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 154737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:01:55+00:00 2026-05-11T10:01:55+00:00

I made the following code example to learn how to use a generics method

  • 0

I made the following code example to learn how to use a generics method signature.

In order to get a Display() method for both Customer and Employee, I actually began replacing my IPerson interface with an Person abstract class.

But then I stopped, remembering a podcast in which Uncle Bob was telling Scott Hanselman about the Single Responsibility Principle in which you should have lots of little classes each doing one specific thing, i.e. that a Customer class should not have a Print() and Save() and CalculateSalary() method but that you should have a CustomerPrinter class and a CustomerSaver class and a CustomerSalaryCalculator class.

That seems an odd way to program. However, getting rid of my interface also felt wrong (since so many IoC containers and DI examples use them inherently) so I decided to give the Single Responsibility Principle a try.

So the following code is different than I have programmed in the past (I would have made an abstract class with a Display() method and got rid of the interface) but based on what I have heard about decoupling and the S.O.L.I.D. principles, this new way of coding (the interface and the PersonDisplayer class) I think this is the right way to go.

I would like to hear if others think the same way on this issue or have experienced positive or negative effects of this (e.g. an unwieldy number of classes each doing one particular thing, etc.).

using System;  namespace TestGeneric33 {     class Program     {         static void Main(string[] args)         {             Container container = new Container();             Customer customer1 = container.InstantiateType<Customer>('Jim', 'Smith');             Employee employee1 = container.InstantiateType<Employee>('Joe', 'Thompson');             Console.WriteLine(PersonDisplayer.SimpleDisplay(customer1));             Console.WriteLine(PersonDisplayer.SimpleDisplay(employee1));             Console.ReadLine();         }     }      public class Container     {         public T InstantiateType<T>(string firstName, string lastName) where T : IPerson, new()         {             T obj = new T();             obj.FirstName = firstName;             obj.LastName = lastName;             return obj;         }     }      public interface IPerson     {         string FirstName { get; set; }         string LastName { get; set; }     }      public class PersonDisplayer     {         private IPerson _person;          public PersonDisplayer(IPerson person)         {             _person = person;         }          public string SimpleDisplay()         {             return String.Format('{1}, {0}', _person.FirstName, _person.LastName);         }          public static string SimpleDisplay(IPerson person)         {             PersonDisplayer personDisplayer = new PersonDisplayer(person);             return personDisplayer.SimpleDisplay();         }     }      public class Customer : IPerson     {         public string FirstName { get; set; }         public string LastName { get; set; }         public string Company { get; set; }     }      public class Employee : IPerson     {         public string FirstName { get; set; }         public string LastName { get; set; }         public int EmployeeNumber { get; set; }     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:01:56+00:00Added an answer on May 11, 2026 at 10:01 am

    I like to think of the Single Responsibility Principle as an implementation of separation of duties. Before I start splitting my classes as you have, I try to think of what each class should be responsible for.

    Your classes are quite simple and lend themselves well to an abstract class with an implemented Print() and Save() functions as you mentioned. I would tend to keep that design over your current one.

    However, if printing and saving were more complicated tasks which might be performed in different ways then a dedicated Printer or Saver class would be warranted, since that responsibility is now more complex. The ‘complexity’ threshold for making a new class is very subjective and will depend on the exact situation, but in the end, the code is just an abstraction for us lowly humans to understand, so make it such that it’s the most intuitive.

    You Container class is a little misleading. It doesn’t actually ‘contain’ anything. It actually implements the Factory Method Pattern and would benefit from being named a factory.

    Also, your PersonDisplayer is never instantiated and can provide all of its functionality through static methods, so why not make it a static class? It’s not uncommon for utility classes such as Printers or savers to be static. Unless you have a need to have separate instances of a printer with different properties, keep it static.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This syntax is used to create an anonymous type in… May 12, 2026 at 12:42 am
  • Editorial Team
    Editorial Team added an answer what you have will work as long as table1 has… May 12, 2026 at 12:42 am
  • Editorial Team
    Editorial Team added an answer Try this: <asp:TemplateField> <ItemTemplate> <asp:TextBox ID="textDateSent" runat="server"> </asp:TextBox> <input type="button"… May 12, 2026 at 12:42 am

Related Questions

forgive my ignorance, I thought it was going to be a no-brainer but I
I've created a table in Microsoft Sql CE that I'm using to hold some
I recently took a class at school where we had to learn Scheme to
I have the following code making a GET request on a URL: $('#searchButton').click(function() {

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.