-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2024-09-05 17:12:06.587562 new snippets
- Loading branch information
1 parent
004b339
commit 551ca93
Showing
19 changed files
with
1,286 additions
and
410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//date: 2024-09-05T16:52:22Z | ||
//url: https://api.github.com/gists/b9c886d5f45c4da84e1e28746c104b79 | ||
//owner: https://api.github.com/users/trikitrok | ||
|
||
class MessageRouter { | ||
public void Route(Message message) { | ||
//!! ouch... x( | ||
ExternalRouter.getInstance().sendMessage(message); | ||
} | ||
} | ||
|
||
class ExternalRouter // another Singleton! x( | ||
{ | ||
private static ExternalRouter instance; | ||
|
||
private ExternalRouter() { | ||
// initialize stuff | ||
} | ||
|
||
public static ExternalRouter getInstance() { | ||
if (instance == null) { | ||
instance = new ExternalRouter(); | ||
} | ||
return instance; | ||
} | ||
|
||
// more code... | ||
public void sendMessage(Message message) { | ||
// interesting code to send the message | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//date: 2024-09-05T17:06:13Z | ||
//url: https://api.github.com/gists/6c259d3e1c1c3d3c0e02c580cbbdf1c5 | ||
//owner: https://api.github.com/users/trikitrok | ||
|
||
class MessageRouter { | ||
public void Route(Message message) { | ||
//!! ouch... x( | ||
ExternalRouter.getInstance().sendMessage(message); | ||
} | ||
} | ||
|
||
class ExternalRouter // another Singleton! x( | ||
{ | ||
private static ExternalRouter instance; | ||
|
||
private ExternalRouter() { | ||
// initialize stuff | ||
} | ||
|
||
public static ExternalRouter getInstance() { | ||
if (instance == null) { | ||
instance = new ExternalRouter(); | ||
} | ||
return instance; | ||
} | ||
|
||
//!! Added for testing purposes only, do not use this in production code | ||
public static void setInstanceForTesting(ExternalRouter anInstance) { | ||
instance = anInstance; | ||
} | ||
|
||
// more code... | ||
public void sendMessage(Message message) { | ||
// interesting code to send the message | ||
} | ||
} | ||
|
||
///////////////////////////////////////////// | ||
// In some test we use the static setter to | ||
// set a test double so that we can control | ||
// what the singleton's instance does | ||
|
||
class MessageRouterTest | ||
{ | ||
@Test | ||
public void routes_message() | ||
{ | ||
ExternalRouter externalRouter = mock(ExternalRouter.class); | ||
ExternalRouter.setInstanceForTesting(externalRouter); | ||
MessageRouter messageRouter = new MessageRouter(); | ||
Message message = new Message(); | ||
|
||
messageRouter.Route(message); | ||
|
||
verify(externalRouter).sendMessage(message); | ||
} | ||
|
||
// some other tests... | ||
|
||
@AfterEach | ||
public void TearDown() | ||
{ | ||
// to keep tests isolated | ||
ExternalRouter.setInstanceForTesting(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//date: 2024-09-05T16:46:15Z | ||
//url: https://api.github.com/gists/278e3df2a2f14aff40d3162dd3cef94c | ||
//owner: https://api.github.com/users/trikitrok | ||
|
||
// After applying Subclass & Override Method | ||
|
||
class RegisterSale { | ||
private List<Item> items; | ||
// more code... | ||
|
||
public void addItem(Barcode code) { | ||
// using the Singleton!! x( | ||
Item newItem = getInventory().getItemForBarCode(code); | ||
items.add(newItem); | ||
} | ||
|
||
protected Inventory getInventory() { | ||
return Inventory.GetInstance(); | ||
} | ||
|
||
// more code... | ||
} | ||
|
||
///////////////////////////// | ||
|
||
// In some test | ||
|
||
public class RegisterSaleTest { | ||
|
||
@Test | ||
public void Adds_An_Item() { | ||
Barcode code = new Barcode(); | ||
// some more setup code | ||
//... | ||
// we subclass & override the getter and return a test double of Inventory | ||
Inventory inventory = mock(Inventory.class); | ||
|
||
when(inventory.getItemForBarCode(code)).thenReturn(AnItem().withBarcode(code).build()); | ||
RegisterSaleForTesting registerSale = new RegisterSaleForTesting(inventory); | ||
|
||
// rest of the test... | ||
} | ||
|
||
public class RegisterSaleForTesting extends RegisterSale { | ||
private final Inventory inventory; | ||
|
||
public RegisterSaleForTesting(Inventory inventory) { | ||
this.inventory = inventory; | ||
} | ||
|
||
// overriden to separate from the singleton | ||
@Override | ||
protected Inventory getInventory() { | ||
return inventory; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.