Code Refactoring 13

preview_player
Показать описание

In this part of my code refactoring tutorial, I'll show you how to replace conditionals with the command pattern. I'm also going to do more though.

I'll also review what the command pattern is. I'll give an example of when you should use it. We'll look at how to add flexibility with it. I'll answer your questions based on the refactoring to patterns book. And, we'll look at how to store commands in an ArrayList.
Рекомендации по теме
Комментарии
Автор

Thank you :)

Use Strategy if you need to dynamically change an algorithm used by an object at run time.
Use Command when you want to store a list of commands for later use

derekbanas
Автор

It amazed me how quickly I learned C#. It is nearly identical to Java. The design patterns and everything else is the same as well. Some times they take exactly the same number of lines to implement. I'm glad you understand that I can't flip over to c# right now though. It is the only thing that is hard about making these videos

derekbanas
Автор

They will be covered. You'll be amazed by how easy they will be if you have been following refactoring :)

derekbanas
Автор

Actually, earlier I asked for C# videos, and you said pretty much the same thing. So I took it upon myself to try to convert the code myself. By doing so, I ended up learning a lot more about both languages!

XonicDigifact
Автор

What topic would you like me to cover? (THIS IS OPEN TO EVERYONE FOR A VOTE) Here are your options: 1. Java Algorithms 2. Java 2D / 3D

derekbanas
Автор

My notes: The command pattern implements a set of predefined describing interactions (invoke/execute). They are fixed/static (which is reflected in the code through the "static" keyword). The strategy pattern implements a set of algorithms/calculations that change dynamically based on the situation.

We create another step of flexibility by constructing on top of the current layer another class which stores the class below (via composition) and delegates an action through similar methods (that's the invoke/execute thing). Due to late binding on the contained object the command is executed dynamically (it's polymorphism).

Comparison before and after: The conditionals are the commands which are coded into new classes. The waiter class actually didn't exist in the original version of the code. Neither did the cash register.

Brax
Автор

Sorry, but I can't cover c# right now. I have to many other topics in line. Pretty much ever OOD tutorial applies to c# though because java is almost identical. Sorry

derekbanas
Автор

As far as I know Minecraft was made using the Lightweight Java Game Library which is a wrapper library for OpenGL

derekbanas
Автор

Java 2d 3d is normally good enough for making most games. OpenGL requires c normally, but I'll cover some java libraries that provide access to it.

derekbanas
Автор

I'll do my best to mix things up and cover everything. Everything is easier than refactoring, but I'll definitely finish refactoring because I have many silent people emailing me to continue. I know everyone wants games. I do as well :)

derekbanas
Автор

I may bounce back and forth between refactoring and algorithms then. Believe me algorithms are much easier to teach

derekbanas
Автор

In line 101 you should have used the 'base type' List instead of ArrayList. Furthermore, in this particular example, CustomerPicker should have been implemented with Singletons since the Objects don't store any 'personalized' data at all. So you'll save some amount of memory if this is used in large environments...

leviathanx
Автор

Thank you for the input :) I know I can build upon perfecting the code, but I try to keep things from being to complicated. It is sometimes hard to balance

derekbanas
Автор

გამარჯობა მწვრთნელი,

გმადლობთ ბევრი კიდევ ერთხელ დეტალური მსჯელობა.

ძალიან სააღმზრდელო.

ჩემი სადილი აფერხებს ჩემი ოქროს მედალი დევნა.

თქვენ გაქვთ დალოცა შაბათ.

როგორც ყოველთვის,

იზრუნეთ.

japanuser
Автор

"You always want to plan for the future and the future always means additional changes"
If all developers could be aware of that!

Arkanthrall
Автор

And if I require another additional condition in future? Then what about CustomerTypePicker class? Do I need to again recompile ithat class? Or just subclass it to extend?

sayantanchatterjee
Автор

Oops, hit send before being finished.... other than that you are doing a good job explaining these kinds of things.
Feels like being back in my studies.... ;)

leviathanx
Автор

yh thats what i ment, opengl libraries. But is for example minecraft made in opengl and java, or opengl libraries and java?

Bostonswe
Автор

დიდი მადლობა. თქვენ ძალიან კეთილი. მაქვს პატივი, რომ თქვენ სარგებლობენ ჩემი ვიდეოები

derekbanas
Автор

Count my "up vote" for the initial ppt slide that outlines the command pattern. It was very helpful to keep track like a table of contents.

wongright