Design Patters: Strategy

Credit: Concepts explained here are based on a book, “増補改訂版Java言語で学ぶデザインパターン入門”.  A strategy pattern makes it easy to change an algorithm to solve a problem. Let’s run a sample program which uses two different strategies for Rock paper scissors. Hand: A class representing a hand, one of Rock, Paper and Scissors. Strategy: A class representing strategyContinue reading “Design Patters: Strategy”

Design Patters: Bridge

Credit: Concepts explained here are based on a book, “増補改訂版Java言語で学ぶデザインパターン入門”. A bridge pattern builds a bridge between two kinds of classes: one working as abstraction and one working as implantation. Let’s see a concrete example. Display and CountDisplay work as abstraction or User Interface. These classes delegate actual work to a class working as implementation.Continue reading “Design Patters: Bridge”

Design Patters: Abstract Factory

Credit: Concepts explained here are based on a book, “増補改訂版Java言語で学ぶデザインパターン入門”. In an abstract factory pattern, different abstract components are combined to create an abstract product. Let’s see a sample program which creates a HTML file. There are two packages: factory package and listfactory package. A factory package contains classes that create an abstract factory, componentsContinue reading “Design Patters: Abstract Factory”

Design Patters: Builder

Credit: Concepts explained here are based on a book, “増補改訂版Java言語で学ぶデザインパターン入門”.  A builder pattern is a pattern in which an instance is created by calling a class which in turn calls an interface which is a framework for objects to be built. Let’s create a sample program in which a document is created. A document isContinue reading “Design Patters: Builder”

Design Patters: Prototype

Credit: Concepts explained here are based on a book, “増補改訂版Java言語で学ぶデザインパターン入門”.  Prototype is a pattern in which an instance is duplicated by calling an Interface, not by calling a constructor. This pattern is used when it is convenient to clone an instance than to create a new instance. Let’s explain concepts in a program. First, weContinue reading “Design Patters: Prototype”

Design Patters: Singleton

Credit: Concepts explained here are based on a book, “増補改訂版Java言語で学ぶデザインパターン入門”. In this blog post, a singleton pattern is explained. This pattern is used when you want to guarantee that there are no more than one instance generated. A singleton class creates an instance by being called get_instance method. Note that since Python does not haveContinue reading “Design Patters: Singleton”

Design Patters: Factory Method

(Credit: Concepts explained here are based on a book, “増補改訂版Java言語で学ぶデザインパターン入門”.) A factory method pattern is a pattern in which a superclass describes how an instance should be created but does not define its internal structure. Its detailed structure is written in a subclass. Let’s create a program. In this program, a framework package is usedContinue reading “Design Patters: Factory Method”

Design Patters: Template Method

A template method pattern is a pattern that has a template. In this pattern, a superclass has template methods which have some abstract methods. These methods describe how abstract methods are called which are implemented by its subclasses. Let’s see an example by creating a program which displays characters or strings for five times. ClassContinue reading “Design Patters: Template Method”

Design Patterns: Adapter

In this post, an adapter pattern is explained. Original code in Java can be found in a book, I “増補改訂版Java言語で学ぶデザインパターン入門”. An adapter works as a middleware between an adaptee and a target, each has its own methods. If an adaptee and a target are incompatible with each other, an adapter bridges a gap between theContinue reading “Design Patterns: Adapter”

Design Patterns: Iterator

In this blog post, I am going to explain one of the design patterns, an iterator. Wikipedia defines an iterator this way:  An iterator is an object that enables a programmer to traverse a container, particularly lists. I explain concepts based on a book, “増補改訂版Java言語で学ぶデザインパターン入門”. I translate code in Java in the book to code in Python. In thisContinue reading “Design Patterns: Iterator”