Click here to Skip to main content
15,892,643 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: OO Software design epiphany - it might not matter Pin
Nelek13-Jan-21 8:23
protectorNelek13-Jan-21 8:23 
GeneralRe: OO Software design epiphany - it might not matter Pin
OriginalGriff13-Jan-21 4:20
mveOriginalGriff13-Jan-21 4:20 
GeneralRe: OO Software design epiphany - it might not matter Pin
den2k8813-Jan-21 4:26
professionalden2k8813-Jan-21 4:26 
GeneralRe: OO Software design epiphany - it might not matter Pin
charlieg13-Jan-21 4:38
charlieg13-Jan-21 4:38 
GeneralRe: OO Software design epiphany - it might not matter Pin
den2k8813-Jan-21 5:23
professionalden2k8813-Jan-21 5:23 
GeneralRe: OO Software design epiphany - it might not matter Pin
PhilipOakley14-Jan-21 1:55
professionalPhilipOakley14-Jan-21 1:55 
GeneralRe: OO Software design epiphany - it might not matter Pin
charlieg13-Jan-21 4:33
charlieg13-Jan-21 4:33 
GeneralRe: OO Software design epiphany - it might not matter Pin
raddevus13-Jan-21 5:02
mvaraddevus13-Jan-21 5:02 
My theory is that there are very few examples around that bring OOP all together.

OOPs main purpose should be reuse and making the code that you write smaller (less code to deal with when adding enhancements or fixing bugs).

I believe that with one very focused example you would see all of PIE-A (Polymorphism, Inheritance, Encapsulation and Abstraction) come together.

But most of the time you don't need this type of architecture until things get large. And, most projects don't get large -- especially samples you see.

Here's My Attempt

This should be an article but I'll do that later.

The Entire Premise
Imagine you want to save data to three different data stores:
1) file
2) database
3) web location

Save instantly becomes our main verb(functionality).

Requirements: We Want Four Things
1. Any dev must be able to include the Save() functionality on their class in the future. (interface)
2. Any dev must be able to call the Save() functionality on any class in the future and easily know that it is named Save() -- this is self-documenting code
3. There must be an easy way for dev to configure where the data will be stored (file, db, url)
4. A dev must be able to create a list of various types (classes in the architecture) and iterate through them, calling Save() and knowing that they will save to their appropriate destination. This is Polymorphism -- all objects implement the Interface which provides Save().

Here is the smallest sample I can come up with and it really works.
Get LINQPad - The .NET Programmer's Playground[^] and run the code below.

You will see the following output:
I'm saving into a FILE : super.txt
I'm saving into a FILE : extra.txt
I'm saving into a DATABASE : connection=superdb;integrated security=true
I'm saving into a WEB LOCATION : http://test.com/saveData?
I'm saving into a FILE : super.txt
I'm saving into a FILE : extra.txt
I'm saving into a DATABASE : connection=superdb;integrated security=true
I'm saving into a WEB LOCATION : http://test.com/saveData?


