Adapter Design Pattern

I’ve always found Derek Banas’ channel really useful. In late middle school, I started watching his tutorials for how to code. So it’s really interesting to go back and watch his videos, to say the least.

I feel like the adapter design pattern is something that shouldn’t really need to be used. Don’t get me wrong, it’s very useful. Like real adapters, however, the problem itself shouldn’t really exist. That’s especially true with code; it seems as though adapters become required when interfaces aren’t abstracted well enough. In his video, for example, he has an EnemyAttacker interface that represents some kind of enemy. However, the methods are very specific and presume certain characteristics about the attacker. We then need to use an adapter to get around that specificity. It seems to me that writing the interface more generally to begin with would be more ideal. But given that it’s a bad idea to modify old working code, an adapter is a great solution.

In principle, an adapter does exactly what you expect it to: it adapts code. It takes one “interface” and connects it to another “interface”. That’s the colloquial interface as opposed to a coding specific interface. We use adapters every day. The best example is a phone charger. It converts (or adapts) 120V AC power to a low DC voltage. It also adapts the physical plug into USB type-c. All of the functionality is hidden inside of the charger and from the perspective of a user, it’s literally plug and play.

An adapter in code acts the same. I honestly like the example he gave so if you want to see an example of actual code, check out his video. Conceptually, one description of an adapter is the following: We have two interfaces A and B that have many differences, but are conceptually similar. We can create an adapter so we can use any A as a B, or vice versa. It’s okay because of their similarity (or perhaps they need not even be similar!). Really, it’s an incredibly basic pattern so the specifics aren’t that important.

Fundamentally, an adapter is just code that allows other code to operate together. Again, I think the best way to conceptualize it is through the imagery of any real life adapters. However, it should ideally not be necessary in scenarios such as the one from the video. Abstraction, within reason of course, should be prioritized ahead of time.

Introduction to the Duodecimal System

Prerequisites

  • A basic understanding of base systems
  • A basic understanding of arithmetic

Terminology

  • I will be using the symbol Χ to mean the number dek. This is the duodecimal single digit for the number ten. I personally prefer the upside-down 2 symbol; however, this is easier to type.
  • I will be using the symbol Ɛ to mean the number el. This is the duodecimal single digit for the number eleven.
  • All plain numbers here will be decimal numbers.
  • I will write duodecimal numbers in brackets unless specified otherwise.
    • 12 = twelve = [10]
    • .25 = [.3]
    • ½ = “one half” = [½]
  • When I’m giving an example of another number base, I will write that number in parenthesis. I will specify the base in words.
  • I might ignore those last two rules when I’m very clearly writing a long example in a non-decimal base.
  • To represent repeating decimals, I will be using 3 notations for clarity because I’m apparently unable to overline text:
    • .453… means .45333333 repeating
    • .12r means .1212121212 repeating
    • .16’56’r means .16565656 repeating

What makes base n better than base m?

We use a base system for writing numbers because it allows for complex math in a way that something derived from a tally system wouldn’t. It’s easy to add 500 and 400 in a bass system. If you had to actually count 500 and 400 lines to determine there are 900 in total, you probably wouldn’t even bother.

So, are some bases better than others? Yes. The number 500 in binary is (111110100). That requires far more space to express the same value. However, in base 500, 500 = (10). Does that make base 500 even better than base 10 because it takes up even less space? No. While it’s good to condense the size of a number for writing purposes, that’s only true to an extent. If you have to memorize 490 new symbols it’s hardly any easier to write (10) than 500. And as the number of digits you allow increases, the more dense the base becomes. So we need to find a balance.

How should we then compare base 7 and base 6? Base 7 will only have 1 extra symbol, which isn’t even a new symbol to us in base 10, and it will allow for increased density. That doesn’t mean that base 7 is better than base 6, however. 7 is a prime number. So the only factors of 7 are 1 and 7. It’s also odd. Which means counting by 2 isn’t as easy as it is in an even base. Notice that the numbers it’s easiest to count with and perform other arithmetic operations with are factors of the base you’re using.

