modelit 0.2.4__tar.gz → 0.2.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {modelit-0.2.4 → modelit-0.2.6}/CONTRIBUTING.md +3 -0
- modelit-0.2.6/MANIFEST.in +1 -0
- {modelit-0.2.4 → modelit-0.2.6}/PKG-INFO +17 -1
- {modelit-0.2.4 → modelit-0.2.6}/README.md +16 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit/cli.py +1 -1
- modelit-0.2.6/modelit/registry.py +92 -0
- modelit-0.2.6/modelit/templates/abstract_factory_pattern/template.java +44 -0
- modelit-0.2.6/modelit/templates/adapter_pattern/template.java +25 -0
- modelit-0.2.6/modelit/templates/advanced_exceptions/template.java +42 -0
- modelit-0.2.6/modelit/templates/assertions/template.java +19 -0
- modelit-0.2.6/modelit/templates/bridge_pattern/template.java +54 -0
- modelit-0.2.6/modelit/templates/builder_pattern/template.java +58 -0
- modelit-0.2.6/modelit/templates/command_pattern/template.java +58 -0
- modelit-0.2.6/modelit/templates/composite_pattern/template.java +53 -0
- modelit-0.2.6/modelit/templates/decorator_pattern/template.java +39 -0
- modelit-0.2.6/modelit/templates/diffusion/template.py +231 -0
- modelit-0.2.6/modelit/templates/facade_pattern/template.java +42 -0
- modelit-0.2.6/modelit/templates/factory_pattern/template.java +36 -0
- modelit-0.2.6/modelit/templates/flyweight_pattern/template.java +47 -0
- modelit-0.2.6/modelit/templates/gan/template.py +233 -0
- modelit-0.2.6/modelit/templates/interpreter_pattern/template.java +36 -0
- modelit-0.2.6/modelit/templates/java_default_methods/template.java +38 -0
- modelit-0.2.6/modelit/templates/jca_digital_signature/template.java +32 -0
- modelit-0.2.6/modelit/templates/jca_encryption/template.java +33 -0
- modelit-0.2.6/modelit/templates/jca_hashing/template.java +26 -0
- modelit-0.2.6/modelit/templates/jca_hmac/template.java +31 -0
- modelit-0.2.6/modelit/templates/jca_mac/template.java +30 -0
- modelit-0.2.6/modelit/templates/jca_rsa/template.java +31 -0
- modelit-0.2.6/modelit/templates/jca_secure_random/template.java +19 -0
- modelit-0.2.6/modelit/templates/lstm/template.py +390 -0
- modelit-0.2.6/modelit/templates/mediator_pattern/template.java +62 -0
- modelit-0.2.6/modelit/templates/memento_pattern/template.java +50 -0
- modelit-0.2.6/modelit/templates/observer_pattern/template.java +47 -0
- modelit-0.2.6/modelit/templates/pca_ae/template.py +226 -0
- modelit-0.2.6/modelit/templates/prototype_pattern/template.java +38 -0
- modelit-0.2.6/modelit/templates/proxy_pattern/template.java +48 -0
- modelit-0.2.6/modelit/templates/rnn/template.py +392 -0
- modelit-0.2.6/modelit/templates/sofm/template.py +90 -0
- modelit-0.2.6/modelit/templates/solid_notification/EmailSender.java +6 -0
- modelit-0.2.6/modelit/templates/solid_notification/Main.java +24 -0
- modelit-0.2.6/modelit/templates/solid_notification/NotificationSender.java +3 -0
- modelit-0.2.6/modelit/templates/solid_notification/NotificationService.java +11 -0
- modelit-0.2.6/modelit/templates/solid_notification/PushSender.java +6 -0
- modelit-0.2.6/modelit/templates/solid_notification/SlackSender.java +6 -0
- modelit-0.2.6/modelit/templates/solid_notification/SmsSender.java +6 -0
- modelit-0.2.6/modelit/templates/solid_shapes/AreaCalculator.java +14 -0
- modelit-0.2.6/modelit/templates/solid_shapes/Circle.java +12 -0
- modelit-0.2.6/modelit/templates/solid_shapes/Main.java +20 -0
- modelit-0.2.6/modelit/templates/solid_shapes/Pentagon.java +14 -0
- modelit-0.2.6/modelit/templates/solid_shapes/Rectangle.java +14 -0
- modelit-0.2.6/modelit/templates/solid_shapes/Shape.java +3 -0
- modelit-0.2.6/modelit/templates/solid_shapes/Triangle.java +14 -0
- modelit-0.2.6/modelit/templates/state_pattern/template.java +40 -0
- modelit-0.2.6/modelit/templates/streams_employee/Employee.java +23 -0
- modelit-0.2.6/modelit/templates/streams_employee/Main.java +57 -0
- modelit-0.2.6/modelit/templates/streams_word_freq/template.java +59 -0
- modelit-0.2.6/modelit/templates/template_pattern/template.java +40 -0
- modelit-0.2.6/modelit/templates/thread_deadlock/template.java +40 -0
- modelit-0.2.6/modelit/templates/thread_deadlock_solved/template.java +54 -0
- modelit-0.2.6/modelit/templates/vae/template.py +322 -0
- modelit-0.2.6/modelit/templates/visitor_pattern/template.java +42 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit.egg-info/PKG-INFO +17 -1
- modelit-0.2.6/modelit.egg-info/SOURCES.txt +75 -0
- {modelit-0.2.4 → modelit-0.2.6}/pyproject.toml +1 -1
- modelit-0.2.4/MANIFEST.in +0 -1
- modelit-0.2.4/modelit/registry.py +0 -74
- modelit-0.2.4/modelit.egg-info/SOURCES.txt +0 -20
- {modelit-0.2.4 → modelit-0.2.6}/.github/workflows/publish.yml +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/.github/workflows/test.yml +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/.gitignore +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/LICENSE +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit/__init__.py +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit/__main__.py +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit/templates/backpropagation/template.py +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit/templates/cnn/template.py +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit/templates/perceptron/template.py +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit.egg-info/dependency_links.txt +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit.egg-info/entry_points.txt +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/modelit.egg-info/top_level.txt +0 -0
- {modelit-0.2.4 → modelit-0.2.6}/setup.cfg +0 -0
|
@@ -17,6 +17,8 @@ modelit/templates/<name>/
|
|
|
17
17
|
modelit/templates/<name>/template.py
|
|
18
18
|
```
|
|
19
19
|
|
|
20
|
+
For multi-file templates, add more files in the same folder.
|
|
21
|
+
|
|
20
22
|
4. Make sure the folder name is the function name.
|
|
21
23
|
5. Test it locally:
|
|
22
24
|
|
|
@@ -39,6 +41,7 @@ modelit create <name>
|
|
|
39
41
|
- Do not add `metadata.json`.
|
|
40
42
|
- Keep file names lowercase.
|
|
41
43
|
- Prefer clean, beginner-friendly code.
|
|
44
|
+
- If a template has multiple files, `--output` should be treated as a directory.
|
|
42
45
|
|
|
43
46
|
## Pull request flow
|
|
44
47
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
recursive-include modelit/templates *.py *.java *.json
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: modelit
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.6
|
|
4
4
|
Summary: Local-first ML starter templates you can print or save
|
|
5
5
|
Author: Yashsmith
|
|
6
6
|
License-Expression: MIT
|
|
@@ -28,6 +28,8 @@ ModelIt is a tiny Python package for storing your ML/DL boilerplate templates.
|
|
|
28
28
|
- `perceptron()` prints the full code
|
|
29
29
|
- `perceptron(output="code1.py")` saves it to a file
|
|
30
30
|
- `modelit create perceptron` works from the terminal
|
|
31
|
+
- single-file templates write to a file
|
|
32
|
+
- multi-file templates write to a folder
|
|
31
33
|
|
|
32
34
|
## Install
|
|
33
35
|
|
|
@@ -86,8 +88,16 @@ modelit/templates/mycode/
|
|
|
86
88
|
modelit/templates/mycode/template.py
|
|
87
89
|
```
|
|
88
90
|
|
|
91
|
+
For Java, use:
|
|
92
|
+
|
|
93
|
+
```text
|
|
94
|
+
modelit/templates/mycode/template.java
|
|
95
|
+
```
|
|
96
|
+
|
|
89
97
|
3. Done. The folder name becomes the function name.
|
|
90
98
|
|
|
99
|
+
If a template has more than one file, `--output` must be a directory path.
|
|
100
|
+
|
|
91
101
|
That means this will work automatically:
|
|
92
102
|
|
|
93
103
|
```python
|
|
@@ -103,6 +113,12 @@ modelit create mycode
|
|
|
103
113
|
modelit create mycode --output mycode.py
|
|
104
114
|
```
|
|
105
115
|
|
|
116
|
+
For multi-file templates:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
modelit create solid_notification --output out/solid_notification
|
|
120
|
+
```
|
|
121
|
+
|
|
106
122
|
## Publish flow
|
|
107
123
|
|
|
108
124
|
1. Add a new template folder.
|
|
@@ -8,6 +8,8 @@ ModelIt is a tiny Python package for storing your ML/DL boilerplate templates.
|
|
|
8
8
|
- `perceptron()` prints the full code
|
|
9
9
|
- `perceptron(output="code1.py")` saves it to a file
|
|
10
10
|
- `modelit create perceptron` works from the terminal
|
|
11
|
+
- single-file templates write to a file
|
|
12
|
+
- multi-file templates write to a folder
|
|
11
13
|
|
|
12
14
|
## Install
|
|
13
15
|
|
|
@@ -66,8 +68,16 @@ modelit/templates/mycode/
|
|
|
66
68
|
modelit/templates/mycode/template.py
|
|
67
69
|
```
|
|
68
70
|
|
|
71
|
+
For Java, use:
|
|
72
|
+
|
|
73
|
+
```text
|
|
74
|
+
modelit/templates/mycode/template.java
|
|
75
|
+
```
|
|
76
|
+
|
|
69
77
|
3. Done. The folder name becomes the function name.
|
|
70
78
|
|
|
79
|
+
If a template has more than one file, `--output` must be a directory path.
|
|
80
|
+
|
|
71
81
|
That means this will work automatically:
|
|
72
82
|
|
|
73
83
|
```python
|
|
@@ -83,6 +93,12 @@ modelit create mycode
|
|
|
83
93
|
modelit create mycode --output mycode.py
|
|
84
94
|
```
|
|
85
95
|
|
|
96
|
+
For multi-file templates:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
modelit create solid_notification --output out/solid_notification
|
|
100
|
+
```
|
|
101
|
+
|
|
86
102
|
## Publish flow
|
|
87
103
|
|
|
88
104
|
1. Add a new template folder.
|
|
@@ -17,7 +17,7 @@ def main(argv: list[str] | None = None) -> None:
|
|
|
17
17
|
|
|
18
18
|
create_parser = subparsers.add_parser("create", help="Print or save a template")
|
|
19
19
|
create_parser.add_argument("name", choices=available_models(), help="Template name")
|
|
20
|
-
create_parser.add_argument("-o", "--output", help="Write
|
|
20
|
+
create_parser.add_argument("-o", "--output", help="Write to a file for single-file templates or a directory for multi-file templates")
|
|
21
21
|
create_parser.set_defaults(func=_create)
|
|
22
22
|
|
|
23
23
|
args = parser.parse_args(argv)
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Template discovery and loading."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from importlib.resources import files
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
PACKAGE_NAME = "modelit"
|
|
10
|
+
TEMPLATES_DIR = "templates"
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class TemplateInfo:
|
|
14
|
+
name: str
|
|
15
|
+
|
|
16
|
+
def _templates_root():
|
|
17
|
+
return files(PACKAGE_NAME).joinpath(TEMPLATES_DIR)
|
|
18
|
+
|
|
19
|
+
def _template_dir(name: str):
|
|
20
|
+
return _templates_root().joinpath(name)
|
|
21
|
+
|
|
22
|
+
def available_models() -> tuple[str, ...]:
|
|
23
|
+
root = _templates_root()
|
|
24
|
+
if not root.is_dir():
|
|
25
|
+
return ()
|
|
26
|
+
|
|
27
|
+
names: list[str] = []
|
|
28
|
+
for child in root.iterdir():
|
|
29
|
+
if child.is_dir() and not child.name.startswith("__"):
|
|
30
|
+
if any(child.iterdir()):
|
|
31
|
+
names.append(child.name)
|
|
32
|
+
return tuple(sorted(names))
|
|
33
|
+
|
|
34
|
+
def load_metadata(name: str) -> TemplateInfo:
|
|
35
|
+
return TemplateInfo(name=name)
|
|
36
|
+
|
|
37
|
+
def load_template_files(name: str) -> dict[str, str]:
|
|
38
|
+
target_dir = _template_dir(name)
|
|
39
|
+
if not target_dir.is_dir():
|
|
40
|
+
raise FileNotFoundError(f"Missing template directory for {name!r}")
|
|
41
|
+
|
|
42
|
+
file_contents = {}
|
|
43
|
+
for child in target_dir.iterdir():
|
|
44
|
+
if child.is_file() and not child.name.startswith("__"):
|
|
45
|
+
file_contents[child.name] = child.read_text(encoding="utf-8")
|
|
46
|
+
return file_contents
|
|
47
|
+
|
|
48
|
+
def build_template_callable(name: str):
|
|
49
|
+
files_dict = load_template_files(name)
|
|
50
|
+
info = load_metadata(name)
|
|
51
|
+
|
|
52
|
+
is_single_file = len(files_dict) == 1
|
|
53
|
+
default_filename = list(files_dict.keys())[0] if is_single_file else name
|
|
54
|
+
|
|
55
|
+
def runner(output: str | None = None) -> None:
|
|
56
|
+
if output:
|
|
57
|
+
out_path = Path(output)
|
|
58
|
+
|
|
59
|
+
if is_single_file:
|
|
60
|
+
if out_path.exists():
|
|
61
|
+
raise FileExistsError(f"Output path already exists: {out_path}")
|
|
62
|
+
out_path.parent.mkdir(parents=True, exist_ok=True)
|
|
63
|
+
out_path.write_text(list(files_dict.values())[0], encoding="utf-8")
|
|
64
|
+
print(f"Generated {out_path}")
|
|
65
|
+
else:
|
|
66
|
+
if out_path.suffix:
|
|
67
|
+
raise ValueError("Multi-file templates require a directory output path")
|
|
68
|
+
if out_path.exists() and not out_path.is_dir():
|
|
69
|
+
raise FileExistsError(f"Output path already exists and is not a directory: {out_path}")
|
|
70
|
+
out_path.mkdir(parents=True, exist_ok=True)
|
|
71
|
+
for fname, content in files_dict.items():
|
|
72
|
+
file_path = out_path / fname
|
|
73
|
+
if file_path.exists():
|
|
74
|
+
print(f"Skipping {file_path} (already exists)")
|
|
75
|
+
continue
|
|
76
|
+
file_path.write_text(content, encoding="utf-8")
|
|
77
|
+
print(f"Generated {file_path}")
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
# If no output is specified, print to terminal
|
|
81
|
+
for fname, content in files_dict.items():
|
|
82
|
+
if not is_single_file:
|
|
83
|
+
print(f"\n{'='*40}\nFile: {fname}\n{'='*40}")
|
|
84
|
+
print(content, end="\n\n" if not is_single_file else "")
|
|
85
|
+
|
|
86
|
+
runner.__name__ = name
|
|
87
|
+
runner.__qualname__ = name
|
|
88
|
+
runner.__module__ = "modelit"
|
|
89
|
+
runner.__doc__ = f"Print or save the {name} template."
|
|
90
|
+
runner.output_file = default_filename # type: ignore[attr-defined]
|
|
91
|
+
runner.template_info = info # type: ignore[attr-defined]
|
|
92
|
+
return runner
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
interface Button { void render(); }
|
|
2
|
+
interface Checkbox { void render(); }
|
|
3
|
+
|
|
4
|
+
class WinButton implements Button {
|
|
5
|
+
public void render() { System.out.println("Rendering Windows Button"); }
|
|
6
|
+
}
|
|
7
|
+
class MacButton implements Button {
|
|
8
|
+
public void render() { System.out.println("Rendering macOS Button"); }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class WinCheckbox implements Checkbox {
|
|
12
|
+
public void render() { System.out.println("Rendering Windows Checkbox"); }
|
|
13
|
+
}
|
|
14
|
+
class MacCheckbox implements Checkbox {
|
|
15
|
+
public void render() { System.out.println("Rendering macOS Checkbox"); }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface GUIFactory {
|
|
19
|
+
Button createButton();
|
|
20
|
+
Checkbox createCheckbox();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class WindowsFactory implements GUIFactory {
|
|
24
|
+
public Button createButton() { return new WinButton(); }
|
|
25
|
+
public Checkbox createCheckbox() { return new WinCheckbox(); }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
class MacOSFactory implements GUIFactory {
|
|
29
|
+
public Button createButton() { return new MacButton(); }
|
|
30
|
+
public Checkbox createCheckbox() { return new MacCheckbox(); }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public class AbstractFactoryDemo {
|
|
34
|
+
public static void main(String[] args) {
|
|
35
|
+
String os = "Mac";
|
|
36
|
+
GUIFactory factory = os.equals("Mac") ? new MacOSFactory() : new WindowsFactory();
|
|
37
|
+
|
|
38
|
+
Button button = factory.createButton();
|
|
39
|
+
Checkbox checkbox = factory.createCheckbox();
|
|
40
|
+
|
|
41
|
+
button.render();
|
|
42
|
+
checkbox.render();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
interface PaymentProcessor {
|
|
2
|
+
void processPayment(double amount);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
class StripeService {
|
|
6
|
+
public void makePayment(double amountInCents) {
|
|
7
|
+
System.out.println("Stripe: Processing payment of " + amountInCents + " cents successfully.");
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class StripeAdapter implements PaymentProcessor {
|
|
12
|
+
private StripeService stripeService = new StripeService();
|
|
13
|
+
|
|
14
|
+
public void processPayment(double amount) {
|
|
15
|
+
double amountInCents = amount * 100;
|
|
16
|
+
stripeService.makePayment(amountInCents);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public class AdapterDemo {
|
|
21
|
+
public static void main(String[] args) {
|
|
22
|
+
PaymentProcessor processor = new StripeAdapter();
|
|
23
|
+
processor.processPayment(50.00);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import java.io.BufferedReader;
|
|
2
|
+
import java.io.FileReader;
|
|
3
|
+
import java.io.IOException;
|
|
4
|
+
|
|
5
|
+
class InsufficientFundsException extends Exception {
|
|
6
|
+
public InsufficientFundsException(String message) {
|
|
7
|
+
super(message);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public class AdvancedExceptionDemo {
|
|
12
|
+
|
|
13
|
+
public static void processTransfer(int accountBalance, int transferAmount) throws InsufficientFundsException {
|
|
14
|
+
if (transferAmount > accountBalance) {
|
|
15
|
+
throw new InsufficientFundsException("Transfer failed: Attempted to send $" + transferAmount + " but balance is $" + accountBalance);
|
|
16
|
+
}
|
|
17
|
+
System.out.println("Transfer of $" + transferAmount + " completed successfully.");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public static void main(String[] args) {
|
|
21
|
+
try {
|
|
22
|
+
processTransfer(5000, 7500);
|
|
23
|
+
} catch (InsufficientFundsException e) {
|
|
24
|
+
System.err.println("Business Error: " + e.getMessage());
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
System.out.println("-----------------");
|
|
28
|
+
|
|
29
|
+
String filePath = "non_existent_transactions.csv";
|
|
30
|
+
|
|
31
|
+
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
|
|
32
|
+
System.out.println(reader.readLine());
|
|
33
|
+
int value = Integer.parseInt("Not_A_Number");
|
|
34
|
+
|
|
35
|
+
} catch (IOException | NumberFormatException e) {
|
|
36
|
+
System.err.println("System Failure: Could not process file or format was corrupted.");
|
|
37
|
+
System.err.println("Root Cause: " + e.getClass().getSimpleName() + " -> " + e.getMessage());
|
|
38
|
+
} finally {
|
|
39
|
+
System.out.println("Cleanup executed. System remains stable.");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
class DiscountCalculator {
|
|
2
|
+
public static double applyDiscount(double originalPrice, double discountPercentage) {
|
|
3
|
+
double discountAmount = originalPrice * (discountPercentage / 100);
|
|
4
|
+
double finalPrice = originalPrice - discountAmount;
|
|
5
|
+
|
|
6
|
+
assert finalPrice >= 0 : "CRITICAL LOGIC ERROR: Final price fell below zero!";
|
|
7
|
+
|
|
8
|
+
return finalPrice;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public class AssertionDemo {
|
|
13
|
+
public static void main(String[] args) {
|
|
14
|
+
System.out.println("Final Price: $" + DiscountCalculator.applyDiscount(1000, 20));
|
|
15
|
+
|
|
16
|
+
System.out.println("Testing invalid logic...");
|
|
17
|
+
System.out.println("Final Price: $" + DiscountCalculator.applyDiscount(500, 150));
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
interface Device {
|
|
2
|
+
void turnOn();
|
|
3
|
+
void turnOff();
|
|
4
|
+
void setVolume(int percent);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
class SmartTV implements Device {
|
|
8
|
+
private int volume = 30;
|
|
9
|
+
public void turnOn() { System.out.println("Smart TV is ON"); }
|
|
10
|
+
public void turnOff() { System.out.println("Smart TV is OFF"); }
|
|
11
|
+
public void setVolume(int percent) {
|
|
12
|
+
volume = percent;
|
|
13
|
+
System.out.println("Smart TV Volume set to " + volume);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
abstract class RemoteControl {
|
|
18
|
+
protected Device device;
|
|
19
|
+
public RemoteControl(Device device) { this.device = device; }
|
|
20
|
+
public abstract void togglePower();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class BasicRemote extends RemoteControl {
|
|
24
|
+
public BasicRemote(Device device) { super(device); }
|
|
25
|
+
public void togglePower() {
|
|
26
|
+
System.out.println("\n[Basic Remote Used]");
|
|
27
|
+
device.turnOn();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class AdvancedRemote extends RemoteControl {
|
|
32
|
+
public AdvancedRemote(Device device) { super(device); }
|
|
33
|
+
public void togglePower() {
|
|
34
|
+
System.out.println("\n[Advanced Remote Used]");
|
|
35
|
+
device.turnOn();
|
|
36
|
+
}
|
|
37
|
+
public void mute() {
|
|
38
|
+
System.out.println("Muting Device...");
|
|
39
|
+
device.setVolume(0);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
public class BridgeDemo {
|
|
44
|
+
public static void main(String[] args) {
|
|
45
|
+
Device tv = new SmartTV();
|
|
46
|
+
|
|
47
|
+
BasicRemote basicRemote = new BasicRemote(tv);
|
|
48
|
+
basicRemote.togglePower();
|
|
49
|
+
|
|
50
|
+
AdvancedRemote advancedRemote = new AdvancedRemote(tv);
|
|
51
|
+
advancedRemote.togglePower();
|
|
52
|
+
advancedRemote.mute();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
class UserProfile {
|
|
2
|
+
private final String username;
|
|
3
|
+
private final String email;
|
|
4
|
+
private final int age;
|
|
5
|
+
private final String phone;
|
|
6
|
+
|
|
7
|
+
private UserProfile(Builder builder) {
|
|
8
|
+
this.username = builder.username;
|
|
9
|
+
this.email = builder.email;
|
|
10
|
+
this.age = builder.age;
|
|
11
|
+
this.phone = builder.phone;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
public void showProfile() {
|
|
15
|
+
System.out.println("User: " + username + " | Email: " + email + " | Age: " + age + " | Phone: " + phone);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static class Builder {
|
|
19
|
+
private String username;
|
|
20
|
+
private String email;
|
|
21
|
+
private int age = 0;
|
|
22
|
+
private String phone = "N/A";
|
|
23
|
+
|
|
24
|
+
public Builder(String username, String email) {
|
|
25
|
+
this.username = username;
|
|
26
|
+
this.email = email;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public Builder setAge(int age) {
|
|
30
|
+
this.age = age;
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public Builder setPhone(String phone) {
|
|
35
|
+
this.phone = phone;
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public UserProfile build() {
|
|
40
|
+
return new UserProfile(this);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public class BuilderDemo {
|
|
46
|
+
public static void main(String[] args) {
|
|
47
|
+
UserProfile user1 = new UserProfile.Builder("user", "user@example.com")
|
|
48
|
+
.setAge(21)
|
|
49
|
+
.build();
|
|
50
|
+
|
|
51
|
+
UserProfile user2 = new UserProfile.Builder("admin_user", "admin@sys.com")
|
|
52
|
+
.setPhone("+91-9876543210")
|
|
53
|
+
.build();
|
|
54
|
+
|
|
55
|
+
user1.showProfile();
|
|
56
|
+
user2.showProfile();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import java.util.Stack;
|
|
2
|
+
|
|
3
|
+
class BankAccount {
|
|
4
|
+
private int balance = 1000;
|
|
5
|
+
public void deposit(int amount) {
|
|
6
|
+
balance += amount;
|
|
7
|
+
System.out.println("Deposited $" + amount + ". Current Balance: $" + balance);
|
|
8
|
+
}
|
|
9
|
+
public void withdraw(int amount) {
|
|
10
|
+
balance -= amount;
|
|
11
|
+
System.out.println("Withdrew $" + amount + ". Current Balance: $" + balance);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface TransactionCommand {
|
|
16
|
+
void execute();
|
|
17
|
+
void undo();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class DepositCommand implements TransactionCommand {
|
|
21
|
+
private BankAccount account;
|
|
22
|
+
private int amount;
|
|
23
|
+
|
|
24
|
+
public DepositCommand(BankAccount account, int amount) {
|
|
25
|
+
this.account = account;
|
|
26
|
+
this.amount = amount;
|
|
27
|
+
}
|
|
28
|
+
public void execute() { account.deposit(amount); }
|
|
29
|
+
public void undo() { account.withdraw(amount); }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class TransactionManager {
|
|
33
|
+
private Stack<TransactionCommand> history = new Stack<>();
|
|
34
|
+
|
|
35
|
+
public void executeTransaction(TransactionCommand command) {
|
|
36
|
+
command.execute();
|
|
37
|
+
history.push(command);
|
|
38
|
+
}
|
|
39
|
+
public void undoLastTransaction() {
|
|
40
|
+
if (!history.isEmpty()) {
|
|
41
|
+
System.out.print("Undoing last transaction... ");
|
|
42
|
+
TransactionCommand lastCommand = history.pop();
|
|
43
|
+
lastCommand.undo();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public class CommandDemo {
|
|
49
|
+
public static void main(String[] args) {
|
|
50
|
+
BankAccount myAccount = new BankAccount();
|
|
51
|
+
TransactionManager manager = new TransactionManager();
|
|
52
|
+
|
|
53
|
+
manager.executeTransaction(new DepositCommand(myAccount, 500));
|
|
54
|
+
manager.executeTransaction(new DepositCommand(myAccount, 200));
|
|
55
|
+
|
|
56
|
+
manager.undoLastTransaction();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import java.util.ArrayList;
|
|
2
|
+
import java.util.List;
|
|
3
|
+
|
|
4
|
+
interface FileSystemComponent {
|
|
5
|
+
void showDetails();
|
|
6
|
+
int getSize();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class File implements FileSystemComponent {
|
|
10
|
+
private String name;
|
|
11
|
+
private int size;
|
|
12
|
+
|
|
13
|
+
public File(String name, int size) { this.name = name; this.size = size; }
|
|
14
|
+
public void showDetails() { System.out.println(" File: " + name + " (" + size + " KB)"); }
|
|
15
|
+
public int getSize() { return size; }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class Directory implements FileSystemComponent {
|
|
19
|
+
private String name;
|
|
20
|
+
private List<FileSystemComponent> components = new ArrayList<>();
|
|
21
|
+
|
|
22
|
+
public Directory(String name) { this.name = name; }
|
|
23
|
+
public void addComponent(FileSystemComponent component) { components.add(component); }
|
|
24
|
+
|
|
25
|
+
public void showDetails() {
|
|
26
|
+
System.out.println("Directory: " + name + " | Total Size: " + getSize() + " KB");
|
|
27
|
+
for (FileSystemComponent component : components) {
|
|
28
|
+
component.showDetails();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public int getSize() {
|
|
33
|
+
return components.stream().mapToInt(FileSystemComponent::getSize).sum();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public class CompositeDemo {
|
|
38
|
+
public static void main(String[] args) {
|
|
39
|
+
File file1 = new File("resume.pdf", 500);
|
|
40
|
+
File file2 = new File("photo.png", 1500);
|
|
41
|
+
File file3 = new File("system_config.txt", 15);
|
|
42
|
+
|
|
43
|
+
Directory personalDir = new Directory("Personal Files");
|
|
44
|
+
personalDir.addComponent(file1);
|
|
45
|
+
personalDir.addComponent(file2);
|
|
46
|
+
|
|
47
|
+
Directory rootDir = new Directory("Root");
|
|
48
|
+
rootDir.addComponent(personalDir);
|
|
49
|
+
rootDir.addComponent(file3);
|
|
50
|
+
|
|
51
|
+
rootDir.showDetails();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
interface Coffee {
|
|
2
|
+
String getDescription();
|
|
3
|
+
double getCost();
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
class Espresso implements Coffee {
|
|
7
|
+
public String getDescription() { return "Espresso"; }
|
|
8
|
+
public double getCost() { return 150.0; }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
abstract class CoffeeDecorator implements Coffee {
|
|
12
|
+
protected Coffee decoratedCoffee;
|
|
13
|
+
public CoffeeDecorator(Coffee coffee) { this.decoratedCoffee = coffee; }
|
|
14
|
+
public String getDescription() { return decoratedCoffee.getDescription(); }
|
|
15
|
+
public double getCost() { return decoratedCoffee.getCost(); }
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class MilkDecorator extends CoffeeDecorator {
|
|
19
|
+
public MilkDecorator(Coffee coffee) { super(coffee); }
|
|
20
|
+
public String getDescription() { return super.getDescription() + ", Steamed Milk"; }
|
|
21
|
+
public double getCost() { return super.getCost() + 30.0; }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
class CaramelDecorator extends CoffeeDecorator {
|
|
25
|
+
public CaramelDecorator(Coffee coffee) { super(coffee); }
|
|
26
|
+
public String getDescription() { return super.getDescription() + ", Caramel Syrup"; }
|
|
27
|
+
public double getCost() { return super.getCost() + 45.0; }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public class DecoratorDemo {
|
|
31
|
+
public static void main(String[] args) {
|
|
32
|
+
Coffee myOrder = new Espresso();
|
|
33
|
+
myOrder = new MilkDecorator(myOrder);
|
|
34
|
+
myOrder = new CaramelDecorator(myOrder);
|
|
35
|
+
|
|
36
|
+
System.out.println("Order Details: " + myOrder.getDescription());
|
|
37
|
+
System.out.println("Total Cost: ₹" + myOrder.getCost());
|
|
38
|
+
}
|
|
39
|
+
}
|