- bad attempt at Square is-a Rectangle. equivalence r.getWidth() = (r.setHeight(4).getWidth()) should hold but would fail for the above Rectangle r = s // imagine this was a function call passing a Square to function asking for a Rectangle Original smelly code, any changes to policy on when customer can check out a book or when a book is available requires change to checkoutBook method:ĬheckoutBook: function (customer, book) Wikipedias Definition: Every module, class, or function should have responsibility for a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class. We will cover the JavaScript library checkout example in this blog post. Martins Definition: There should never be more than one reason for a class to change. If you push code deeper into classes and behind encapsulation boundaries the change is isolated, code is more maintainable.One way that ugly code arises is new code has to be patched in as new features are added.This basic O-O principle you may not know as well, here is a brief overview. For example, consider an application that takes a collection of shapescircles, and squaresand calculates the sum of the area of all the shapes in the collection. also related to SRP below, SRP is "one concern per class"Īka encapsulate code that changes a lot. Single-responsibility Principle (SRP) states: A class should have one and only one reason to change, meaning that a class should have only one job. Separation of Concerns (SoC) - don't have many different concerns in one class instead, different tasks/aspects should be in different classes/functions.We will cover a similar principle below, the Single Responsibility Principle (SRP). Similarly at the lower level of methods: the name should be (all that) it does.Single, clearly stated purpose which fits its name and all of its fields and Make classes cohesive: class should have a.If design is proving to be inflexible, refactor it to restore it to be a good design (Refactoring is a lecture topic on its own later).For example, in Assigment 1 you should have been coding to the RESTful interface specification, not to the front-end code. Code to interfaces, not implementations.Well-designed software is easier to debug, change and extend.Here are some basic design principles you probably have already heard about: at the recommended book Head-First Object-Oriented Analysis and Design (HFOOA&D below). We assume some knowledge of Object-Oriented Design if you have not heard of any of this look e.g.They must be learned by specific coding examples/experiences.Yes, these principles are admittedly fuzzy and not mutually exclusive According to the Single Responsibility Principle (SRP), components should have only one reason to change.Every great programmer has a toolbox of design principles they use to help them produce great code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |