Object Design Style Guide
eBook - ePub

Object Design Style Guide

Matthias Noback

Share book
  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Object Design Style Guide

Matthias Noback

Book details
Book preview
Table of contents
Citations

About This Book

Objects are the central concept of languages like Java, Python, C#. Applying best practices for object design means that your code will be easy to read, write, and maintain. Object Design Style Guide captures dozens of techniques for creating pro-quality OO code that can stand the test of time. Examples are in an instantly familiar pseudocode, teaching techniques you can apply to any OO language, from C++ to PHP.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Object Design Style Guide an online PDF/ePUB?
Yes, you can access Object Design Style Guide by Matthias Noback in PDF and/or ePUB format, as well as other popular books in Computer Science & Object Oriented Programming. We have over one million books available in our catalogue for you to explore.

Information

Chapter 1. Programming with objects: A primer

This chapter covers
  • Working with objects
  • Unit testing
  • Dynamic arrays
Before we get into the actual style guide, this chapter covers some of the fundamental aspects of programming with objects. We’ll briefly go over some important concepts and establish a shared terminology that we can build on in the following chapters.
We’ll be covering the following topics in this chapter:
  • Classes and objects— Creating objects based on classes, using a constructor, static versus object methods, static factory methods for creating new instances, and throwing exceptions inside a constructor (section 1.1).
  • State— Defining private and public properties, assigning values to them, constants, and mutable versus immutable state (section 1.2).
  • Behavior— Private and public methods, passing values as arguments, and NullPointerExceptions (section 1.3).
  • Dependencies— Instantiating them, locating them, and injecting them as constructor arguments (section 1.4).
  • Inheritance— Interfaces, abstract classes, overriding implementations, and final classes (section 1.5).
  • Polymorphism— Same interface, different behavior (section 1.6).
  • Composition— Assigning objects to properties and building more advanced objects (section 1.7).
  • Return statements and exceptions— Returning a value from a method, throwing an exception inside a method, catching exceptions, and defining custom exception classes (section 1.9).
  • Unit testing— Arrange-Act-Assert, testing for failures, and using test doubles to replace dependencies (section 1.10).
  • Dynamic arrays— Using them to create lists or maps (section 1.11).
If you are somewhat familiar with all of these topics, feel free to skip this chapter and jump to chapter 2. If some topics are unknown to you, take a look at the corresponding sections. If you are just beginning as an object-oriented programmer, I recommend reading this whole chapter.

1.1. Classes and objects

The runtime behavior of an object is defined by its class definition. Using a given class, you can create any number of objects. The following listing shows a simple class, with no state or behavior, which can be instantiated.
Listing 1.1. A minimum viable class
class Foo { // There's nothing here } object1 = new Foo(); object2 = new Foo(); object1 == object2 // false 1
  • 1 Two instances of the same class should not be considered the same.
Once you have an instance, you can call methods on it.
Listing 1.2. Calling a method on an instance
class Foo { public function someMethod(): void { // Do something } } object1 = new Foo(); object1.someMethod();
A regular method, like someMethod(), can only be called on an instance of the class. Such a method is called an object method. You can also define methods that can be called without an instance. These are called static methods.
Listing 1.3. Defining a static method
class Foo { public function anObjectMethod(): void { // ... } public static function aStaticMethod(): void { // ... } } object1 = new Foo(); object1.anObjectMethod(); 1 Foo.aStaticMethod(); 2
  • 1 anObjectMethod() can only be called on an instance of SomeClass.
  • 2 aStaticMethod() can be called without an instance.
Besides object and static methods, a class can also contain a special method: the constructor. This method will be called before a reference to the object gets returned. If you need to do anything to prepare the object before it’s going to be used, you can do it inside the constructor.
Listing 1.4. Defining a constructor method
class Foo { public function __construct() { // Prepare the object } } object1 = new Foo(); 1
  • 1 __construct() will be implicitly called before a Foo instance gets assigned to objectl.
You can prevent an object from being fully instantiated by throwing an exception inside the constructor, as shown in the following listing. You can read more about exceptions in section 1.9.
Listing 1.5. Throwing an exception inside the constructor
class Foo { public function __construct() { throw new RuntimeException(); 1 } } try { objectl = new Foo(); } catch (RuntimeException exception) { // `object1` will be undefined here }
  • 1 It won’t be possible to instantiate Foo because its constructor always throws an exception.
The standard way to instantiate a class is using the new operator, as we just saw. It’s also possible to define a static factory method on the class itself, which returns a new instance of the class.
Listing 1.6. Defining a static factory method
class Foo { public static function create(): Foo { return new Foo(); } } object1 = Foo.create(); object2 = Foo.create();
The create() method has to be defined as static because it should be called on the class, not on an instance of that class.

1.2. State

An object can contain data. This data will be stored in properties. A property will have a name and a type, and it can be populated at any moment after instantiation. A common place for assigning values to properties is inside the constructor.
Listing 1.7. Defining properties and assigning values
class Foo { private int someNumber; private string someString; public function __construct() { this.someNumber = 10; this.someString = 'Hello, world!'; } } object1 = new Foo(); 1
  • 1 After instantiation, someNumber and someString will contain 10 and ‘Hello, world!’ respectively.
The data contained in an object is also known as its state. If that data is going to be hardcoded, as in the previous example, you might as well make it part of the property definition or define a constant for it.
Listing 1.8. Defining constants
class Foo { private const int someNumber = 10; 1 private someString = 'Hello, world!'; }
  • 1 Your programming language may have a different syntax. For example, in Java you would use “final private int someNumber.”
On the other hand, if the initial value of a property should be variable, you can let the client provide a value for it as a constructor argument. By adding a parameter to the constructor, you force clients to provide a value when instantiating the class.
Listing 1.9. Adding a constructor argument
class Foo { private int someNumber; public function __construct(int initialNumber) { this.someNumber = initialNumber; } } object1 = new Foo(); // doesn't work 1 objec...

Table of contents