Wrap Class – Refactor Legacy Pattern

Wrap Class

When using a wrap method, we want to add a new behavior that can be added around the method, such as before or after the existing code.
However, there are times when we need to add behavior in more than one method, like logging before or after some, or all public methods.

Description and how to apply

There are at least 2 ways of doing it:

1. Using the Decorator Pattern

When using the **Decorator Pattern**, we want to make sure both classes implement the same interface or inherit from the same base class, because we want the subclass to have the same “public” interface. This is because a Decorator class can be used interchangeably between the other Decorators. For instance, having a logging decorator or a performance decorator should be as easy as just pass one instance or the other in a class constructor, and its use should be transparent.

2. Aggregating the current class

If you think you want to be more specific about the class being refactored,
then a Wrap Class just wrapping the existing one should be enough. In this case, you don’t need to *extract interface* since the class is passed in the constructor parameters and used in the method. In this approach, you can do a “method rename”, for instance, if the original method was called *process*, the new exposed method could be *processWithLogging*. Another advantage is that in the constructor, you can pass other information useful for the new behavior, something that in the Decorator Pattern can “violate” the Decorator Pattern principle.

Which one?

From the examples below you can observe the Aggregating solution is more specific than the Decorator. Both will work, but if you need help choosing, choose the Decorator if you think that other changes will happen in the future that can be added creating more Decorators, and/or the updates are just adding new behavior, and probably you expect changes in multiple methods, like logging every public method. Use the Aggregating solution if the change is very specific, just in a few places and you don’t expect great changes in the future for these methods.


Imagine the following business existing code:

Using the Decorator Pattern

You just have to replace the calls to CustomerPaymentProcessor with the Decorator, sice both implement the same interface.

Probably when refactoring you will need to *Extract Interface* or create an *Abstract Class* to implement this pattern.

Using a Wrapper Class

Here, the solution is specific to the code refactoring being done, it cannot be reused.


Android Adventures – Development Tips – Part 5

How to add the back button on the Activity Action Bar

If the character space stops working with EditBox when using Listeners
Probably it is the google keyboard trying to impose words instead of characters
The one solution that worked for me was to disable the text suggestions


Android Adventures-Development Tips-Part 4

How to remove the divider in a ListView?
What if my text doesn’t fit in a TextView?
How to return values from to the caller Activity?
Main Activity:
Highlight an item in a ListView


Android Adventures–Development Tips-Parte 3

How to shrink activity layout when virtual keyboard is displayed and hides some of the views?
There are several different solutions, one of them is:

– In the AndroidManifest.xml, for the target activity


If you use Database Room framework for SQLite management and Kotlin:

If you are using Kotlin don’t create a new method, you can have compilation errors on the generated class

java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
Solution 1: add allowMainThreadQueries

MyApp.database =  Room.databaseBuilder(this, AppDatabase::class.java, “MyDatabase”).allowMainThreadQueries().build()

Solution 2: Use another thread

Solution 3: If you use Kotlin 1.3+ you can use coroutines

How to explore a database in Android Emulator?
– You can copy the db files from the device to your computer and use a sqlite browser

– I use: Android Debug Database: https://github.com/amitshekhariitbhu/Android-Debug-Database


Android Adventures – Development Tips – Parte 2

How to add an icon to a button?
How to push a button in a LinearLayout to the right?
The trick in is the empty view


How to have an ImageButton transparent?
How to clear all the Spannable styles in an EditText?
val currentPositionsInputText = contentTextEditBox.selectionStart

Android Adventures – Development Tips

How to prevent an EditBox from expanding?

android:layout_width="0dp" – or height, depending the need and the use of the weight

android:layout_weight="1" – if you just have one and you want to fill the whole layout

How to add a Scrollbar to an EditBox?
How to write the text on top left of the EditBox?
How to avoid the overlap of the Toolbar with the included layout?
How to Highlight blocks of text in an EditBox?
Spannable spannable=new SpannableString(string);
// You can add other styles here, like bold, underline, other// if you are using a loop, put the Spannable construction outside the loop or sometimes only the last one will be used

spannable.setSpan(new ForegroundColorSpan(Color.BLUE), start, end, 0);

