Head First Design Patterns: A Brain-Friendly Guide
All
Stack Overflow 99
This Year
Stack Overflow 4
This Month
Stack Overflow 1
[https://toptalkedbooks.com/amzn/0596007124)
https://toptalkedbooks.com/amzn/0201633612
https://toptalkedbooks.com/amzn/0596007124
https://toptalkedbooks.com/amzn/0735619670
And you could check stack overflow for question on general programming books. I would always go for a general concept
functional programming
overhow to functional programming in haskell
.But I'll be perfectly honest, I'm a victim of the curse of knowledge so I honestly don't know how one should start. What I do remember is that I had huge problems with syntax (how do I tell language X to do a
for (Employee e in employeeList)
, how do you write a switch and stuff, why would I ever need a ternary operator, and like that.But once you master the basics of how to write x, y and z in your preferred language, you absolutely need to understand design patterns, you absolutely need to understand how code is supposed to be written, you absolutely need to understand data structures, you absolutely need to understand inheritance and polymorphism, you absolutely need to understand lambdas and functional programming.
Then you can get to the more "neat" stuff like the benefits of having immutables, and "job specific stuff" like how to solve race conditions in threading; sockets and web communication, etc.
Sure :)
EDIT: Added links to Amazon just in case anyone wants to see reviews.
[$1 Or More Tier]
Head First Ruby
Head First C
Head First Object-Oriented Analysis and Design
Head First SQL
Head First Statistics
[$8 Or More Tier]
Head First Javascript Programming
Head First PMP
Head First HMTL and CSS
Head First C#
Head First Agile
[$15 Or More Tier]
Head First Design Patterns
Head First Java
Head First Python
Head First Learn to Code
Head First Android Development
I enjoyed reading the headfirst patterns book as a refresher
[1] https://sophia.javeriana.edu.co/~cbustaca/docencia/POO-2016-... [2] https://www.amazon.com/Head-First-Design-Patterns-Brain-Frie...
Hope this helps. It describes the various types of factories. I used Head First Design Patterns as my reference. I used yuml.me to diagram.
Static Factory
Is a class with a Static Method to product various sub types of Product.
Simple Factory
Is a class that can produce various sub types of Product. (It is better than the Static Factory. When new types are added the base Product class does not need to be changed only the Simple Factory Class)
Factory Method
Contains one method to produce one type of product related to its type. (It is better than a Simple Factory because the type is deferred to a sub-class.)
Abstract Factory
Produces a Family of Types that are related. It is noticeably different than a Factory Method as it has more than one method of types it produces. (This is complicated refer to next diagram for better real-life example).
Example From The .NET Framework
DbFactoriesProvider is a Simple Factory as it has no sub-types. The DbFactoryProvider is an abstract factory as it can create various related database objects such as connection and command objects.
I've taken a class in college that spent two weeks around design patters, and read the Gang of Four book to no avail. Understanding what each pattern served for and how to use them to fit my problems was very hard for me, a developer that didn't have much experience in OO programming.
The book that really made it click for me was Head First Design Patterns. It starts by showing a problem, different approaches the developers considered, and then how they ended up using a design pattern in order to fix it. It uses a very simple language and keeps the book very engaging.
Design patterns end up being a way to describe a solution, but you don't have to adapt your classes to the solution. Think of them more as a guide that suggest a good solution to a wide array of problems.
Let's talk about SOLID:
A book I'd highly recommend to you for MVC in swing would be "Head First Design Patterns" by Freeman and Freeman. They have a highly comprehensive explanation of MVC.
Um, in case you're interested, you could download a fairly entertaining song about the MVC pattern from here!
One issue you may face with Swing programming involves amalgamating the SwingWorker and EventDispatch thread with the MVC pattern. Depending on your program, your view or controller might have to extend the SwingWorker and override the
doInBackground()
method where resource intensive logic is placed. This can be easily fused with the typical MVC pattern, and is typical of Swing applications.EDIT #1:
Additionally, it is important to consider MVC as a sort of composite of various patterns. For example, your model could be implemented using the Observer pattern (requiring the View to be registered as an observer to the model) while your controller might use the Strategy pattern.
EDIT #2:
I would additionally like to answer specifically your question. You should display your table buttons, etc in the View, which would obviously implement an ActionListener. In your
actionPerformed()
method, you detect the event and send it to a related method in the controller (remember- the view holds a reference to the controller). So when a button is clicked, the event is detected by the view, sent to the controller's method, the controller might directly ask the view to disable the button or something. Next, the controller will interact with and modify the model (which will mostly have getter and setter methods, and some other ones to register and notify observers and so on). As soon as the model is modified, it will call an update on registered observers (this will be the view in your case). Hence, the view will now update itself.You have to make up your mind whether the so called apple-specific method (in this case
checkPrice()
) is really specific to Apple. Or it is actually generally applicable to all fruits.A method that is generally applicable should be declared in the base class
Assuming the answer is yes (in this case it does seems to be yes), then you should declare the method in the base class. In this case you can iterate through all the different types of fruits, and all of them would accept the method
checkPrice()
, so you don't even need to make a special case for apples.A method that isn't generally applicable can be declared in an interface
What if the answer is no? Let's assume we need another method called
getJuicePrice()
, and we further assume that only some fruits can be made into juice (apple juice, orange juice) but other cannot (pineapple? durian?). In this case, a simple solution is to declare an interface, and only the fruits for which the method is appropriate would implement the interface. So let's say this interface isJuiceBehavior
And all fruits for which juice behavior is applicable (yes for
Apple
, no forDurian
) would implement the interface:And then in your loop, what you check is whether a
fruit
isinstanceof
the interface:This solution would work for simple cases, but in more complicated cases, you may notice that you start to duplicate a lot of implementation code for
getJuicePrice()
for different types of fruits. This leads to the next topicDesign Pattern: Strategy
You may want to start thinking about the Design Pattern called Strategy, which further encapsulates
JuiceBehavior
and make it into a family of classes representing different juice behaviors. It also let you set different types of fruits to take different implementations ofJuiceBehavior
. I won't go into the details here. But you can read up on that on some books about Design Patterns. Such asThe Decorator Pattern is probably the most straight forward one to use and would be a good one to extend concrete objects functionality and/or characteristics.
Here is some light reading: Head First Design Patterns - CH3 pdf
FYI, couple must have's for learning and referencing design patterns regardless your language of choice:
1) Head First Design Patterns
2) Patterns for Enterprise Application Architecture
3) Design Patterns: Elements of Reusable Object-Oriented Software
And sites:
1) DoFactory
2) StackOverflow Design Patterns Newbie
There are a few others, I'll have to dig them up.