Chapter 30 Flashcards

(21 cards)

1
Q

Advantages of Singleton

A

-You can be sure that a class has only a single instance.

-You gain a global access point to that instance

-The singleton object is initialized only when it’s requested for the first time

-You can change any class into a Singleton simply by making its constructors private and adding the appropriate static functions and variable.

  • If the Singleton is never used, it is never created.

-It is possible to extend the Singleton class through sub-classes.

  • It is possible to permit a variable number of instance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Creational design pattern

A

They are about the creation (instantiation) of objects in such a way that the system remains flexible and afford code reuse.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Singleton

A

It assures that there is one and only one instance of a class and provides a
global point of access to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Creational design patterns

A
  • Singleton
  • Prototype
  • Factory method
  • Abstract factory
  • Builder
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The lock statement

A

prevents another thread to access the code inside the lock.

Another thread will stop execution when it encounters the lock and only continue when the lock is released by the first thread.

We say that the second thread is blocked

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Disadvantages of singleton

A

 The Singleton makes an instance global since the class is internal or public. Global is
bad.

 The Singleton pattern can mask bad design, for instance, when the components of the
program know too much about each other.

 The pattern requires special treatment in a multithreaded environment so that multiple
threads won’t create a singleton object several times.

 There is no good way to destroy or decommission a Singleton.

 Not inherited. A class derived from a Singleton is not a Singleton. If it needs to be a
Singleton, the static function and variable need to be added to it.

 Bad efficiency: Each call to GetInstance invokes the if statement. For most of those calls, the if statement is useless.

 Non-transparent: Users of a Singleton know that they are using it, because they must invoke the GetInstance method

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why we dont use a static class to ensure a single global instance of a variable

A
  1. A static class cannot be extended whereas a Singleton class can be extended.
  2. Some languages allow a static class to have unwanted instances whereas a Singleton class prevents it. (Not in C#, though.)
  3. A static class cannot be initialised with a state (using a constructor parameter), whereas a Singleton class can be.
  4. A static class is loaded automatically when the program or namespace containing the class is loaded.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

When to use the singleton pattern

A

when a class in your program should have just a single instance available across the scope as allowed by the class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The Prototype pattern

A

-It is used to create a duplicate object or clone of the current object.

It provides an interface for creating parts of a product.

This pattern is used when the creation of an object is costly or complex.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Value types

A

When we copy a value from one object to another, a different memory cell is created

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Reference types

A

-An assignment of one object to another creates another pointer to the same memory cell.

So, when the content of the first object changes, the content of the second one changes as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The MemberwiseClone() method

A
  • makes a shallow copy of the object.

-That means that value types
are copied to the new object, but for reference types, the variable in the new object points to the original value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When to use the prototype method

A

-when the creation of an object is costly or complex.

-when the creation of an object is costly or complex.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Advantages of The prototype pattern

A
  • You can clone objects without coupling to their concrete classes.
  • You can get rid of repeated initialization code in favour of cloning pre-built prototypes.
  • You can produce complex objects more conveniently.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Disadvantages of the Prototype pattern

A

Cloning complex objects that have circular references might be very tricky.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Factory Method Pattern

A

-It builds on the concept of a factory class by defining an interface or abstract class for the factory class and allow subclasses thereof (concrete creators) to decide which product class to instantiate.

-Different concrete creators create different product types.

17
Q

Advantages of the Factory Pattern Method

A
  • You avoid tight coupling between the creator and the concrete products. The code works only with the product interface.
  • Single Responsibility Principle. You can move the product creation code into one place in
    the program, making the code easier to support.
  • Open/Closed Principle. You can introduce new types of products into the program without
    breaking existing client code.
18
Q

Disadvantages of the Factory Method Pattern

A
  • The code may become more complicated since you need to introduce a lot of new subclasses to implement the pattern.
19
Q

When to use the factory Method Pattern

A

-when you don’t know beforehand the exact types and
dependencies of the objects your code should work with

-when you want to provide users of your library or framework with a way to extend its internal components

20
Q

Factory Pattern vs Builder Pattern ( Factory)

A

Every factory class creates an objects of a specific class.

21
Q

Factory Pattern vs Builder Pattern ( Builder)

A

The builder classes create objects of the same class but with different values for the
properties.