Object Design Style Guide
eBook - ePub

Object Design Style Guide

Matthias Noback

Partager le livre
  1. English
  2. ePUB (adapté aux mobiles)
  3. Disponible sur iOS et Android
eBook - ePub

Object Design Style Guide

Matthias Noback

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

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.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Object Design Style Guide est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Object Design Style Guide par Matthias Noback en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Object Oriented Programming. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2019
ISBN
9781617296857

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 des matiĂšres