Design Patterns: Elements of Reusable Object-Oriented Software
About This Book
Capturing a wealth of experience about the design of object-oriented software, four top-notch designers present a catalog of simple and succinct solutions to commonly occurring design problems. Previously undocumented, these 23 patterns allow designers to create more flexible, elegant, and ultimately reusable designs without having to rediscover the design solutions themselves.
The authors begin by describing what patterns are and how they can help you design object-oriented software. They then go on to systematically name, explain, evaluate, and catalog recurring designs in object-oriented systems. With Design Patterns as your guide, you will learn how these important patterns fit into the software development process, and how you can leverage them to solve your own design problems most efficiently.
Each pattern describes the circumstances in which it is applicable, when it can be applied in view of other design constraints, and the consequences and trade-offs of using the pattern within a larger design. All patterns are compiled from real systems and are based on real-world examples. Each pattern also includes code that demonstrates how it may be implemented in object-oriented programming languages like C++ or Smalltalk.
[1] https://www.amazon.com/Design-Patterns-Elements-Reusable-Obj...
Fair warning though, it's a fairly hard book to read. For a lighter, more fun intro to design patterns in particular, I always recommend Game Programming Patterns [2]
[1] https://www.amazon.com/Design-Patterns-Elements-Reusable-Obj...
[2] https://gameprogrammingpatterns.com/contents.html
https://www.amazon.com/Design-Patterns-Elements-Reusable-Obj...
In my opinion design patterns are not a fundamental concept, but rather provide common syntax for collaboration purposes for various patterns that are parts of language and more or less invisible in e.g. Scheme or F#. But if one is diving into C++ it's probably convenient to be familiar with these concepts.
Well [1] that book you're referring to, has changed my perspective on abstractions and how to communicate them to other people. There's probably no "Holy Grail" of abstraction guides better than that IMHO.
Designing and implementing useful and maintainable abstractions comes from experience in both consuming and designing abstractions for API's. Having empathy for both the consumers and future developer's of the API gives you a good perspective on how to structure the abstraction.
The best abstractions are from people who have used bad abstractions, and are usually incrementally developed over time...
[1] https://www.amazon.com.au/Design-Patterns-Elements-Reusable-...
If you want something challenging, dive into Design Patterns and see if you can recreate the examples in the book. I also recommend Code which covers the history of hardware and programming. It was the book that helped me understand how programming abstracts the physical components of the computer.
Reading is good, but you're already in school to study and learn stuff. I think your time would be much better spent on building apps / websites in your time off.
Personal opinion: for language-specific books, the hands-on ones are the only ones worth reading.
If you want "bedtime reading", I'd suggest books that are broader than learning a specific language. Books that discuss algorithms, or design patterns...stuff like that.
Speaking of design pattern books...https://www.amazon.com/dp/0201633612/ Not sure how much mileage you'll get out of that, but it's considered one of the best design patterns books that I've ever heard of.
My blog about software architecture: http://www.tutisani.com/software-architecture/ (may not be for very beginners but I hope that it's able to communicate important topics).
I'd also suggest reading the classic book about design patterns (a.k.a. Gang of Four): https://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/ref=sr_1_3?crid=1XRJO0L09LHLY&keywords=design+patterns+elements+of+reusable+object+oriented+software&qid=1557502967&s=gateway&sprefix=design+patterns%2Caps%2C162&sr=8-3
There are several good thought leaders in this direction, specifically Martin Fowler (https://martinfowler.com/) and Eric Evans (he does not write much online, but his book is great - all about modeling properly): https://www.amazon.com/Domain-Driven-Design-Tackling-Complexity-Software/dp/0321125215
​
I'm big on modeling, objects, etc. so reply back if any questions.
I would recommend reading Design Patterns: Elements of Reusable Object-Oriented Software. That book will give you the majority of design knowledge you would gain at this point in your career from college.
I'm surprised Design Patterns: Elements of Reusable Object-Oriented Software isn't on the list. Old (1994), but gold.
Design Patterns (aka Gang of Four)
https://www.amazon.co.uk/Design-patterns-elements-reusable-object-oriented/dp/0201633612
ShapeManager cannot create a shape if not knowing what this shape is (Square, Circle or something else). And it really doesn't know because you say the method createShare has no parameters. Either you misunderstood the question or the lecturer didn't explain it well. You should ask him/her for clarifications. If you look at the libraries of Java or any other OO language, I am pretty sure you won't find such scenario and implementation pattern as the one you gave in your example.
@croraf
You should find some other reading I think e.g. the classic book http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612. The main idea of a factory is that it returns something whose type the caller doesn't know, and doesn't care about. For example, if you have a method createSocket() in some SocketFactory, this method is usually defined to return an interface or an abstract class Socket. But actually it returns new SocketImpl1() and new SocketImpl2() which are concrete classes. What the factory returns may depend on many things - a system property, the underlying OS, anything you can think of. The main idea is that the factory centralizes the creation of Socket objects at one single place. This way, if you need to make a change, you can make it just in the factory. I think this book also has some decent Java counterparts too, you may look around. Other free good sources are referenced here.
Real world examples of Factory Method pattern
Yes of course you can implement that
I'll draw a small architecture then ill explain it to u:
first of all , you can learn about Mappers here and TDGs here. A mapper has a method called cacheAll() which calls and delegate to TDG's method cacheAll() which in its turn has a mission to get all rows from a table from the db( the rows you want to cache in the cache object).
so basically first you have to create a listener implementing "ServletContextListener" which means its a listener for the whole servlet context, and inside its contextInitialized you have to call mp.fill(Mapper.cacheAll()), so it is sthg like ( this is general code, of course write it better and optimize it)
Don't forget to add your listener in web.xml:
so what this will do , is on startup of the server, will cache all record into a hashmap mp in a cache object.
As for updating cache based on database change, you will have to use observer pattern.
UPDATE
I forgot to mention, about the cache object, i assume you want it accessible for all users or your app, so you should code it as a singleton (singleton pattern), code like that:
Also if the data that you want to cache can be changed by users, so make it a Threadlocal singleton.
Not necessarily video game related, but the best walk through I've seen for doing multi-platform software was in GOF (http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612). Read the case study on the windowing system.
I would advise that you read the book 'Design Patterns' by Erich Gamma et al. (Amazon link: http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612)
Design Patterns is also very well known https://www.amazon.com/Design-Patterns-Elements-Reusable-Obj...
- Clean Code (by "Uncle Bob")) [https://www.amazon.com/Clean-Code-Handbook-Software-Craftsma...]
- Design Patterns (by "Gang of 4") [https://www.amazon.com/Design-Patterns-Elements-Reusable-Obj...]
- Introduction to Algorithms (by "CLRS") [https://www.amazon.com/Introduction-Algorithms-3rd-MIT-Press...]
I believe the famous recommendation of "favor composition over inheritance" was coined in the GoF Design Patterns book.
It says (p.20):
Notice that this statement refers to class inheritance, and must be distinguished from interface inheritance which is fine.
Dynamism
Both are ways to achieve reusability, but the advantage of composition over inheritance is dynamism. Since the composition can be changed dynamically at runtime this represents a great advantage, whereas inheritance is statically defined at compile time.
Encapsulation
Also, composition is based on using the public interfaces of the composed objects, therefore objects respect each other's public interfaces and therefore this fosters encapsulation. On the other hand, inheritance breaks encapsulation since child components typically consume a protected interface from the parent. It is a well known problem that changes in the parent class can break the child classes, the famous base class problem. Also in inheritance parent classes define the physical representation of subclasses, therefore child clases depend on parent classes to evolve.
Cohesion
Another advantage of composition is that it keeps classes focused on one task and this foster cohesion as well.
Liabilities
Evidently a problem with composition is that you will have more objects and fewer classes. That makes a little more difficult to visualize your design and how it achieves its goals. When debugging code it is harder to know what is going on unless you know what exact instance of a given composite is currently being used by an object. So composition makes designs a bit harder to understand in my opinion.
Since the advantages of composition are multiple that's why it is suggested to favor it over inheritance, but that does not mean inheritance is always bad. You can achieve a great deal when inheritance is properly used.
Interesting References
I would suggest a study of GoF Design Patterns to see good examples of both types of reusability, for instance a Strategy Pattern that uses composition vs a Template Method that uses inheritance.
Most of the patterns make a great use of interface inheritance and then object composition to achieve their goals and only a few use class inheritance as a reusability mechanism.
If you want to delve more the book Holub on Patterns, on chapter 2 has a section called Why
extends
is Evil that delve much more on the liabilities of class inheritance.The book mentions three specific aspects
Good opening slides for any education course in my opinion are:
For design patterns I could expect several visual tools or "job aids".
I would follow a structure similar to the Elements of Reusable Object-Oriented Software book:
As already mentioned, design patterns are really ideas, so when teaching you must convey the idea. If they understand the problem, solution and consequences of the design pattern, then they will be far better off than trying to force patterns into the code (and that will become a nightmare). Recognition of where and what patterns (if any) can be applied is the real goal. The Huston examples are really good for putting out an example of code to the class and seeing if they can identify a pattern to improve it. Hope this helps.
Head First Design Patterns is an excellent reference as well.
I think the first thing that is needed here is a good / appropriate data structure to keep / manipulate the table data. That depends on specific requirements you can have, such as size of the table, performance requirements, etc.
Let's assume you will use some
Matrix
class which provides low-level operations over table (set cell value, add/remove row, transpose, etc).This class will operate with basic data structures, for example, it may have
get_row
method which will return alist
of numbers. Now we can, for example, get the summary of values in this list, but we can't just change some list item and have this change reflected in the parentMatrix
(the row data is disconnected from the parent matrix structure).Now we can build our structure upon this
Matrix
class, our two targets are:1) make it more convenient for the user, some of the "conveniences" can be:
Table
object (if we modify the row item, it will be reflected in the parent table and other row / column / cell obejcts).2) hide the actual data structure we use to store the table data
Matrix
implementation independently without breaking user codeThis is the approximate classes structure I would start with (python-like pseudo code):
The
Matrix
class is a low level data structure and it should not be a part of a public interface.The public interface is represented by
Table
class and other related classes below:To keep
Table
andRow /
Column /
Cell
objects in-sync we can use the Observer pattern.Here the
Table
is aSubject
andRow
/Column
/Cell
areObservers
. Once the state of theTable
(and underlying data) is changed, we can update all dependent objects.The
Column
andCell
classes are similar, theColumn
will hold the column data and theCell
will wrap the cell value. The user-level usage can look like be this:Using this approach you can quite easily implement such operations as copy, cut, paste. Also you can apply Command pattern here and extract these operations into small classes. This way it will also be quite easy to implement undo and redo.
The
PivotTable
table can also be a special kind ofObservable
. Depending on the requirements to the features for the pivot table, you may find Builder pattern useful to configure the pivot table. Something like this:The classes to export table to different formats probably don't have to be observable, so they'll just wrap the
Table
object and transform it to the appropriate format:Of course, the above is just one of many possible implementation options, treat them as some ideas that can be useful (or not useful) depending on specific requirements you have.
You may find more ideas in the GoF patterns book.