Let’s count in base 7 until we hit a repeat 1s digit:

  1. 0 1 2 3 4 5 6 10
  2. 0 2 4 6 11 13 15 20
  3. 0 3 6 12 15 21 24 30
  4. 0 4 11 15 22 26 33 40
  5. 0 5 13 21 26 34 42 50
  6. 0 6 15 24 33 42 51 60
  7. 0 10

It takes a long time to hit a repeat digit. This means there aren’t simple patterns that are easy to remember. Now let’s count in base 6 until we hit a repeat 1s digit:

  1. 0 1 2 3 4 5 10
  2. 0 2 4 10
  3. 0 3 10
  4. 0 4 12 20
  5. 0 5 14 23 32 41 50
  6. 0 10

As you can see, it’s far easier to count in base 6 than it is in base 7. You may also notice some patterns.

  • Anytime you count by a factor of base n, you will repeat after (10) = n.
  • Anytime you count by a non-factor a, you will repeat after an.
  • Anytime you count by a multiple of a factor km, where mn, you will repeat after kn.

You also might notice that certain patterns you’re familiar with in base 10 are present in these bases. For instance, the pattern for counting by 9 in base 10 works to count by 6 in base 7. This is because, in general for base n, all numbers share patterns. The reason 9 has that pattern of count up by 1 10 and count down by 1 1 is because 10 = 9 + 1. In a sense, 9 and 1 are inverses. You can also notice their 1s pattern is the reverse of the other’s. This is true for all digits less than n. Look at the above patterns and look at how the numbers that add to the base has inverse patterns in the 1s place.

This isn’t limited to counting. Since addition is repeating counting, subtraction is the inverse of addition, multiplication is repeated addition, and division is the inverse of multiplication, the patterns that show up here apply to those arithmetic operations. Let’s look at fractions in base 7:

  1. 1/1 = 1
  2. ½ = .333…
  3. ⅓ = .222…
  4. ¼ = .1515r
  5. ⅕ = .1254r
  6. ⅙ = .111…
  7. 1/10 = .1

As you can see, basically all of them are infinite rational septenary-decimals. Now let’s look at base 6:

  1. 1/1 = 1
  2. ½ = .3
  3. ⅓ = .2
  4. ¼ = .13
  5. ⅕ = .1111…
  6. 1/10 = .1

As you can see, fractions of non-factors result in infinite rationals. It’s clear they’re rational by the fact that we began with a fraction. This means that while there are infinite terms, the terms repeat. That being said, it’s still clearly simpler to use decimals that have finitely many digits. Ideally 1. Also notice that in base n, n-1 is never a factor except when n = 2. So the fraction of n-1 will always be .1111…, since x * .1111… is .xxxx… where x is the largest possible digit in base n. That is simply equal to 1.

Counting and fractions are only two of the ways these patterns manifest themselves. However, by now, it should be clear that the more factors a base has, the easier it is to use on a day to day level. It’s important to know that at a high level, math won’t change. It’ll remain the same to mathematicians regardless of what base we use. The benefits of one base over another are primarily to ease of learning and ease of day-to-day use. So the following are the main ways a base can be better than another:

  • Less space required
  • Less digits required.
  • More factors.
  • More prime factors.

I’ve yet to explicitly talk about prime factors. It is more useful to have 2 and 3 as factors than it is to have 3 and 5 as factors. This is because ½ of all integers are even and ⅓ are multiples of 3. I’m aware that those statements are not necessarily correct when we’re dealing with infinite sets, but I believe you understand what I mean. There are a lot of multiples of small primes. So we want as many factors as possible without having too many new symbols to memorize for an ideal base.


Duodecimal (Dozenal / Base 12)

So, what’s so good about 12? Well let’s start by comparing 10 and 12 in terms of the aforementioned properties:

  • Base 10 has 10 digits: 0,1,2,3,4,5,6,7,8,9.
  • Base 12 has 12 digits: 0,1,2,3,4,5,6,7,8,9,Χ,Ɛ
  • 10 has 4 factors: 1,2,5,10
  • 12 has 6 factors: 1,2,3,4,6,12
  • They both have 2 prime factors.