Now a dev can
1. create an IPersistable object.
2. pass in an IConfigurable object (which determines which data store the Save() will write to
3. call Save() on the object

Dev Only Needs To Know Two Things: Abstraction
1. create a configurable object -- select which data store
2. call Save()

C#
void Main()
{
    List<IPersistable<IConfigurable>> allItems = new List<IPersistable<IConfigurable>>();
	FileConfig fc = new FileConfig("super.txt");
	IPersistable<IConfigurable> item = new FileSaver(fc);
    
	List<IPersistable<IConfigurable>> fakeData = new List<IPersistable<IConfigurable>>();
	fakeData.Add(new FileSaver(new FileConfig("extra.txt")));
	fakeData.Add(new DatabaseSaver(new DatabaseConfig("connection=superdb;integrated security=true")));
	fakeData.Add(new TcpSaver(new TcpConfig("http://test.com/saveData?")));
	
	allItems.Add(item);
    foreach (IPersistable<IConfigurable> ic in fakeData){
        allItems.Add(ic);            
    } 

    foreach (IPersistable<IConfigurable> ip in allItems)
    {
        ip.Save();
    }
	
	foreach (var ip in allItems)
    {
        ip.Save();
    }
}

interface IPersistable<T> where T : IConfigurable {
    bool Save();
}

interface IConfigurable {
}

class FileSaver : IPersistable<IConfigurable>{
	
    protected FileConfig config;
    public FileSaver(FileConfig config){
        this.config = config;
    }
    virtual public bool Save(){
        Console.WriteLine(String.Format("I'm saving into a FILE : {0}",config.FileName));
        return true;
    }
}

class FileConfig : IConfigurable{

    public string FileName{get;set;}
    public FileConfig(String fileName=null){
        FileName = fileName;
    }

}

class DatabaseConfig : IConfigurable{
    public string ConnectionString{get;set;}
    public DatabaseConfig(String ConnectionString=null){
        this.ConnectionString = ConnectionString;
    }
}

class TcpConfig : IConfigurable{
    public string Uri {get;set;}
	public TcpConfig(String uri){
		Uri = uri;
	}
}

class DatabaseSaver : IPersistable<IConfigurable>{
    protected DatabaseConfig config;
    public DatabaseSaver(DatabaseConfig config){
        this.config = config;
    }
    public bool Save(){
        Console.WriteLine(String.Format("I'm saving into a DATABASE : {0}", config.ConnectionString));
        return true;
    }
}

class TcpSaver : IPersistable<IConfigurable>{
    TcpConfig config;
    public TcpSaver(TcpConfig config){
        this.config = config;
    }
    public bool Save(){
        Console.WriteLine(String.Format("I'm saving into a WEB LOCATION : {0}", config.Uri));
        return true;
    }
}

GeneralRe: OO Software design epiphany - it might not matter Pin
Greg Utas13-Jan-21 5:07
professionalGreg Utas13-Jan-21 5:07 
GeneralRe: OO Software design epiphany - it might not matter Pin
den2k8813-Jan-21 5:29
professionalden2k8813-Jan-21 5:29 
GeneralRe: OO Software design epiphany - it might not matter Pin
Marc Clifton13-Jan-21 6:11
mvaMarc Clifton13-Jan-21 6:11 
GeneralRe: OO Software design epiphany - it might not matter Pin
PhilipOakley14-Jan-21 1:58
professionalPhilipOakley14-Jan-21 1:58 
GeneralRe: OO Software design epiphany - it might not matter Pin
Member 1330167913-Jan-21 20:17
Member 1330167913-Jan-21 20:17 
GeneralRe: OO Software design epiphany - it might not matter Pin
NelsonGoncalves13-Jan-21 20:55
NelsonGoncalves13-Jan-21 20:55 
GeneralRe: OO Software design epiphany - it might not matter Pin
giulicard13-Jan-21 21:34
giulicard13-Jan-21 21:34 
GeneralRe: OO Software design epiphany - it might not matter Pin
NelsonGoncalves13-Jan-21 21:59
NelsonGoncalves13-Jan-21 21:59 
GeneralRe: OO Software design epiphany - it might not matter Pin
_WinBase_13-Jan-21 23:00
_WinBase_13-Jan-21 23:00 
GeneralRe: OO Software design epiphany - it might not matter Pin
Ed Korsberg14-Jan-21 1:29
Ed Korsberg14-Jan-21 1:29 
GeneralRe: OO Software design epiphany - it might not matter Pin
PhilipOakley14-Jan-21 2:03
professionalPhilipOakley14-Jan-21 2:03 
GeneralRe: OO Software design epiphany - it might not matter Pin
Ed Korsberg14-Jan-21 3:35
Ed Korsberg14-Jan-21 3:35 
GeneralRe: OO Software design epiphany - it might not matter Pin
PhilipOakley14-Jan-21 5:08
professionalPhilipOakley14-Jan-21 5:08 
GeneralRe: OO Software design epiphany - it might not matter Pin
DumpsterJuice14-Jan-21 1:43
DumpsterJuice14-Jan-21 1:43 
GeneralRe: OO Software design epiphany - it might not matter Pin
Lurk14-Jan-21 2:16
Lurk14-Jan-21 2:16 
GeneralRe: OO Software design epiphany - it might not matter Pin
Matt Bond14-Jan-21 2:51
Matt Bond14-Jan-21 2:51 
GeneralRe: OO Software design epiphany - it might not matter Pin
davercadman14-Jan-21 3:00
davercadman14-Jan-21 3:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.