Being a better architect – Developing for several markets


An architect should always be ready to increase is knowledge considering the actual market tendencies, because it is his job to present solutions to his customers, which are always considering solutions to decrease their costs using IT. With this vision, as an architect I am always pushing myself with new challenges and learning more and more. Since the beginning of the current year I have bought books, in subjects like IT, social behavior, strategy and soccer. I like to develop myself in different subjects in order to acquire other perspectives and expand my way of thinking. I believe it increases my creativity and give me thinking tools to handle challenging projects.

Two weeks ago, I decided to develop an application and publish it in different App Stores, I want to know the software production lifecycle in all these markets:

  • Android Play Store
  • Apple Store
  • Google Chrome Extension
  • Firefox Add On
  • Windows Metro
  • Windows Phone
  • (Maybe) Android Watch
  • (Maybe) Apple iWatch

The idea was to learn how these markets/stores work. What is to develop an application in different languages, different technologies and different paradigms.

After some analysis, I decided not to use popular developing frameworks, where your code once and they will produce code for the several markets. I wanted to use the native environment/language, Developing using HTML5/JavaScript is about design, not about acquiring some know how in each environment. I wanted to use the knowledge I already had in languages like Java, C#, JavaScript and HTML, XAML and little bit of Objective C, and maybe learn the new Swift Apple language.

At the end, my expectations are to be happy with the result. Knowing I have learned how to develop to the most important markets and be a better technical team leader, architect and have increased my know how to manage these kind of projects.

The process

I already started with this professional project/program. I have used a three step approach:

First, what kind of application to develop.

I have a lot of ideas for apps, so I chose one where I could develop more than one screen, should have some utility and a basis for increments and upgrades. For version 1.0, I just wanted to release it, not earning money or using traffic or Ads, just be in the market to get the know how about planning, developing and publishing it.


Second, decide the target markets/platforms.

I chose the six platforms/markets above, I believed they were the big ones.

Chrome and Firefox extensions are in the list to improve the knowledge in the browser arena, I think it is important for an architect having a little bit of knowledge in the user applications/tools we use daily. It helps understand the end user needs.

The “watch” items, lets see if I have time and energy. The issue with developing for several markets, it’s the management, not the developing.


Third, the order of development.

I started with the Chrome Extension, after researching how to develop one. It is simple, just HTML, JavaScript, and a manifest. Some screenshots and marketing images, and it is done. A good basis of know how for the future developments.


Next steps

I am now developing the windows version for Windows Phone and Windows Metro. I have already developed the Android App and Chrome Extension. You can follow all these apps on my blog at “Give Me Numbers – Apps”.

In future posts I will describe the process I have followed for each app. Including, at the end, a comparison between all technologies from the perspective of a developer/publisher and manager

Great Technical Books I have read in the last months

transferirAs a software architect I must have a broad knowledge of several subjects so I can have several tools in my brain to create the better solutions with the right tools. I’ve read (and still reading!) a lot of books, so this post will show some of my last readings with some comment on each of the books.

In future posts I will show other interesting books, sometimes not directly related with software but they gave me another way of thinking about systems, patterns and algorithms that helped me to be a better professional, as a technician and leader whenever necessary.

Hope it can be helpful to your tech and soft improvement in the software industry.

If you know others books you have read who can enrich my knowledge, I will appreciate your comment!

How many methods you have in your classes with: Logging, transaction, validation, exception management? How about remove the code from those methods, write it in another class(es) and add just attributes (or java annotations) and surprise! Your code its so much cleaned! This book has improved the quality of my code, architecture and thinking about applications!
Dependency Injection its about building great software architectures. Its about building software like a house, its about decoupling and loosely couple. Its about building testable software, its about software with quality.
A great book about NoSql! Not too deep, but with the enough information to understand the NoSql world. Even with years in database systems I’ve learned new ways of thinking in this book.
The reference for programming windows, now for windows 8. You can’t go wrong with this book.
You want to know about SCRUM, agile and how it works, this is a very good book to start.
Best book for android development.
I know C, C# and C++. With this book from these guys, I added Objective C to the Cs languages list!
Great book about iOS development. Starts from the basis and then develops some applications and of course your knowledge.