Now, how do they compare? In terms of number size, base 12 has smaller numbers because it has 2 extra digits. Now 2 isn’t that many extra to remember, but it should still be considered. Notice that [10] = 12, [100] = 144, [1000] = 1728. In base 12, you can represent 2 more 1-digit numbers, 44 more 2-digit numbers, and 728 more 3-digit numbers. This compounding effect continues to infinity. For just two more digits, you can increase the density of numbers considerably.

Now let’s talk about the elephant in the room: the amazing properties of the factors of 12. 12 is divisible by 2 and 3, as well as 4. This means the most common fractions ½, ⅓, and ¼ will be one digit duodecimals. Let’s compare the patterns of base 10 to base 12:

  1. 0 1 2 3 4 5 6 7 8 9 10
  2. 0 2 4 6 8 10
  3. 0 3 6 9 12 15 18 21 24 27 30
  4. 0 4 8 12 16 20
  5. 0 5 10
  6. 0 6 12 18 24 30
  7. 0 7 14 21 28 35 42 49 56 63 70
  8. 0 8 16 24 36 44 52 60
  9. 0 9 18 27 36 45 54 63 72 81 90
  10. 0 10
  1. 0 1 2 3 4 5 6 7 8 9 Χ Ɛ 10
  2. 0 2 4 6 8 Χ 10
  3. 0 3 6 9 10
  4. 0 4 8 10
  5. 0 5 Χ 13 18 21 26 2Ɛ 34 39 42 47 50
  6. 0 6 10
  7. 0 7 12 19 24 2Ɛ 36 41 48 53 5Χ 65 70
  8. 0 8 14 20
  9. 0 9 16 23 30
  10. 0 Χ 18 26 34 42 50 5Χ 68 76 84 92 Χ0
  11. 0 Ɛ 1Χ 29 38 47 56 65 74 83 92 Χ1 Ɛ0

Now at a glance, duodecimal might appear more complicated. And I will admit that the pattern for 5 and 7 is more complex than any of the patterns in base 10. However, if you look closely, the rest of the patterns are incredibly simple. Let’s now look at fractions:

  1. 1/1 = 1
  2. ½ = .5
  3. ⅓ = .333…
  4. ¼ = .25
  5. ⅕ = .2
  6. ⅙ = .1666…
  7. 1/7 = .142857r
  8. ⅛ = .125
  9. 1/9 = .111…
  10. 1/10 = .1
  1. 1/1 = 1
  2. ½ = .6
  3. ⅓ = .4
  4. ¼ = .3
  5. ⅕ = .2497r
  6. ⅙ = .2
  7. 1/7 = .186Χ35r
  8. ⅛ = .16
  9. 1/9 = .14
  10. 1/Χ = .1’2497’r
  11. 1/Ɛ = .111…
  12. 1/10 = .1

I’ll be the first to admit that when base 12 has repeating fractions, they are more complex than when base 10 has repeating fractions. However, because of 12’s factors, this will happen less often. It also means the most common fractions are simple one digit duodecimals. Since education is such a big worry when it comes to number systems, learning fractions in base 12 will be easier. We’ll also still have examples of repeating duodecimals to teach to kids.

A likely reason why we use base 10 is because we have 10 fingers, so counting by hand is easy. However, look at your hands. Each finger is split into thirds by your knuckles. This means with 4 fingers, you have 12 sections. So kids can be taught how to count by tapping each section with their thumb from the top of the index finger to the bottom of the pinky. And notice, this is superior to base 10 counting! You can count up to 24 on your hands in base 12. In base 10 you can only count up to 10 before you have to get creative. That being said, if you were showing someone a number through your hands, it would be the same as base 10. So perhaps the fact that kids would have to learn two ways to count could be a negative for duodecimal.

