SOLIDNot Just a State of Matter, It’s Principles for OO Propriety
Chris Weldon
Me
• Fightin’ Texas Aggie
• .Net and PHP Developer
• UNIX and Windows Sysadmin
• Senior Consultant at Improving Enterprises
• Contact Me: [email protected]
Agile, Microsoft, Open Technologies, UX
Applied Training, Coaching, Mentoring
Certified Consulting
Rural Sourcing
Recruiting Services
What is OOD?
What is OOD?
Abstraction
What is OOD?
InheritanceAbstraction
What is OOD?Encapsulation
InheritanceAbstraction
What is OOD?Encapsulation Polymorphism
InheritanceAbstraction
What is SOLID?
?
Is it law?
They be more like“guidelines”
An Order
public class Order{ private int _id; private int _userId; private Product[] _products;
public int getId() { return this._id; }
public void setId(int id) { this._id = id; }
public int getUserId() { return this._userId; }
public void setUserId(int userId) { this._userId = userId; }
public Product[] getProducts() { return this._products; }
public void addProduct(Product product) { this._products.add(product); }
public void setProducts(Product[] products) { this._products = products; }
private DBConnection _db;
public Order() { // Read from configuration to get connection string. string connectionString = string.Empty; Handle fileHandle = fopen("/etc/database/app.config", "r"); while (string row = readLine(fileHandle)) { if (row.StartsWith("dbo")) { connectionString = row; } } fclose(fileHandle); this._db = new DBConnection(connectionString); }
public static Order CreateOrder(int userId, Product[] products) { Order order = new Order(); order.setUserId(userId); order.setProducts(products); return order; }
public static Order getOrderById(int orderId) { if (orderId == null || orderId < 0) { DBCommand command = this._db.exec("SELECT * FROM orders"); Order[] results = this._db.getResults(command);
// Get the first result. return results[0]; }
DBCommand command = this._db.exec("SELECT * FROM orders WHERE id = ?", orderId); Order order = this._db.getSingle($command); return order; }
public void deliverOrder() { try { WebServiceConnection networkConnection =
new WebServiceConnection("http://shpt1/shipctrl.svc"); networkConnection->open();
ShippingManager shippingManager = new ShippingManager(networkConnection);
shippingManager.setOrder(this); shippingManager.setDeliveryType(DeliveryType.EXPRESS); shippingManager.shipViaWebService();
this.notifyCustomer(); } catch (Exception e) { Handle logFile = fopen("/tmp/logfile.log", "a"); fwrite("An error occurred while delivering the order.", logFile); fclose(logFile); } }
public void notifyCustomer() { try { mailer = new Mailer(); mailer.setFrom(“[email protected]”, “Grumpy Baby Orders”); mailer.setSubject(“Order #” + this.getId() + “ out for Delivery”); mailer.setBodyText(“Your order is being shipped!”); mailer.send(); } catch (\Exception $e) { Handle logFile = fopen("/tmp/logfile.log", "a"); fwrite("An error occurred while emailing the notice.", logFile); fclose(logFile); } }}
OMG
OMGWho vomited in my codebase?
“There should never be more than one reasonfor a class to change”
Improve
Improve
public class Order{ // I’m just a model}
Improve
public class Order{ // I’m just a model}
public class OrderDao{ // I just talk to the database public OrderDao(); public static Order CreateOrder(int userId, Product[] products); public static Order getOrderById(int orderId);}
Improve
public class Order{ // I’m just a model}
public class OrderDao{ // I just talk to the database public OrderDao(); public static Order CreateOrder(int userId, Product[] products); public static Order getOrderById(int orderId);}
public class OrderDeliverer{ // I just deliver orders public void deliverOrder();}
Improve
public class Order{ // I’m just a model}
public class OrderDao{ // I just talk to the database public OrderDao(); public static Order CreateOrder(int userId, Product[] products); public static Order getOrderById(int orderId);}
public class OrderDeliverer{ // I just deliver orders public void deliverOrder();}
public class CustomerNotifier{ // I just deliver orders public void notifyCustomer();}
Improve
public class Order{ // I’m just a model}
public class OrderDao{ // I just talk to the database public OrderDao(); public static Order CreateOrder(int userId, Product[] products); public static Order getOrderById(int orderId);}
public class OrderDeliverer{ // I just deliver orders public void deliverOrder();}
public class CustomerNotifier{ // I just deliver orders public void notifyCustomer();}
it’s what we do
Next Sample
public void ActivateDrillBit(string customerOption) { if (customerOption == "small") { SmallBit drillBit = new SmallBit(); drillBit.activate(); } else if (customerOption == "medium") { MediumBit drillBit = new MediumBit(); drillBit.activate("120hz"); } else if (customerOption == "large") { LargeBit drillBit = new LargeBit(); drillBit.activate("240hz", Options.Water); }}
Requirements Change!Customer Needs to Specify Options
public void ActivateDrillBit(string customerOption, string freq, Options options) { if (freq == "") freq = "240hz"; if (options == null) options = Options.NoWater; if (customerOption == "small") { SmallBit drillBit = new SmallBit(); drillBit.activate(freq, options); } else if (customerOption == "medium") { MediumBit drillBit = new MediumBit(); drillBit.activate(freq, options); } else if (customerOption == "large") { LargeBit drillBit = new LargeBit(); drillBit.activate(freq, options); }}
YouBroke
MyApp
!@#!@
public class DrillBitActivator { public void ActivateDrillBit(string customerOption) { // ... }}
public class DrillBitConfigurableActivator : DrillBitActivator { public DrillBitConfigurableActivator(string freqency, Options options) { // Configurable! }
public override function ActivateDrillBit(string customerOption) { // ... }}
public function ActivateDrillBit(string customerOption) IDrillBit drillBit = DrillBitFactory::CreateDrillBit(customerOption); drillBit.activate(this._freq, this._options);}
Next:A Familiar Example
public interface IManageCustomers { void TakeSpecifications(Specs specs); void ReviewSpecifications(Specs specs); void GiveToEngineers(Specs specs); void DealWithTheGoshDarnCustomers(); bool IsPeoplePerson();}
public class GoodManager : IManageCustomers { public void TakeSpecifications(Specs specs) { this.ReviewSpecifications(specs); }
public void ReviewSpecifications(Specs specs) { // If specs seem good. this.GiveToEngineers(specs); }
public void GiveToEngineers(Specs specs) { // E-mails specs to engineers. }
public void DealWithTheGoshDarnCustomers() { // You better believe he does. }
public bool IsPeoplePerson() { return true; // Absolutely! }}
public class Tom : IManageCustomers { public void TakeSpecifications(Specs specs) { throw new DelegateToSecretary(); }
public void ReviewSpecifications(Specs specs) { throw new DelegateToSecretary(); }
public void GiveToEngineers(Specs specs) { throw new DelegateToSecretary(); }
public void DealWithTheGoshDarnCustomers() { // Your gosh darn right I do! }
public bool IsPeoplePerson() { return true; // I AM a people person, dammit! }}
public interface ITakeSpecifications { function TakeSpecifications(Specs specs);}
public interface IReviewSpecifications { function ReviewSpecifications(Specs specs);}
public interface IGiveToEngineers { function GiveToEngineers(Specs specs);}
public interface IManageCustomers { function DealWithTheGoshDarnCustomers(); function IsPeoplePerson();}
public class GoodManager : IManageCustomers, ITakeSpecifications, IReviewSpecifications, IGiveToEngineers { public void TakeSpecifications(Specs specs) { this.ReviewSpecifications(specs); }
public void ReviewSpecifications(Specs specs) { // If specs seem good. this.GiveToEngineers(specs); }
public void GiveToEngineers(Specs $specs) { // E-mails specs to engineers. }
public void DealWithTheGoshDarnCustomers() { // You better believe he does. }
public bool IsPeoplePerson() { return true; // Absolutely! }}
public class Tom : IManageCustomers { public void DealWithTheGoshDarnCustomers() { // Your gosh darn right I do! }
public bool IsPeoplePerson() { return true; // I AM a people person, dammit! }}
Hand Tom off to our consultants
Next Up
public interface IDataResource{ void Load(); void Save();}
public class AppSettings : IDataResource { public void Load() { // Load application settings. }
public void Save() { // Save application settings. }}
public class UserSettings : IDataResource { public void Load() { // Load user settings. }
public void Save() { // Save user settings. }}
static IDataResource[] LoadAll() { IDataResource[] resources = new IDataResource[2] { new AppSettings(), new UserSettings() };
foreach (IDataResource resource in resources) { resource.Load(); }
return resources;}
static void SaveAll(IDataResource[] resources) { foreach (IDataResource resource in resources) { resource.Save(); }}
Our “Duck”
public class UnsaveableSettings : AppSettings { public override void Load() { // Loads settings. }
public override void Save() { throw new CannotSaveException(); }}
static IDataResource[] LoadAll() { IDataResource[] resources = new IDataResource[3] { new AppSettings(), new UserSettings(), new UnsaveableSettings() };
foreach (IDataResource resource in resources) { resource.Load(); }
return resources;}
static void SaveAll(IDataResource[] resources) { foreach (IDataResource resource in resources) { resource.Save(); }}
static IDataResource[] LoadAll() { IDataResource[] resources = new IDataResource[3] { new AppSettings(), new UserSettings(), new UnsaveableSettings() };
foreach (IDataResource resource in resources) { resource.Load(); }
return resources;}
static void SaveAll(IDataResource[] resources) { foreach (IDataResource resource in resources) { resource.Save(); }}
static IDataResource[] LoadAll() { IDataResource[] resources = new IDataResource[3] { new AppSettings(), new UserSettings(), new UnsaveableSettings() };
foreach (IDataResource resource in resources) { resource.Load(); }
return resources;}
static void SaveAll(IDataResource[] resources) { foreach (IDataResource resource in resources) { resource.Save(); }}
What happens with UnsaveableSettings?
static void SaveAll(IDataResource[] resources) { foreach (IDataResource resource in resources) { if (resource is UnsaveableSettings) continue;
resource.Save(); }}
teh fix!
static void SaveAll(IDataResource[] resources) { foreach (IDataResource resource in resources) { if (resource is UnsaveableSettings) continue;
resource.Save(); }}
teh fix!
static void SaveAll(IDataResource[] resources) { foreach (IDataResource resource in resources) { if (resource is UnsaveableSettings) continue;
resource.Save(); }}
teh fix!
omg shoot megood job reducing abstraction
The Real Fix
The Real FixInterface Segregation +
Polymorphism
public interface IDataResource{ void Load();}
public interface ISaveResource{ void Save();}
public class AppSettingsLoaderBase : IDataResource { public virtual void Load() { // Load application settings. }}
public class AppSettings extends AppSettingsLoaderBase implements ISaveResource { public function Save() { // Save application settings. }}
public class UnsaveableSettings extends AppSettingsLoaderBase { public override void Load() { // Loads settings. }}
static IDataResource[] LoadAll() { IDataResource[] resources = new IDataResource[3] { new AppSettings(), new UserSettings(), new UnsaveableSettings() };
foreach (IDataResource resource in resources) { resource.Load(); }
return resources;}
static void SaveAll(ISaveResource[] resources) { foreach (ISaveResource resource in resources) { resource.Save(); }}
Final Problem
public class Authenticator { private DataAccessLayer _repository;
public DataAccessLayer() { this._repository = new DataAccessLayer(); }
public bool authenticate(string username, string password) { string hashedPassword = md5(password); User user = this._repository.findByUsernameAndPassword( username, hashedPassword); return user == null; }}
Problems?
Problems?
authenticate() : boolAuthenticator
findByUsernameAndPassword : arrayDataAccessLayer
Problems?
authenticate() : boolAuthenticator
findByUsernameAndPassword : arrayDataAccessLayer
Strongly coupled to DataAccessLayer
Dependency Inversion
• “High-level modules should not depend upon low level modules. They should depend upon abstractions.
• “Abstractions should not depend upon details. Details should depend upon abstractions.”
Robert Martin
Step 1Invert Dependency
class Authenticator { private DataAccessLayer _repository;
public Authenticator(DataAccessLayer repository) { this._repository = repository; }
public bool authenticate(string username, string password) { string hashedPassword = md5(password); User user = this._repository.findByUsernameAndPassword( username, hashedPassword); return user == null; }}
Coupling = Bad
Step 2Depend on Abstractions
public interface IUserRepository { User findByUsernameAndPassword(string username, string password);}
public class DataAccessLayer : IUserRepository { public User findByUsernameAndPassword(string username, string password) { // Do some database stuff. }}
class Authenticator { private IUserRepository _repository;
public Authenticator(IUserRepository repository) { this._repository = repository; }
public bool authenticate(string username, string password) { string hashedPassword = md5(password); User user = this._repository.findByUsernameAndPassword( username, hashedPassword); return user == null; }}
Comparison
Comparison
authenticate() : boolAuthenticator
findByUsernameAndPassword : arrayDataAccessLayer
Comparison
authenticate() : boolAuthenticator
findByUsernameAndPassword : arrayDataAccessLayer
Comparison
authenticate() : boolAuthenticator
findByUsernameAndPassword : arrayDataAccessLayer
authenticate() : boolAuthenticator
findByUsernameAndPassword : arrayIUserRepository
findByUsernameAndPassword : arrayDataAccessLayer
Benefit: Flexibility
public class WebServiceUserRepository : IUserRepository { public User findByUsernameAndPassword(string username, string password) { // Fetch our user through JSON or SOAP }}
public class OAuthRepository : IUserRepository { public User findByUsernameAndPassword(string username, string password) { // Connect to your favorite OAuth provider }}
Recap
S = SRP - Single Responsibility Principle
S = SRP - Single Responsibility Principle
O = OCP - Open/Closed Principle
S = SRP - Single Responsibility Principle
O = OCP - Open/Closed Principle
L = LSP - Liskov Substitution Principle
S = SRP - Single Responsibility Principle
O = OCP - Open/Closed Principle
L = LSP - Liskov Substitution Principle
I = ISP - Interface Segregation Principle
S = SRP - Single Responsibility Principle
O = OCP - Open/Closed Principle
L = LSP - Liskov Substitution Principle
I = ISP - Interface Segregation Principle
D = DIP - Dependency Inversion Principle
Questions?
Thank You!
http://spkr8.com/neraathCheck improvingaggies.com