public class Shelf {
private ArrayList<INotification> noticeList = new ArrayList<INotification>();
public void setNotification(INotificationnotification) {
noticeList.add(notification);
}
}
本当に見ますか?
Shelf.java
public class Shelf { // Colleague
private ArrayList<INotification> noticeList = new ArrayList<INotification>();
private String name;
private int stock = 0; // 在庫数
public Shelf(String name, int stock) {
this.name = name;
setStock(stock);
}
public void setNotification(INotificationnotification) {
noticeList.add(notification);
}
public String getName() {
return name;
}
public void setStock(int stock) { // 在庫数の変更
this.stock = stock;
}
public int getStock() { // 在庫状況の確認
return stock;
}
public void bought(int amount) { // お買い上げ
stock -= amount;
for (int i = 0 ; i < noticeList.size() ; i++)
((INotification)noticeList.get(i)).notice(this);
}
}
Staff.java
public class Staff implements INotification {
public void notice(Shelfshelf) {
System.out.println(shelf.getName() + "は残り" + shelf.getStock() + "個です。");
}
}
Enshu402.cpp
#include <cstdlib> // rand, srand使用
#include <ctime> // time使用
#include "Shelf.h"
#include "Customer.h"
#include "Manager.h"
#include "Staff.h"
const int SHELF_COUNT = 5; // 陳列棚数
Shelf* shelfList[] = {
new Shelf("陳列棚A", 30),
new Shelf("陳列棚B", 10),
new Shelf("陳列棚C", 20),
new Shelf("陳列棚D", 20),
new Shelf("陳列棚E", 50)
}; // 陳列棚(名前、商品数)5台
int main()
{
Managerm(SHELF_COUNT, shelfList); // 売り場主任
Staffs; // 売り場担当者
for (int i = 0; i < SHELF_COUNT; i++)
{
shelfList[i]->setNotice(&s); // 売り場担当者を通知先にする
shelfList[i]->setNotice(&m); // 売り場主任を通知先にする
}
Customerc(SHELF_COUNT, shelfList);
c.start(100 * (rand() % SHELF_COUNT));
while (true)
{
Sleep(1000);
if (m.getStatus() == false) break; // すべて売り切れ
}
c.stop();
for (int i = 0; i < SHELF_COUNT; i++)
delete shelfList[i];
return 0;
}
#pragma once
#include "Notification.h"
class Shelf;
class Staff : public Notification
{
public:
Staff(void);
virtual ~Staff(void);
void notice(Shelf*);
};
public class Shelf // Colleague
{
private INotificationnotice;
private string name;
private int stock = 0; // 在庫数
public Shelf(string name, int stock)
{
this.name = name;
setStock(stock);
}
public void setNotification(INotificationnotice)
{
this.notice = notice;
}
}
本当に見ますか?
Shelf.cs
public class Shelf // Colleague
{
private ArrayListnoticeList = new ArrayList();
private string name;
private int stock = 0; // 在庫数
public Shelf(string name, int stock)
{
this.name = name;
setStock(stock);
}
public void setNotification(INotificationnotice)
{
noticeList.Add(notice);
}
public string getName()
{
return name;
}
public void setStock(int stock) // 在庫数の変更
{
this.stock = stock;
}
public int getStock() // 在庫状況の確認
{
return stock;
}
public void bought(int amount) // お買い上げ
{
stock -= amount;
foreach (INotificationnotice in noticeList)
notice.notice(this);
}
}
Staff.cs
public class Staff : INotification {
public void notice(Shelfs)
{
Console.WriteLine(s.getName() + ":残り " + s.getStock());
}
}
Enshu402.vb
Module Enshu402
Private shelfList As Shelf() = New Shelf() {
New Shelf("陳列棚A", 30),
New Shelf("陳列棚B", 10),
New Shelf("陳列棚C", 20),
New Shelf("陳列棚D", 20),
New Shelf("陳列棚E", 50)
} ' 陳列棚(名前、商品数)5台
Sub Main()
Dim m As Manager = New Manager(shelfList) ' 売り場主任
Dim s As Staff = New Staff() ' 売り場担当者
For Each shelf In shelfListshelf.setNotification(s)
shelf.setNotification(m)
Next
Dim c As Customer = New Customer(shelfList)
c.start(100 * Fix(Rnd() * shelfList.Length))
Do While (True)
Threading.Thread.Sleep(1000)
If m.getStatus() = False Then Exit Do
Loop
c.stop()
End Sub
End Module
Public Class Shelf ' Colleague
Private noticeList As ArrayList = New ArrayList()
Private name As String
Private stock As Integer = 0 ' 在庫数
Public Sub New(name As String, stock As Integer)
MyClass.name = name
setStock(stock)
End Sub
Public Sub setNotification(notice As INotification)
noticeList.Add(notice)
End Sub
End Class
本当に見ますか?
Shelf.vb
Public Class Shelf ' Colleague
Private noticeList As ArrayList = New ArrayList()
Private name As String
Private stock As Integer = 0 ' 在庫数
Public Sub New(name As String, stock As Integer)
MyClass.name = name
setStock(stock)
End Sub
Public Sub setNotification(notice As INotification)
noticeList.Add(notice)
End Sub
Public Function getName() As String
Return name
End Function
Public Sub setStock(stock As Integer) ' 在庫数の変更
MyClass.stock = stock
End Sub
Public Function getStock() As Integer ' 在庫状況の確認
Return stock
End Function
Public Sub bought(amount As Integer) ' お買い上げ
stock -= amount For Each notice As INotification In noticeListnotice.notice(Me)
Next
End Sub
End Class
Staff.vb
Public Class Staff : Implements INotification {
Public Sub notice(s As Shelf) Implements INotification.notice
Console.WriteLine(s.getName() & ":残り " & s.getStock())
End Sub
End Class
Enshu402.js
const Shelf = require("./Shelf.js");
const Manager = require("./Manager.js");
const Staff = require("./Staff.js");
const Customer = require("./Customer.js");
let shelfList = new Array(
new Shelf("陳列棚A", 30),
new Shelf("陳列棚B", 10),
new Shelf("陳列棚C", 20),
new Shelf("陳列棚D", 20),
new Shelf("陳列棚E", 50)
); // 陳列棚(名前、商品数)5台
let manager = new Manager(shelfList); // 売り場主任
let staff = new Staff(); // 売り場担当者
for (let i = 0 ; i < shelfList.length ; i++) {
shelfList[i].setINotification(staff); // 通知先を売り場担当者にする
shelfList[i].setNotification(manager); // 通知先を売り場主任にする
}
let customer = new Customer(shelfList);
customer.start(); // Customer クラスの開始
use Shelf;
use Manager;
use Staff;
use Customer;
my @shelfList = (
new Shelf("陳列棚A", 30),
new Shelf("陳列棚B", 10),
new Shelf("陳列棚C", 20),
new Shelf("陳列棚D", 20),
new Shelf("陳列棚E", 50)
); # 陳列棚(名前、商品数)5台
my $manager = new Manager(\@shelfList); # 売り場主任
my $staff = new Staff(); # 売り場担当者
for (my $i = 0 ; $i < @shelfList ; $i++) {
@shelfList[$i]->setNotification($staff); # 通知先を売り場担当者にする
@shelfList[$i]->setNotification($manager); # 通知先を売り場主任にする
}
my $customer = new Customer(\@shelfList);
$customer->start(); # Customer クラスの開始
class Shelf
def initialize(name, stock)
@name = name@noticeList = []
setStock(stock)
end
def setNotification(notification)
@noticeList.push(notification)
end
end
本当に見ますか?
Shelf.rb
class Shelf
def initialize(name, stock)
@name = name@noticeList = []
setStock(stock)
end
def setNotification(notification)
@noticeList.push(notification)
end
def getName()
return @name
end
def setStock(stock) # 在庫数の変更
@stock = stock
end
def getStock() # 在庫状況の確認
return @stock
end
def bought(amount) # お買い上げ
@stock -= amount;
for target in @noticeListtarget.notice(self)
end
end
end
Staff.rb
class Staff
def notice(shelf)
puts "#{shelf.getName()}は残り#{shelf.getStock()}個です。"
end
end
Enshu402.py
from Shelf import Shelf
from Manager import Manager
from Meat import Meat
from Customer import Customer
shelfList = [
Shelf("陳列棚A", 30),
Shelf("陳列棚B", 10),
Shelf("陳列棚C", 20),
Shelf("陳列棚D", 20),
Shelf("陳列棚E", 50)
] # 陳列棚(名前、商品数)5台
manager = Manager(shelfList) # 売り場主任
staff = Staff() # 売り場担当者
for shelf in shelfList:
shelf.setNotification(shelf) # 通知先を売り場担当者にする
shelf.setNotification(manager) # 通知先を売り場主任にする
customer = Customer(shelfList)
customer.start()
class Shelf {
private $noticeList = array();
public function setNotification($notification) {
$this->noticeList[] = notification;
}
}
本当に見ますか?
Shelf.php
<?php
class Shelf { // Colleague
private static $no = 0;
private $noticeList = array();
private $notification;
private $id;
private $name;
private $stock = 0; // 在庫数
public function __construct($name, $stock) {
$this->id = Shelf::$no++;
$this->name = $name;
$this->setStock($stock);
}
public function setNotification($notification) {
$this->notification[] = $notification;
}
public function getName() {
return $this->name;
}
public function setStock($stock) { // 在庫数の変更
$shmid = shmop_open($this->id, "c", 0644, 4); // 共有メモリ
shmop_write($shmid, str_pad($stock, 2, 0, STR_PAD_LEFT), 0);
}
public function getStock() { // 在庫状況の確認
$shmid = shmop_open($this->id, "c", 0644, 4); // 共有メモリ
$stock = shmop_read($shmid, 0, 2);
return (int)$stock;
}
public function bought($amount) { // お買い上げ
$shmid = shmop_open($this->id, "c", 0644, 4); // 共有メモリ
$stock = shmop_read($shmid, 0, 2);
$stock = (int)$stock - $amount;
shmop_write($shmid, str_pad($stock, 2, 0, STR_PAD_LEFT), 0);
foreach($this->noticeList as $notification)
$notification->notice($this);
}
}
?>
Staff.php
<?php
class Staff implements INotification {
public function notice($shelf) {
print $shelf->getName() . "は残り" . $shelf->getStock() . "個です。\n";
}
}
?>
Enshu402.ts
import {Shelf} from "./Shelf";
import {Manager} from "./Manager";
import {Staff} from "./Staff";
import {Customer} from "./Customer";
private shelfList:Array<Shelf> = new Array<Shelf>(
new Shelf("陳列棚A", 30),
new Shelf("陳列棚B", 10),
new Shelf("陳列棚C", 20),
new Shelf("陳列棚D", 20),
new Shelf("陳列棚E", 50)
); // 陳列棚(名前、商品数)5台
let manager:Manager = new Manager(shelfList); // 売り場主任
let staff:Staff = new Staff(); // 売り場担当者
for (let i:number = 0 ; i < shelfList.length ; i++) {
shelfList[i].setNotification(staff); // 通知先を売り場担当者にする
shelfList[i].setNotification(manager); // 通知先を売り場主任にする
}
let customer:Customer = new Customer(shelfList);
customer.start(); // Customer クラスの開始
public class Shelf {
private var name:String
private var stock:Int = 0 // 在庫数
private var noticeList:[INotification] = []
public void setNotification(_ notification:INotification) {
self.noticeList.append(notification)
}
}
本当に見ますか?
Shelf.swift
public class Shelf { // Colleague
private var name:String
private var stock:Int = 0 // 在庫数
private var noticeList:[INotification] = []
public init(_ name:String, _ stock:Int) {
self.name = name
self.setStock(stock)
}
public func setNotification(_ notification:INotification) {
self.noticeList.append(notification)
}
public func getName() -> String {
return name
}
public func setStock(_ stock:Int) { // 在庫数の変更
self.stock = stock
}
public func getStock() -> Int { // 在庫状況の確認
return stock
}
public func bought(_ amount:Int) { // お買い上げ
stock -= amountnoticeList.forEach {
$0.notice(self)
}
}
}
Staff.swift
public class Staff : INotification {
public func notice(_ shelf:Shelf) {
print(shelf.getName() + "は残り" + shelf.getStock() + "個です。")
}
}
Enshu402.kt
fun main() {
val shelfList: Array<Shelf> = arrayOf(
Shelf("陳列棚A", 30),
Shelf("陳列棚B", 10),
Shelf("陳列棚C", 20),
Shelf("陳列棚D", 20),
Shelf("陳列棚E", 50)
) // 陳列棚(名前、商品数)5台
val manager = Manager(shelfList) // 売り場主任
val staff = Staff() // 売り場担当者
for (shelf in shelfList) {
shelf.setNotification(staff) // 通知先を売り場担当者にする
shelf.setNotification(manager) // 通知先を売り場主任にする
}
val customer = Customer(shelfList)
customer.start() // Customer クラスの開始
import inotification;
public class Shelf {
private INotification[] noticeList;
public void setNotification(INotificationnotification) {
noticeList ~= notification;
}
}
本当に見ますか?
shelf.d
import inotification;
public class Shelf { // Colleague
private INotification[] noticeList;
private string name;
private int stock = 0; // 在庫数
public this(in string name, in int stock) {
this.name = name;
setStock(stock);
}
public void setNotification(INotificationnotification) {
noticeList ~= notification;
}
public string getName() {
return name;
}
public void setStock(in int stock) { // 在庫数の変更
this.stock = stock;
}
public int getStock() { // 在庫状況の確認
return stock;
}
public void bought(in int amount) { // お買い上げ
stock -= amount;
foreach (int i; 0..noticeList.length)
noticeList[i].notice(this);
}
}
staff.d
import inotification;
import shelf;
public class Staff : INotification {
public void notice(Shelfshelf) {
printfln("%sは残り%d個です。", shelf.getName(), shelf.getStock(),);
}
}
private void printfln(T...)(T args) { // Shift JIS 出力
import std, std.windows.charset, core.vararg;
std.stdio.writeln(to!(string)(toMBSz(std.format.format(args))));
}
Enshu402.dpr
program Enshu402;
uses
System.SysUtils,
UnitCustomer,
UnitShelf,
UnitManager,
UnitStaff;
var shelfList:TShelfs;
var _manager:Manager;
var _staff:Staff;
var _customer:Customer;
var _shelf:Shelf;
var shelfList:TShelfs;
begin
shelfList := [
Shelf.Create('陳列棚A', 30),
Shelf.Create('陳列棚B', 10),
Shelf.Create('陳列棚C', 20),
Shelf.Create('陳列棚D', 20),
Shelf.Create('陳列棚E', 50)
]; // 陳列棚(名前、商品数)5台
_manager := Manager.Create(shelfList); // 売り場主任
_staff := Staff.Create(); // 売り場担当者
for _shelf in shelfList do begin
_shelf.setNotification(_staff);
_shelf.setNotification(_manager);
end;
_customer := Customer.Create(shelfList);
_customer.WaitFor();
_customer.Free;
for _shelf in shelfList do begin
_shelf.Free;
end;
end.
unit UnitShelf;
interface
uses
UnitINotification;
type
Shelf = class
private
var noticeList:TList<INotification>;
implementation
procedure Shelf.setNotification(notification:INotification);
begin
noticeList.Add(notification);
end;
end.
本当に見ますか?
Shelf.pas
unit UnitShelf;
interface
uses
UnitINotification;
type
Shelf = class
private
var noticeList:TList<INotification>;
var name:string;
var stock:integer;
public
constructor Create(name:string; stock:integer);
procedure setNotification(notification:INotification);
function getName():string;
procedure setStock(stock:integer); // 在庫数の変更
function getStock():integer; // 在庫状況の確認
procedure bought(amount:integer); // お買い上げ
end;
TShelfs = array of Shelf;
implementation
constructor Shelf.Create(name:string; stock:integer);
begin
self.name := name;
noticeList := TList<INotification>.Create();
setStock(stock);
end;
procedure Shelf.setNotification(notification:INotification);
begin
noticeList.Add(notification);
end;
function Shelf.getName():string;
begin
Result := name;
end;
procedure Shelf.setStock(stock:integer); // 在庫数の変更
begin
self.stock := stock;
end;
function Shelf.getStock():integer; // 在庫状況の確認
begin
Result := stock;
end;
procedure Shelf.bought(amount:integer); // お買い上げ
var notification:INotification;
begin
stock := stock - amount;
for notification in noticeList do begin
notification.notice(self);
end;
end;
end.
Staff.pas
unit UnitStaff;
interface
uses
System.SysUtils,
UnitINotification,
UnitShelf;
type
Staff = class(TInterfacedObject, INotification)
public
procedure notice(shelf:TObject);
end;
implementation
procedure Staff.notice(shlf:TObject); // 循環参照の回避
var _shelf:Shelf;
begin
_shelf := shlf as Shelf;
Writeln(Format('%sは残り%d個です。', [_shelf.getName(), _shelf.getStock()]));
end;
end.