One last thing I’d like to mention about duodecimal is that it’s basically the ideal base for humans. You might ask why hexadecimal isn’t ideal. After all, it’s incredibly easy to translate between hexadecimal and binary. Perhaps it would even significantly increase the speed of our computers if we did use hexadecimal. That being said, 6 new digits is a lot to learn. Also, 16’s factors are 1,2,4,8,16. Only 5 factors and only 1 prime factor. Even numbers and powers of 2 are very convenient in hexadecimal, but odd numbers are not. I believe duodecimal is still the better base for daily use. The main reason why is simply because 12 = 3 * 4. Having a factor of 4 means you have two factors of 2. That means you have 3 powers of 2: 1,2,4 as factors. You also have 3 meaning you have both of the simplest primes. Another reason why factors are so important is because of number theory. In duodecimal, every number that ends in a multiple of 3: 0,3,6,9 is a multiple of 3. Every number that ends in a multiple of 4: 0,4,8 is multiple of 4. Every ,number that ends in a multiple of 6: 0,6 is a multiple of 6. Every multiple of 8 ends in 0,8,4, or 2. Every multiple of 9 ends in 0,9,6, or 3. Every multiple of 3 and 4 ends in 0. Having a factor of 3 and 4 is ideal and 12 is the smallest multiple of 3 and 4 with a total of 6 factors.


Conclusion

If we lived in an ideal world, we would be using duodecimal with a version of the metric system based on 12s instead of 10s. Unfortunately, the world is already set on base 10. If you think it would be hard for the US to switch to the metric system, imagine trying to switch the entire world to the duodecimal system. It’s almost certainly never going to happen.

With that said, it doesn’t change the fact that duodecimal is objectively more convenient and useful than decimal in many ways. Hopefully, this has helped you understand that and maybe you’ll become one of the few of us who are wishing that maybe one day we can make the switch. If you’re interested in more, check out the duodecimal category for similar posts. Or leave a comment about what you want to hear more about!

If you find spelling errors or logic errors please let me know in the comments. I’ll be sure to come back and edit this post. My goal as a blogger is to be scientific and non-biased. I don’t want to add to the massive amount of misinformation online, so I’m willing to point out when I’m wrong and make changes later on. I usually write off the top of my head so I expect there to be a lot of errors, especially with the informal way I’m writing this.

Introductory Post for CS-343

Welcome! First I would like to point out that all posts for this course should all be placed in roughly the same categories. However, they all will be tagged with CS-343. This should make them easy to find.

These posts will be centered around me searching for online materials other people have posted that relate to the core topics of CS-343. I will then be sharing them, interpreting them, etc. It will be a collection of information primarily useful to the following topics (as taken from the course syllabus):

  • Design Principles
    • Object Oriented Programming
    • SOLID
    • DRY
    • YAGNI
    • GRASP
    • “Encapsulate what varies.”
    • “Program to an interface, not an implementation.”
    • “Favor composition over inheritance.”
    • “Strive for loosely couples designs between objects that interact”
    • Principle of Least Knowledge
    • Inversion of Control
  • Design Patterns
    • Creational
    • Structural
    • Behavioral
    • Concurrency
  • Refactoring
  • Smells
    • Code Smells
    • Design Smells
  • Software Architectures
    • Architectural Patterns
    • Architectural Styles
  • REST API Design
  • Software Frameworks
  • Documentation
  • Modeling
    • Unified Modeling Language
    • C4 Model
  • Anti-Patterns
  • Implementation of Web Systems
    • Front End
    • Back End
    • Data Persistence Layer

Hopefully, these posts provide you with many resources to help you learn these topics for the first time or to help you recall them after a long time has passed! I wish you the best of luck in whatever you’re hoping to achieve!

The Introspective Thinker

Welcome! This blog will be a place where I can do some of the following things:

  • Leave notes for myself
  • Create explanations for certain technological tools or mathematical ideas
  • Figure out a political position based on a logical argument and citations
  • Discuss random topics on my mind
  • Think about the nature of life and the universe
  • And more!

I’m not sure whether or not I’ll delve too deeply into politics, but if I do I want to take a neutral scientifically skeptical viewpoint. I want to cite sources for data, go back and make corrections, and edit as needed.

I imagine I’ll mostly be discussing STEM ideas but I may end up doing anything from a game walk-through to a film analysis.

I hope this ends up providing some kind of use for you!